def get_incorrect_usage(cntxt, tenant): existing_usage = db.quota_usage_get_all_by_project(cntxt, tenant) # {u'ram': {'reserved': 0L, 'in_use': 0L}, # u'floating_ips': {'reserved': 0L, 'in_use': 1L}, # u'instances': {'reserved': 0L, 'in_use': 0L}, # u'cores': {'reserved': 0L, 'in_use': 0L}, # 'project_id': tenant, # u'security_groups': {'reserved': 0L, 'in_use': 1L}} # # Get (instance_count, total_cores, total_ram) for project. # If instances does not exist, then this try: security_groups = existing_usage["security_groups"]["in_use"] except KeyError: security_groups = 1 try: instances = existing_usage["instances"]["in_use"] except KeyError: instances = 0 try: cores = existing_usage["cores"]["in_use"] except KeyError: cores = 0 try: ram = existing_usage["ram"]["in_use"] except KeyError: ram = 0 return OrderedDict( (("db_instance_count", instances), ("db_core_count", cores), ("db_ram_count", ram), ("db_secgroup_count", security_groups)))
def get_project_quotas(self, context, resources, project_id, quota_class=None, defaults=True, usages=True): """ Given a list of resources, retrieve the quotas for the given project. :param context: The request context, for access checks. :param resources: A dictionary of the registered resources. :param project_id: The ID of the project to return quotas for. :param quota_class: If project_id != context.project_id, the quota class cannot be determined. This parameter allows it to be specified. It will be ignored if project_id == context.project_id. :param defaults: If True, the quota class value (or the default value, if there is no value from the quota class) will be reported if there is no specific value for the resource. :param usages: If True, the current in_use and reserved counts will also be returned. """ project_quotas = db.quota_get_all_by_project(context, project_id) project_usages = None if usages: project_usages = db.quota_usage_get_all_by_project(context, project_id) return self._process_quotas(context, resources, project_id, project_quotas, quota_class=quota_class, defaults=defaults, usages=project_usages)
def get_incorrect_usage(cntxt, tenant): existing_usage = db.quota_usage_get_all_by_project(cntxt, tenant) # {u'ram': {'reserved': 0L, 'in_use': 0L}, # u'floating_ips': {'reserved': 0L, 'in_use': 1L}, # u'instances': {'reserved': 0L, 'in_use': 0L}, # u'cores': {'reserved': 0L, 'in_use': 0L}, # 'project_id': tenant, # u'fixed_ips': {'reserved': 0L, 'in_use': 0L}, # u'security_groups': {'reserved': 0L, 'in_use': 1L}} # # Get (instance_count, total_cores, total_ram) for project. # If instances does not exist, then this if 'instances' in existing_usage: # it's possible for accounts to use nothing but the system default # group, which doesn't get tracked, so default to 1 try: security_groups = existing_usage["security_groups"]["in_use"] except KeyError: security_groups = 1 return OrderedDict(( ("db_instance_count", existing_usage["instances"]["in_use"]), ("db_core_count", existing_usage["cores"]["in_use"]), ("db_ram_count", existing_usage["ram"]["in_use"]), ("db_fixed_ips", existing_usage["fixed_ips"]["in_use"]), ("db_secgroup_count", security_groups) )) else: print "%s get_incorrect_usage failed to find quota usage information in " \ "database. Is this tenant in use?" % tenant exit(0)
def test_discard(self): blessed_uuid = utils.create_blessed_instance(self.context) pre_usages = db.quota_usage_get_all_by_project(self.context, self.context.project_id) self.cobalt_api.discard_instance(self.context, blessed_uuid) instance = db.instance_get_by_uuid(self.context, blessed_uuid) self.assertEqual(task_states.DELETING, instance['task_state']) # Assert that the resources have diminished. post_usages = db.quota_usage_get_all_by_project(self.context, self.context.project_id) self.assertEqual(pre_usages['instances'].get('in_use',0) - 1, post_usages['instances'].get('in_use',0)) self.assertEqual(pre_usages['ram'].get('in_use', 0) - instance['memory_mb'], post_usages['ram'].get('in_use',0)) self.assertEqual(pre_usages['cores'].get('in_use',0) - instance['vcpus'], post_usages['cores'].get('in_use',0))
def get_project_quotas(self, context, resources, project_id, quota_class=None, defaults=True, usages=True): """ Given a list of resources, retrieve the quotas for the given project. :param context: The request context, for access checks. :param resources: A dictionary of the registered resources. :param project_id: The ID of the project to return quotas for. :param quota_class: If project_id != context.project_id, the quota class cannot be determined. This parameter allows it to be specified. It will be ignored if project_id == context.project_id. :param defaults: If True, the quota class value (or the default value, if there is no value from the quota class) will be reported if there is no specific value for the resource. :param usages: If True, the current in_use and reserved counts will also be returned. """ quotas = {} project_quotas = db.quota_get_all_by_project(context, project_id) if usages: project_usages = db.quota_usage_get_all_by_project(context, project_id) # Get the quotas for the appropriate class. If the project ID # matches the one in the context, we use the quota_class from # the context, otherwise, we use the provided quota_class (if # any) if project_id == context.project_id: quota_class = context.quota_class if quota_class: class_quotas = db.quota_class_get_all_by_name(context, quota_class) else: class_quotas = {} for resource in resources.values(): # Omit default/quota class values if not defaults and resource.name not in project_quotas: continue quotas[resource.name] = dict( limit=project_quotas.get(resource.name, class_quotas.get(resource.name, resource.default)) ) # Include usages if desired. This is optional because one # internal consumer of this interface wants to access the # usages directly from inside a transaction. if usages: usage = project_usages.get(resource.name, {}) quotas[resource.name].update(in_use=usage.get("in_use", 0), reserved=usage.get("reserved", 0)) return quotas
def test_double_discard(self): blessed_uuid = utils.create_blessed_instance(self.context) pre_usages = db.quota_usage_get_all_by_project(self.context, self.context.project_id) self.cobalt_api.discard_instance(self.context, blessed_uuid) self.cobalt_api.discard_instance(self.context, blessed_uuid) instance = db.instance_get_by_uuid(self.context, blessed_uuid) self.assertEqual(task_states.DELETING, instance["task_state"]) # Assert that the resources have diminished only once and not twice since we have # discarded twice. post_usages = db.quota_usage_get_all_by_project(self.context, self.context.project_id) self.assertEqual(pre_usages["instances"].get("in_use", 0) - 1, post_usages["instances"].get("in_use", 0)) self.assertEqual( pre_usages["ram"].get("in_use", 0) - instance["memory_mb"], post_usages["ram"].get("in_use", 0) ) self.assertEqual( pre_usages["cores"].get("in_use", 0) - instance["vcpus"], post_usages["cores"].get("in_use", 0) )
def get_project_quotas(self, context, resources, project_id, quota_class=None, defaults=True, usages=True): """ Given a list of resources, retrieve the quotas for the given project. :param context: The request context, for access checks. :param resources: A dictionary of the registered resources. :param project_id: The ID of the project to return quotas for. :param quota_class: If project_id != context.project_id, the quota class cannot be determined. This parameter allows it to be specified. It will be ignored if project_id == context.project_id. :param defaults: If True, the quota class value (or the default value, if there is no value from the quota class) will be reported if there is no specific value for the resource. :param usages: If True, the current in_use and reserved counts will also be returned. """ project_quotas = db.quota_get_all_by_project(context, project_id) project_usages = None if usages: project_usages = db.quota_usage_get_all_by_project( context, project_id) return self._process_quotas(context, resources, project_id, project_quotas, quota_class=quota_class, defaults=defaults, usages=project_usages)
def get_incorrect_usage(cntxt, tenant): existing_usage = db.quota_usage_get_all_by_project(cntxt, tenant) # {u'ram': {'reserved': 0L, 'in_use': 0L}, # u'floating_ips': {'reserved': 0L, 'in_use': 1L}, # u'instances': {'reserved': 0L, 'in_use': 0L}, # u'cores': {'reserved': 0L, 'in_use': 0L}, # 'project_id': tenant, # u'security_groups': {'reserved': 0L, 'in_use': 1L}} # # Get (instance_count, total_cores, total_ram) for project. # If instances does not exist, then this try: security_groups = existing_usage["security_groups"]["in_use"] except KeyError: security_groups = 1 try: instances = existing_usage["instances"]["in_use"] except KeyError: instances = 0 try: cores = existing_usage["cores"]["in_use"] except KeyError: cores = 0 try: ram = existing_usage["ram"]["in_use"] except KeyError: ram = 0 return OrderedDict(( ("db_instance_count", instances), ("db_core_count", cores), ("db_ram_count", ram), ("db_secgroup_count", security_groups) ))
def get_project_quotas(self, context, resources, project_id, quota_class=None, defaults=True, usages=True): """ Given a list of resources, retrieve the quotas for the given project. :param context: The request context, for access checks. :param resources: A dictionary of the registered resources. :param project_id: The ID of the project to return quotas for. :param quota_class: If project_id != context.project_id, the quota class cannot be determined. This parameter allows it to be specified. It will be ignored if project_id == context.project_id. :param defaults: If True, the quota class value (or the default value, if there is no value from the quota class) will be reported if there is no specific value for the resource. :param usages: If True, the current in_use and reserved counts will also be returned. """ quotas = {} project_quotas = db.quota_get_all_by_project(context, project_id) if usages: project_usages = db.quota_usage_get_all_by_project( context, project_id) # Get the quotas for the appropriate class. If the project ID # matches the one in the context, we use the quota_class from # the context, otherwise, we use the provided quota_class (if # any) if project_id == context.project_id: quota_class = context.quota_class if quota_class: class_quotas = db.quota_class_get_all_by_name(context, quota_class) else: class_quotas = {} for resource in resources.values(): # Omit default/quota class values if not defaults and resource.name not in project_quotas: continue quotas[resource.name] = dict(limit=project_quotas.get( resource.name, class_quotas.get(resource.name, resource.default)), ) # Include usages if desired. This is optional because one # internal consumer of this interface wants to access the # usages directly from inside a transaction. if usages: usage = project_usages.get(resource.name, {}) quotas[resource.name].update( in_use=usage.get('in_use', 0), reserved=usage.get('reserved', 0), ) return quotas
def fix_usage(cntxt, tenant, actual_table_name, incorrect_table_name): print "\nUpdating quota usage to reflect actual usage..\n" # Calculate differences existing_usage = db.quota_usage_get_all_by_project(cntxt, tenant) actual = get_actual_usage(cntxt, tenant) # it's possible for accounts to use nothing but the system default # group, which doesn't get tracked, so default to 0 try: security_groups = existing_usage["security_groups"]["in_use"] secgroup_difference = security_groups - actual["actual_secgroup_count"] except KeyError: security_groups = None instance_difference = \ existing_usage["instances"]["in_use"] - actual["actual_instance_count"] core_difference = \ existing_usage["cores"]["in_use"] - actual["actual_core_count"] ram_difference = \ existing_usage["ram"]["in_use"] - actual["actual_ram_count"] # Actual_fixed_ips should be the same as actual_instance_count since there is no # Reason in our environment for someone to use two fixed IPs # existing_usage["fixed_ips"]["in_use"]-actual["actual_fixed_ips"] fixedips_difference = \ existing_usage["fixed_ips"]["in_use"] - actual["actual_instance_count"] # Quota_usage_update(context, project_id, resource, **kwargs) # Update ram. db.quota_usage_update(cxt, tenant, 'ram', in_use=existing_usage["ram"]["in_use"] - ram_difference) # Update instances db.quota_usage_update(cxt, tenant, 'instances', in_use=existing_usage["instances"]["in_use"] - instance_difference) # Update cores db.quota_usage_update(cxt, tenant, 'cores', in_use=existing_usage["cores"]["in_use"] - core_difference) # Update fixed IPs """ db.quota_usage_update(cxt, tenant, 'fixed_ips', in_use=existing_usage["fixed_ips"]["in_use"] - fixedips_difference) """ # Update security groups if security_groups is not None: db.quota_usage_update(cxt, tenant, 'security_groups', in_use=security_groups - secgroup_difference) print "############### Actual Usage (including non-active instances) ###############" print make_table(actual_table_name, get_actual_usage(cxt, tenant).values()) print "############### Corrected Database Usage ###############" print make_table(incorrect_table_name, get_incorrect_usage(cxt, tenant).values())