def test_tenant_quota_usages_unlimited_quota(self): inf_quota = self.quotas.first() inf_quota['ram'] = -1 quotas.is_service_enabled(IsA(http.HttpRequest), 'volume').AndReturn(True) api.nova.flavor_list(IsA(http.HttpRequest)) \ .AndReturn(self.flavors.list()) api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(inf_quota) api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \ .AndReturn(self.floating_ips.list()) api.nova.server_list(IsA(http.HttpRequest)) \ .AndReturn(self.servers.list()) vsm.volume_list(IsA(http.HttpRequest)) \ .AndReturn(self.volumes.list()) vsm.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(inf_quota) self.mox.ReplayAll() quota_usages = quotas.tenant_quota_usages(self.request) expected_output = { 'injected_file_content_bytes': {'quota': 1}, 'metadata_items': {'quota': 1}, 'injected_files': {'quota': 1}, 'security_groups': {'quota': 10}, 'security_group_rules': {'quota': 20}, 'fixed_ips': {'quota': 10}, 'gigabytes': {'available': 920, 'used': 80, 'quota': 1000}, 'ram': {'available': float("inf"), 'used': 1024, 'quota': float("inf")}, 'floating_ips': {'available': 0, 'used': 2, 'quota': 1}, 'instances': {'available': 8, 'used': 2, 'quota': 10}, 'volumes': {'available': 0, 'used': 3, 'quota': 1}, 'cores': {'available': 8, 'used': 2, 'quota': 10} } # Compare internal structure of usages to expected. self.assertEquals(quota_usages.usages, expected_output)
def tenant_quota_usages(request): # Get our quotas and construct our usage object. disabled_quotas = get_disabled_quotas(request) usages = QuotaUsage() for quota in get_tenant_quota_data(request, disabled_quotas=disabled_quotas): usages.add_quota(quota) # Get our usages. floating_ips = network.tenant_floating_ip_list(request) flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) instances = nova.server_list(request) # Fetch deleted flavors if necessary. missing_flavors = [ instance.flavor['id'] for instance in instances if instance.flavor['id'] not in flavors ] for missing in missing_flavors: if missing not in flavors: try: flavors[missing] = nova.flavor_get(request, missing) except: flavors[missing] = {} exceptions.handle(request, ignore=True) usages.tally('instances', len(instances)) usages.tally('floating_ips', len(floating_ips)) if 'volumes' not in disabled_quotas: volumes = vsm.volume_list(request) usages.tally('gigabytes', sum([int(v.size) for v in volumes])) usages.tally('volumes', len(volumes)) # Sum our usage based on the flavors of the instances. for flavor in [flavors[instance.flavor['id']] for instance in instances]: usages.tally('cores', getattr(flavor, 'vcpus', None)) usages.tally('ram', getattr(flavor, 'ram', None)) # Initialise the tally if no instances have been launched yet if len(instances) == 0: usages.tally('cores', 0) usages.tally('ram', 0) return usages
def tenant_quota_usages(request): # Get our quotas and construct our usage object. disabled_quotas = get_disabled_quotas(request) usages = QuotaUsage() for quota in get_tenant_quota_data(request, disabled_quotas=disabled_quotas): usages.add_quota(quota) # Get our usages. floating_ips = network.tenant_floating_ip_list(request) flavors = dict([(f.id, f) for f in nova.flavor_list(request)]) instances = nova.server_list(request) # Fetch deleted flavors if necessary. missing_flavors = [instance.flavor['id'] for instance in instances if instance.flavor['id'] not in flavors] for missing in missing_flavors: if missing not in flavors: try: flavors[missing] = nova.flavor_get(request, missing) except: flavors[missing] = {} exceptions.handle(request, ignore=True) usages.tally('instances', len(instances)) usages.tally('floating_ips', len(floating_ips)) if 'volumes' not in disabled_quotas: volumes = vsm.volume_list(request) usages.tally('gigabytes', sum([int(v.size) for v in volumes])) usages.tally('volumes', len(volumes)) # Sum our usage based on the flavors of the instances. for flavor in [flavors[instance.flavor['id']] for instance in instances]: usages.tally('cores', getattr(flavor, 'vcpus', None)) usages.tally('ram', getattr(flavor, 'ram', None)) # Initialise the tally if no instances have been launched yet if len(instances) == 0: usages.tally('cores', 0) usages.tally('ram', 0) return usages
def test_tenant_quota_usages(self): quotas.is_service_enabled(IsA(http.HttpRequest), 'volume').AndReturn(True) api.nova.flavor_list(IsA(http.HttpRequest)) \ .AndReturn(self.flavors.list()) api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(self.quotas.first()) api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \ .AndReturn(self.floating_ips.list()) api.nova.server_list(IsA(http.HttpRequest)) \ .AndReturn(self.servers.list()) vsm.volume_list(IsA(http.HttpRequest)) \ .AndReturn(self.volumes.list()) vsm.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(self.quotas.first()) self.mox.ReplayAll() quota_usages = quotas.tenant_quota_usages(self.request) expected_output = { 'injected_file_content_bytes': { 'quota': 1 }, 'metadata_items': { 'quota': 1 }, 'injected_files': { 'quota': 1 }, 'security_groups': { 'quota': 10 }, 'security_group_rules': { 'quota': 20 }, 'fixed_ips': { 'quota': 10 }, 'gigabytes': { 'available': 920, 'used': 80, 'quota': 1000 }, 'ram': { 'available': 8976, 'used': 1024, 'quota': 10000 }, 'floating_ips': { 'available': 0, 'used': 2, 'quota': 1 }, 'instances': { 'available': 8, 'used': 2, 'quota': 10 }, 'volumes': { 'available': 0, 'used': 3, 'quota': 1 }, 'cores': { 'available': 8, 'used': 2, 'quota': 10 } } # Compare internal structure of usages to expected. self.assertEquals(quota_usages.usages, expected_output)