Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add management command to report total quotas #86

Open
jtriley opened this issue Feb 1, 2023 · 0 comments
Open

add management command to report total quotas #86

jtriley opened this issue Feb 1, 2023 · 0 comments

Comments

@jtriley
Copy link
Contributor

jtriley commented Feb 1, 2023

It would be useful to have a management command that reports the total quota values across all allocations

This command could also do the same on a per-project basis.

Some proof-of-concept code:

import yaml

from django.core.management.base import BaseCommand
from django.core.exceptions import ObjectDoesNotExist

from coldfront_plugin_cloud import attributes
from coldfront_plugin_cloud import openstack
from coldfront.core.resource.models import (Resource, ResourceType)
from coldfront.core.allocation.models import (Allocation, AllocationStatusChoice)


class Command(BaseCommand):
    help = 'Show cloud quotas (OpenShift and OpenStack)'

    def add_arguments(self, parser):
        parser.add_argument('type', choices=['all', 'OpenStack', 'OpenShift'], help='cloud type')
        parser.add_argument('--project-id', help='limit scope to project id')

    def get_quota_totals(self, cloud_type):
        try:
            resources = Resource.objects.filter(
                resource_type=ResourceType.objects.get(
                    name=cloud_type,
                )
            )
        except ObjectDoesNotExist as e:
            print(f'Skpping {cloud_type} - resource type does not exist')
            return 1


        allocations = Allocation.objects.filter(
            resources__in=resources,
            status=AllocationStatusChoice.objects.get(name='Active')
        )

        totals = {}

        for attr in attributes.ALLOCATION_QUOTA_ATTRIBUTES:
            if cloud_type in attr:
                total = 0
                for allocation in allocations:
                    try:
                        val = float(allocation.get_attribute(attr))
                        total += val
                    except TypeError:
                        continue
                totals[attr] = total
        print(yaml.dump(totals).strip())

    def handle(self, *args, **options):
        if (t := options['type']) != 'all':
            self.get_quota_totals(t)
        else:
            self.get_quota_totals('OpenStack')
            self.get_quota_totals('OpenShift')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants