-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add management command to list cloud quotas
- Loading branch information
Showing
1 changed file
with
94 additions
and
0 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
src/coldfront_plugin_cloud/management/commands/cloud_quotas.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import json | ||
import logging | ||
|
||
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) | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Show cloud quotas (OpenShift and OpenStack)' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'--cloud-type', | ||
choices=['all', 'OpenStack', 'OpenShift'], | ||
default='all', | ||
help='cloud type' | ||
) | ||
parser.add_argument( | ||
'--project-id', | ||
help='limit scope to project id' | ||
) | ||
parser.add_argument( | ||
'--format', | ||
choices=['json'], | ||
default='json', | ||
help='output format' | ||
) | ||
|
||
def get_cloud_attrs(self, cloud_type): | ||
attrs = [ | ||
i for i in attributes.ALLOCATION_QUOTA_ATTRIBUTES if cloud_type in i | ||
] | ||
return attrs | ||
|
||
def get_quota_totals(self, cloud_type, project_id=None): | ||
try: | ||
resources = Resource.objects.filter( | ||
resource_type=ResourceType.objects.get( | ||
name=cloud_type, | ||
) | ||
) | ||
except ObjectDoesNotExist as e: | ||
logger.error(f'{cloud_type} resource type does not exist') | ||
return 1 | ||
|
||
filter_kwargs = {} | ||
|
||
if project_id: | ||
filter_kwargs['project_id'] = project_id | ||
|
||
allocations = Allocation.objects.filter( | ||
project='all', | ||
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 render_json(self, quota_totals): | ||
print(json.dumps(quota_totals, indent=4)) | ||
|
||
def handle(self, *args, **options): | ||
fmt = options['format'] | ||
cloud_type = options['cloud_type'] | ||
project_id = options.get('project_id', None) | ||
|
||
if cloud_type != 'all': | ||
quota_totals = self.get_quota_totals(cloud_type, project_id=project_id) | ||
else: | ||
quota_totals = self.get_quota_totals('OpenStack', project_id=project_id) | ||
quota_totals.update(self.get_quota_totals('OpenShift', project_id=project_id)) | ||
|
||
if fmt == 'json': | ||
self.render_json(quota_totals) |