|
| 1 | +import json |
| 2 | +import logging |
| 3 | + |
| 4 | +from django.core.management.base import BaseCommand |
| 5 | +from django.core.exceptions import ObjectDoesNotExist |
| 6 | + |
| 7 | +from coldfront_plugin_cloud import attributes |
| 8 | +from coldfront_plugin_cloud import openstack |
| 9 | +from coldfront.core.resource.models import (Resource, ResourceType) |
| 10 | +from coldfront.core.allocation.models import (Allocation, AllocationStatusChoice) |
| 11 | + |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +class Command(BaseCommand): |
| 17 | + help = 'Show cloud quotas (OpenShift and OpenStack)' |
| 18 | + |
| 19 | + def add_arguments(self, parser): |
| 20 | + parser.add_argument( |
| 21 | + '--cloud-type', |
| 22 | + choices=['all', 'OpenStack', 'OpenShift'], |
| 23 | + default='all', |
| 24 | + help='cloud type' |
| 25 | + ) |
| 26 | + parser.add_argument( |
| 27 | + '--project-id', |
| 28 | + help='limit scope to project id' |
| 29 | + ) |
| 30 | + parser.add_argument( |
| 31 | + '--format', |
| 32 | + choices=['json'], |
| 33 | + default='json', |
| 34 | + help='output format' |
| 35 | + ) |
| 36 | + |
| 37 | + def get_cloud_attrs(self, cloud_type): |
| 38 | + attrs = [ |
| 39 | + i for i in attributes.ALLOCATION_QUOTA_ATTRIBUTES if cloud_type in i |
| 40 | + ] |
| 41 | + return attrs |
| 42 | + |
| 43 | + def get_quota_totals(self, cloud_type, project_id=None): |
| 44 | + try: |
| 45 | + resources = Resource.objects.filter( |
| 46 | + resource_type=ResourceType.objects.get( |
| 47 | + name=cloud_type, |
| 48 | + ) |
| 49 | + ) |
| 50 | + except ObjectDoesNotExist as e: |
| 51 | + logger.error(f'{cloud_type} resource type does not exist') |
| 52 | + return 1 |
| 53 | + |
| 54 | + filter_kwargs = {} |
| 55 | + |
| 56 | + if project_id: |
| 57 | + filter_kwargs['project_id'] = project_id |
| 58 | + |
| 59 | + allocations = Allocation.objects.filter( |
| 60 | + project='all', |
| 61 | + resources__in=resources, |
| 62 | + status=AllocationStatusChoice.objects.get(name='Active'), |
| 63 | + ) |
| 64 | + |
| 65 | + totals = {} |
| 66 | + |
| 67 | + for attr in attributes.ALLOCATION_QUOTA_ATTRIBUTES: |
| 68 | + if cloud_type in attr: |
| 69 | + total = 0 |
| 70 | + for allocation in allocations: |
| 71 | + try: |
| 72 | + val = float(allocation.get_attribute(attr)) |
| 73 | + total += val |
| 74 | + except TypeError: |
| 75 | + continue |
| 76 | + totals[attr] = total |
| 77 | + print(yaml.dump(totals).strip()) |
| 78 | + |
| 79 | + def render_json(self, quota_totals): |
| 80 | + print(json.dumps(quota_totals, indent=4)) |
| 81 | + |
| 82 | + def handle(self, *args, **options): |
| 83 | + fmt = options['format'] |
| 84 | + cloud_type = options['cloud_type'] |
| 85 | + project_id = options.get('project_id', None) |
| 86 | + |
| 87 | + if cloud_type != 'all': |
| 88 | + quota_totals = self.get_quota_totals(cloud_type, project_id=project_id) |
| 89 | + else: |
| 90 | + quota_totals = self.get_quota_totals('OpenStack', project_id=project_id) |
| 91 | + quota_totals.update(self.get_quota_totals('OpenShift', project_id=project_id)) |
| 92 | + |
| 93 | + if fmt == 'json': |
| 94 | + self.render_json(quota_totals) |
0 commit comments