def get(self): # resource_types = { # 'VirtualCpuResourceInformation': 'VCPU', # 'VirtualMemoryResourceInformation': 'MEMORY_MB', # 'VirtualStorageResourceInformation': 'DISK_GB' # } resource_types = ['MEMORY_MB', 'VCPU'] data = flask.request.args.getlist('ComputeResourceTypeId') # import ipdb; ipdb.set_trace() resource_type = resource_types[int(data[0])] config = flask.current_app.osloconfig session = OpenStackClients(config).session response = session.get('/resource_providers', endpoint_filter={ 'service_type': 'placement', 'interface': 'public', 'region_name': 'RegionOne' }) resource_providers = json.loads( response.__dict__['_content'].decode("utf-8")) available_capacity = 0 total_capacity = 0 allocated_capacity = 0 # check all the compute nodes for resource_provider in resource_providers['resource_providers']: links = resource_provider['links'] for link in links: if link['rel'] == 'inventories': linkt = link['href'].replace("/placement", "") response = session.get(linkt, endpoint_filter={ 'service_type': 'placement', 'interface': 'public', 'region_name': 'RegionOne' }) data = json.loads( response.__dict__['_content'].decode("utf-8")) inventories = data['inventories'][resource_type] available_capacity = available_capacity + ( inventories['total'] - inventories['reserved']) total_capacity = total_capacity + inventories['total'] allocated_capacity = allocated_capacity + inventories[ 'reserved'] capacity_information = { 'availableCapacity': available_capacity, 'reservedCapacity': None, # May be use blazar lib 'totalCapacity': total_capacity, 'allocatedCapacity': allocated_capacity } return flask.jsonify(capacity_information), OK
def post(self): data_filter = flask.request.get_json() filter_list = list(data_filter.keys()) config = flask.current_app.osloconfig nova = OpenStackClients(config).nova() session = OpenStackClients(config).session response = session.get('/v3/projects', endpoint_filter={ 'service_type': 'identity', 'interface': 'public', 'region_name': 'RegionOne' }) content = json.loads(response.__dict__['_content'].decode("utf-8")) project_ids = [p['id'] for p in content['projects']] quotas_projects = [] for project_id in project_ids: quotas = nova.quotas.get(project_id) quotas_projects.append({ 'resourceGroupId': quotas.id, 'numVCPUs': quotas.cores, 'numVcInstances': quotas.instances, 'virtualMemSize': quotas.ram }) filtered_project = [] for project in quotas_projects: match = False for item in filter_list: # Get param value from server try: value = self.get_param_value(project, data_filter[item]['param']) except (KeyError) as e: print(e) return flask.jsonify('Error param name, for ' + item + ' (' + str(e) + ')'), OK test = common.compare_value(data_filter[item]['op'], value, data_filter[item]['value']) if test == '-1': return flask.jsonify('Error wrong operator, for ' + item), OK elif test: match = True else: match = False break if match: filtered_project.append(project) # import ipdb; ipdb.set_trace() return flask.jsonify(filtered_project), OK