def compare_quotas(name_or_id=None): """Compare the allocation and quota information for a tenant """ if name_or_id == None: print 'A tenant name or id is required' return keystone_api = hm_keystone.client_session(version=3) try: tenant = hm_keystone.get_tenant(keystone_api, name_or_id) except: print 'Tenant {0} not found in keystone'.format(name_or_id) return nova_api = hm_nova.client() quotas = nova_api.quotas.get(tenant.id) print 'nova quotas: instances {0}, cores {1}, ram {2}'.format( quotas.instances, quotas.cores, quotas.ram / 1024) usage = _get_usage(nova_api, _get_flavor_map(nova_api), tenant.id) print 'nova usage: instances {0}, cores {1}, ram {2}'.format( usage['instances'], usage['vcpus'], usage['ram'] / 1024) allocations_api = NectarApiSession() allocations = allocations_api.get_allocations(); tenant_allocations = filter(lambda x: x['tenant_uuid'] == tenant.id and \ (x['status'] == 'A' or x['status'] == 'X'), allocations) if len(tenant_allocations) == 0: print 'No approved allocation records for tenant {0} / {1}'.format( tenant.id, tenant.name) return tenant_allocations.sort(key=lambda alloc: alloc['modified_time']) current_allocation = tenant_allocations[-1] format = '{0} mismatch: allocated {1}, nova {2}, used {3}' if current_allocation['instance_quota'] != quotas.instances: print format.format('Instance quota', current_allocation['instance_quota'], quotas.instances, usage['instances']) if current_allocation['core_quota'] != quotas.cores: print format.format('VCPU quota', current_allocation['core_quota'], quotas.cores, usage['vcpus']) if current_allocation['ram_quota'] * 1024 != quotas.ram: print format.format('RAM quota', current_allocation['ram_quota'] * 1024, quotas.ram, usage['ram'])
def _get_current_allocations(): allocations_api = NectarApiSession() allocations = allocations_api.get_allocations(); tenant_allocations = {} for alloc in allocations: if alloc['status'] != 'A' and alloc['status'] != 'X': continue uuid = alloc['tenant_uuid'] if uuid == None or uuid == '': continue if uuid in tenant_allocations: old_alloc = tenant_allocations[uuid] if alloc['modified_time'] > old_alloc['modified_time']: tenant_allocations[uuid] = alloc else: tenant_allocations[uuid] = alloc return tenant_allocations