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
def post(self): data_filter = flask.request.get_json() filter_list = list(data_filter.keys()) config = flask.current_app.osloconfig neutron = OpenStackClients(config).neutron() networks = neutron.list_networks()['networks'] resources = [ NetworkResource(network, 'network', neutron).export()['networkData'] for network in networks ] #------------------------------------------------------ # For debug # f = open( 'file.txt', 'w' ) # f.write(resources) # f.close() # f = open("file.txt").read() # import json # resources = json.loads(f) #------------------------------------------------------ filtered_ressource = [] for network in resources: # import ipdb; ipdb.set_trace() match = False for item in filter_list: # import ipdb; ipdb.set_trace() try: test = self.test_value(data_filter[item], network) except (KeyError) as e: print(e) return flask.jsonify('Error param name, for ' + item + ' (' + str(e) + ')'), OK if test == '-1': return flask.jsonify('Error wrong operator, for ' + item), OK elif test: match = True else: match = False break if match: filtered_ressource.append(network) # import ipdb; ipdb.set_trace() return flask.jsonify(filtered_ressource), OK
def get(self): #import ipdb; ipdb.set_trace() config = flask.current_app.osloconfig nova = OpenStackClients(config).nova() #neutron = OpenStackClients(config).neutron() availabilityZones = nova.availability_zones.list() resourceZones = [] for availabilityZone in availabilityZones: if availabilityZone.zoneName == 'internal': continue resourceZone = { 'zoneId': conf.cfg.CONF.ressource_zone.zoneId, 'zoneName': availabilityZone.zoneName, 'zoneState': 'available' if availabilityZone.zoneState['available'] else 'unavailable', 'nfviPopId': conf.cfg.CONF.ressource_zone.nfviPopId, 'zoneProperty': conf.cfg.CONF.ressource_zone.zoneProperty } resourceZones.append(resourceZone) return flask.jsonify(resourceZones), 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() servers = nova.servers.list() resources = [extract_virtual_compute(server) for server in servers] filtered_ressource = [] for server in resources: match = False for item in filter_list: # Get param value from server try: value = self.get_param_value(server, 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_ressource.append(server) # import ipdb; ipdb.set_trace() return flask.jsonify(filtered_ressource), OK
def convert_name_to_uid_image(self, name): config = flask.current_app.osloconfig glance = OpenStackClients(config).glance() for image in glance.images.list(): if image.name == name: return image.id return name
def convert_name_to_uid_flavour(self, name): config = flask.current_app.osloconfig nova = OpenStackClients(config).nova() for flavors in nova.flavors.list(): if flavors.name == name: return flavors.id return name
def post(self): config = flask.current_app.osloconfig nova = OpenStackClients(config).nova() data = flask.request.get_json() resource_group_id = data['resourceGroupId'] compute_quota = data['virtualComputeQuota'] resources = {} if compute_quota.get('numVCPUs'): resources['cores'] = compute_quota.get('numVCPUs') if compute_quota.get('numVcInstances'): resources['instances'] = compute_quota.get('numVcInstances') if compute_quota.get('virtualMemSize'): resources['ram'] = compute_quota.get('virtualMemSize') nova.quotas.update(resource_group_id, **resources) quotas = nova.quotas.get(resource_group_id) quotas_data = { 'resourceGroupId': quotas.id, 'numVCPUs': quotas.cores, 'numVcInstances': quotas.instances, 'virtualMemSize': quotas.ram } return flask.jsonify(quotas_data), CREATED
def delete(self, id): config = flask.current_app.osloconfig nova = OpenStackClients(config).nova() nova.flavors.delete(id) return '', OK
def get(self): config = flask.current_app.osloconfig neutron = OpenStackClients(config).neutron() val_list = neutron.list_networks() networks = val_list['networks'] physical_network = conf.cfg.CONF.nfvi_pop.inter_pop_physical_network input_inter_pop_vlans = conf.cfg.CONF.nfvi_pop.inter_pop_vlans used_vlans = [] str_inter_pop_vlans = input_inter_pop_vlans.split(":") vlan_range = range(int(str_inter_pop_vlans[0]), int(str_inter_pop_vlans[1])) free_vlans = list(vlan_range) for network in networks: if physical_network == network['provider:physical_network']: used_vlans.append(network['provider:segmentation_id']) for vlan in used_vlans: if vlan in free_vlans: free_vlans.remove(vlan) free_vlans.sort() return flask.jsonify(free_vlans), OK
def delete(self): config = flask.current_app.osloconfig nova = OpenStackClients(config).nova() neutron = OpenStackClients(config).neutron() compute_ids = flask.request.args.getlist('computeId') stopped_servers = [] for compute_id in compute_ids: try: address = None server = nova.servers.get(compute_id) for key, value in server.addresses.items(): for item in value: if item['OS-EXT-IPS:type'] == 'floating': address = item['addr'] break server.delete() if address != None: floatingip_id = neutron.list_floatingips( floating_ip_address=address)['floatingips'][0]['id'] neutron.delete_floatingip(floatingip_id) stopped_servers.append(compute_id) except Exception: pass return flask.jsonify(stopped_servers), OK
def post(self): data_filter = flask.request.get_json() filter_list = list(data_filter.keys()) config = flask.current_app.osloconfig glance = OpenStackClients(config).glance() glance_images = glance.images.list() images = [{ 'id': image['id'], 'name': image['name'], 'provider': image['owner'], 'version': image.get('version', None), 'checksum': image['checksum'], 'containerFormat': image['container_format'], 'diskFormat': image['disk_format'], 'createdAt': image['created_at'], 'updatedAt': image['updated_at'], 'minDisk': image['min_disk'], 'minRam': image['min_ram'], 'size': image['size'], 'status': image['status'] } for image in glance_images] filtered_image = [] for image in images: match = False for item in filter_list: # Get param value from server try: value = self.get_param_value(image, 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_image.append(image) return flask.jsonify(filtered_image), OK
def post(self): data = flask.request.get_json() config = flask.current_app.osloconfig nova = OpenStackClients(config).nova() id = data['flavourId'] ram = data['virtualMemory']['virtualMemSize'] vcpus = data['virtualCpu']['numVirtualCpu'] disk = data['storageAttributes']['sizeOfStorage'] flavour = nova.flavors.create(id, ram, vcpus, disk, id) return flask.jsonify(flavour.id), CREATED
def post(self): request = flask.request if 'softwareImage' not in request.files: return flask.jsonify('No file part'), BAD_REQUEST file = request.files['softwareImage'] # if user does not select file, browser also # submit an empty part without filename if file.filename == '': return flask.jsonify('No selected file'), BAD_REQUEST # filename = secure_filename(file.filename) data = request.values config = flask.current_app.osloconfig glance = OpenStackClients(config).glance() visibility = data.get('visibility', 'private') image = glance.images.create(name=data['name'], disk_format='iso', container_format='ova', visibility=visibility, provider=data.get('provider', ''), version=data.get('version', ''), filename=file.filename) glance.images.upload(image.id, file) image = glance.images.get(image.id) data = { 'id': image['id'], 'name': image['name'], 'provider': image['owner'], 'version': image.get('version', None), 'checksum': image['checksum'], 'containerFormat': image['container_format'], 'diskFormat': image['disk_format'], 'createdAt': image['created_at'], 'updatedAt': image['updated_at'], 'minDisk': image['min_disk'], 'minRam': image['min_ram'], 'size': image['size'], 'status': image['status'] } return flask.jsonify(data), CREATED
def delete(self): config = flask.current_app.osloconfig nova = OpenStackClients(config).nova() resource_group_ids = flask.request.args.getlist('resourceGroupId') deleted_ids = [] for resource_group_id in resource_group_ids: try: nova.quotas.delete(resource_group_id) deleted_ids.append(resource_group_id) except Exception: pass return flask.jsonify(deleted_ids), OK
def get(self, id): config = flask.current_app.osloconfig glance = OpenStackClients(config).glance() image = glance.images.get(id) data = { 'id': image['id'], 'name': image['name'], 'provider': image['owner'], 'version': image.get('version', None), 'checksum': image['checksum'], 'containerFormat': image['container_format'], 'diskFormat': image['disk_format'], 'createdAt': image['created_at'], 'updatedAt': image['updated_at'], 'minDisk': image['min_disk'], 'minRam': image['min_ram'], 'size': image['size'], 'status': image['status'] } return flask.jsonify(data), OK
def delete(self): ids = flask.request.args.getlist('networkResourceId') config = flask.current_app.osloconfig neutron = OpenStackClients(config).neutron() deleted_ids = [] for id in ids: if neutron.list_networks(id=id)['networks']: try: neutron.delete_network(id) deleted_ids.append(id) except Exception: pass if neutron.list_subnets(id=id)['subnets']: # import ipdb; ipdb.set_trace() # -- Identifying an admin interface # An admin interface is a special interface, when you delete it, you need to delete the vtep vm if there is # one and release the floating ip used to access it # Check if the subnet has a gateway interface, that means it's an admin subnet if subnet_has_gateway_interface(neutron, id): address = None # check if there is a vtep vm to delete nova = OpenStackClients(config).nova() vtep_name = conf.cfg.CONF.vtep.vtep_name server_list = nova.servers.findall() try: for server in server_list: if server.name == vtep_name: for key, value in server.addresses.items(): for item in value: if item['OS-EXT-IPS:type'] == 'floating': address = item['addr'] # print ('---> found address: ', address) break # delete server server = nova.servers.delete(server.id) # just to avoid exception check if we have found a floating address if address != None: floatingip_id = neutron.list_floatingips( floating_ip_address=address )['floatingips'][0]['id'] float_delete = neutron.delete_floatingip( floatingip_id) print(float_delete) break except Exception as error_msg: print('-----> exception: ' + str(error_msg)) pass # TODO either I look for the router in the list or to improve speed, I put it in conf file # sometime there is more than one router in the tenant so maybe it would be better to have # the router_id in conf delete_router_interface_and_router(neutron, id) # delete subnet neutron.delete_subnet(id) deleted_ids.append(id) if neutron.list_ports(id=id)['ports']: try: neutron.delete_port(id) deleted_ids.append(id) except Exception: pass if neutron.list_floatingips(id=id)['floatingips']: try: neutron.delete_floatingip(id) deleted_ids.append(id) except Exception: pass # routers = neutron.list_routers(id=id) if neutron.list_routers(id=id)['routers']: try: neutron.delete_router(id) deleted_ids.append(id) except Exception as error_msg: print('-----> exception: ' + str(error_msg)) pass return flask.jsonify(deleted_ids), OK
def post(self): data = flask.request.get_json() LOG.info("network_request:" + json.dumps(data, indent=4)) config = flask.current_app.osloconfig neutron = OpenStackClients(config).neutron() name = data['networkResourceName'] network = { 'name': name, 'admin_state_up': True, } # id = data['reservationId'] if data['networkResourceType'] == 'network': if 'metadata' in data: metadata = convert_metadata_array_to_dict(data['metadata']) if "interpop_vlan" in metadata and metadata[ 'interpop_vlan'] != "": physical_network = conf.cfg.CONF.nfvi_pop.inter_pop_physical_network network.update({"provider:network_type": "vlan"}) network.update( {"provider:physical_network": physical_network}) network.update({ "provider:segmentation_id": metadata['interpop_vlan'] }) is_network_exists = check_if_network_exists(neutron, name) if is_network_exists is None: network = neutron.create_network({'network': network}) else: network['network'] = is_network_exists data = NetworkResource(network['network'], 'network', neutron).export() elif data['networkResourceType'] == 'subnet': subnet_data = data.get('typeSubnetData', None) if 'metadata' in subnet_data: metadata = convert_metadata_array_to_dict( subnet_data['metadata']) if 'dns' in metadata.keys(): meta_dns = metadata['dns'] meta_dns = meta_dns.replace("[", "") meta_dns = meta_dns.replace("]", "") meta_dns = meta_dns.replace("\"", "") meta_dns = meta_dns.split(",") elif 'subnet_type' in metadata.keys( ) and metadata['subnet_type'] == 'admin': meta_dns = conf.cfg.CONF.vtep.vtep_dns else: # we are not supposed to reach this case, but just in case meta_dns = [] else: meta_dns = [] #import ipdb; ipdb.set_trace() cidr = subnet_data['cidr'] cttcAddressPool = subnet_data.get('addressPool', None) if self.isCttcFormat(cttcAddressPool): addressPool = self.indexesToAddrPools(cidr, cttcAddressPool) cttcFormat = True else: addressPool = cttcAddressPool cttcFormat = False ip_versions = {'IPv4': 4, 'IPv6': 6} if 'gatewayIp' not in subnet_data.keys( ) or subnet_data['gatewayIp'] == "null": subnet_data['gatewayIp'] = None body_create_subnet = { 'subnet': { 'name': name, 'enable_dhcp': subnet_data['isDhcpEnabled'], 'network_id': subnet_data['networkId'], # 'segment_id': None, # 'project_id': '4fd44f30292945e481c7b8a0c8908869', # 'tenant_id': '4fd44f30292945e481c7b8a0c8908869', 'dns_nameservers': meta_dns, 'allocation_pools': addressPool, 'host_routes': [], 'ip_version': ip_versions[subnet_data['ipVersion']], 'gateway_ip': subnet_data['gatewayIp'], 'cidr': cidr, # 'id': '3b80198d-4f7b-4f77-9ef5-774d54e17126', # 'created_at': '2016-10-10T14:35:47Z', 'description': '', # 'ipv6_address_mode': None, # 'ipv6_ra_mode': None, # 'revision_number': 1, # 'service_types': [], 'subnetpool_id': None, # 'tags': ['tag1,tag2'], # 'updated_at': '2016-10-10T14:35:47Z' } } # network_name = name.replace("subnet-", "") # is_network_exists = check_if_network_exists(neutron, network_name) # if is_network_exists is None: # network = neutron.create_network({'network': network}) # else: # network['network'] = is_network_exists subnets = neutron.list_subnets(network_id=subnet_data['networkId'], name=name) if len(subnets['subnets']) != 0: data = NetworkResource(subnets['subnets'][0], 'subnet', neutron, cttcFormat).export() if cttcFormat == True: data['subnetData']['addressPool'] = cttcAddressPool else: subnet = neutron.create_subnet(body=body_create_subnet) data = NetworkResource(subnet['subnet'], 'subnet', neutron, cttcFormat).export() if cttcFormat == True: data['subnetData']['addressPool'] = cttcAddressPool if metadata and 'router_id' in metadata.keys(): router_id = metadata['router_id'] subnet_object = { 'subnet_id': data['subnetData']['resourceId'] } neutron.add_interface_router(router_id, subnet_object) if metadata and 'ip-floating-required' in metadata.keys(): floating_required = metadata['ip-floating-required'] if floating_required == "True": router_body = { 'router': { 'name': "router_" + name, 'admin_state_up': True } } router = neutron.create_router(router_body) router_id = router['router']['id'] subnet_object = { 'subnet_id': data['subnetData']['resourceId'] } neutron.add_interface_router(router_id, subnet_object) if "external_network" in metadata.keys(): external_network_name = metadata[ 'external_network'] else: external_network_name = conf.cfg.CONF.nfvi_pop.floating_network networks = neutron.list_networks( name=external_network_name)['networks'] if len(networks) > 1: message = "More then one networks with name:" + external_network_name raise Exception(message) if len(networks) == 0: message = "No networks with name:" + external_network_name print(message) raise Exception(message) router_id = router['router']['id'] network_id = networks[0]['id'] router_dict = { 'network_id': network_id, 'enable_snat': True } neutron.add_gateway_router(router_id, router_dict) elif data['networkResourceType'] == 'network-port': port_data = data.get('typeNetworkPortData', None) if 'metadata' in port_data: # floating_ip or vtep metadata = convert_metadata_array_to_dict( port_data['metadata']) if metadata['type'] == "floating_ip": body_create_floatingip = { "floatingip": { "floating_network_id": port_data['networkId'] # external network } } # request a floatingip for this project (on the network external) # this address won't be bind to any interface, it's done when the vm is created floating = neutron.create_floatingip( body=body_create_floatingip) data = NetworkResource(floating['floatingip'], 'float', neutron).export() # add the subnet of the admin network to the project's router body_add_inerface_router = { "subnet_id": metadata['subnet_id'] } # If there is no gateway interface for this subnet, it means that we need to add # this subnet to the router if not subnet_has_gateway_interface( neutron, metadata['subnet_id']): # If there is more than one router for a project, we can ad the name= of the router # in the config file router = neutron.list_routers( project_id=conf.cfg.CONF.DEFAULT.project_id) # TODO store the router_id in the config file ? router_id = router['routers'][0]['id'] neutron.add_interface_router(router_id, body_add_inerface_router) elif metadata['type'] == "vtep": port_data = data.get('typeNetworkPortData', None) nova = OpenStackClients(config).nova() vtep_name = conf.cfg.CONF.vtep.vtep_name vtep_image = conf.cfg.CONF.vtep.vtep_image vtep_flavor = conf.cfg.CONF.vtep.vtep_flavor admin_network_id = metadata['admin_network_id'] internal_ipaddr = metadata['internal_ip'] internal_network_id = metadata['internal_network_id'] # add the network interfaces _nics = [] _nics.append({'net-id': admin_network_id}) _nics.append({ 'net-id': internal_network_id, 'v4-fixed-ip': internal_ipaddr }) # the connection to the vm is done with ssh key and user ubuntu # ssh -i vim-manager-key ubuntu@floating_ip key_name = 'vim-manager-key' # param for cloudinit user_data vlan_id = port_data['segmentId'] remote_floating_ip = metadata['remote_floating_ip'] kwargs = dict( meta=None, files={}, reservation_id=None, min_count=1, max_count=1, security_groups=[], userdata=generate_cloudinit_string( vlan_id, remote_floating_ip, internal_ipaddr), key_name=key_name, availability_zone=None, block_device_mapping_v2=[], nics=_nics, scheduler_hints={}, config_drive=None, ) # create the vm server = nova.servers.create(vtep_name, vtep_image, vtep_flavor, **kwargs) # TODO not the best way, but we need to have the vm up and running to look for the port max_retry = 0 while server.status == 'BUILD': time.sleep(6) # to avoid to access openstack to often # the try is to avoid crash if the server doesn't yet exist just wait try: server = nova.servers.find(name=vtep_name) except Exception: pass max_retry = max_retry + 1 if max_retry > 10: break # get the local admin ip address to bound the floating ip address if data['networkResourceName'] in server.networks.keys(): # the local ip address is always index 0 hence the hardcoded value [0] admin_ipaddr = server.networks[ data['networkResourceName']][0] else: # TODO if we stop we probably need to delete the vtep vm because if we try another time # it won't work as the vm will already exist so we have to think of a way to clean up if # ther is a problem return flask.jsonify( 'Error wrong networkResourceName, expecting: ' + str(server.networks.keys())), OK # get the port_id of the vm_vtp admin interface ip for floating ip mapping ports = neutron.list_ports( network_id=admin_network_id)['ports'] for port in ports: for fixed_ip in port['fixed_ips']: if (fixed_ip['ip_address'] == admin_ipaddr): port_id = port['id'] break body_update_floatingip = { "floatingip": { "port_id": port_id } } # get the id of the floating ip that we want to bind to the the vtep_vm floatingip_id = neutron.list_floatingips( floating_ip_address=metadata['local_floating_ip'] )['floatingips'][0]['id'] # attach the floating ip to the interface of the vm float_update = neutron.update_floatingip( floatingip_id, body=body_update_floatingip) # disable port security for internal port (the MAC address of the the bridge interface # br_vtp is not known by openstack so if the port security is true, the traffic is blocked) # get the internal interface port id ports = neutron.list_ports( network_id=internal_network_id)['ports'] for port in ports: for fixed_ip in port['fixed_ips']: if (fixed_ip['ip_address'] == internal_ipaddr): port_id = port['id'] break body_update_security_port = { "port": { "security_groups": [], # no security group "port_security_enabled": False } } # update port with security disable port_update = neutron.update_port( port_id, body=body_update_security_port) print(port_update) # import ipdb; ipdb.set_trace() data = NetworkResource(float_update['floatingip'], 'float', neutron).export() else: # regular port creation body_create_port = { "port": { 'name': name, "admin_state_up": True, "network_id": port_data['networkId'], } } port = neutron.create_port(body=body_create_port) data = NetworkResource(port['port'], 'port', neutron).export() elif data['networkResourceType'] == 'router': body_router_create = { 'router': { 'name': name, 'admin_state_up': True } } router = neutron.create_router(body_router_create) if 'metadata' in data: metadata = convert_metadata_array_to_dict(data['metadata']) if 'external_network' in metadata.keys(): network_name = metadata['external_network'] networks = neutron.list_networks( name=network_name)['networks'] if len(networks) > 1: message = "More then one networks with name:" + network_name raise Exception(message) if len(networks) == 0: message = "No networks with name:" + network_name print(message) raise Exception(message) router_id = router['router']['id'] network_id = networks[0]['id'] router_dict = { 'network_id': network_id, 'enable_snat': True } neutron.add_gateway_router(router_id, router_dict) data = NetworkResource(router['router'], 'router', neutron).export() return flask.jsonify(data), CREATED
def post(self): data = flask.request.get_json() config = flask.current_app.osloconfig nova = OpenStackClients(config).nova() neutron = OpenStackClients(config).neutron() name = data['computeName'] image = data['vcImageId'] flavour = data['computeFlavourId'] _nics = [] for item in data.get('interfaceData'): if 'ipAddress' in item.keys(): _nics.append({ 'net-id': item['networkId'], 'v4-fixed-ip': item['ipAddress'] }) else: _nics.append({'net-id': item['networkId']}) if 'metadata' in data and 'key-name' in data['metadata'].keys(): key_name = data['metadata']['key-name'] else: key_name = None kwargs = dict( meta=None, files={}, reservation_id=None, min_count=1, max_count=1, security_groups=[], userdata=None, # key_name=None, key_name=key_name, availability_zone=None, block_device_mapping_v2=[], nics=_nics, scheduler_hints={}, config_drive=None, ) # import ipdb; ipdb.set_trace() server = nova.servers.create(name, image, flavour, **kwargs) if 'metadata' in data and 'floating-ip' in data['metadata'].keys(): # wait the vm to be up to add floating ip max_retry = 0 while server.status == 'BUILD': time.sleep(6) # to avoid to access openstack to often # the try is to avoid crash if the server doesn't yet exist just wait try: server = nova.servers.find(name=name) except Exception: pass max_retry = max_retry + 1 if max_retry > 10: break admin_ipaddr = None # get the local admin ip address to bound the floating ip address if 'admin-network-name' in data['metadata'].keys() and data[ 'metadata']['admin-network-name'] in server.networks.keys( ): # the local ip address is always index 0 hence the hardcoded value [0] admin_ipaddr = server.networks[data['metadata'] ['admin-network-name']][0] else: # TODO if we stop we probably need to delete the vtep vm because if we try another time # it won't work as the vm will already exist so we have to think of a way to clean up if # ther is a problem return flask.jsonify( 'Error wrong admin-network-name, expecting: ' + str(server.networks.keys())), OK admin_network_id = neutron.list_networks( name=data['metadata'] ['admin-network-name'])['networks'][0]['id'] # get the port_id of the vm_vtp admin interface ip for floating ip mapping ports = neutron.list_ports(network_id=admin_network_id)['ports'] for port in ports: for fixed_ip in port['fixed_ips']: if (fixed_ip['ip_address'] == admin_ipaddr): port_id = port['id'] break body_update_floatingip = {"floatingip": {"port_id": port_id}} # get the id of the floating ip that we want to bind to the the vtep_vm floatingip_id = neutron.list_floatingips( floating_ip_address=data['metadata'] ['floating-ip'])['floatingips'][0]['id'] # attach the floating ip to the interface of the vm float_update = neutron.update_floatingip( floatingip_id, body=body_update_floatingip) print(float_update) data = extract_virtual_compute(server) return flask.jsonify(data), CREATED
def post(self): data = flask.request.get_json() print('-----------------------------------------------') print(data) config = flask.current_app.osloconfig nova = OpenStackClients(config).nova() neutron = OpenStackClients(config).neutron() name = data['computeName'] image = self.convert_name_to_uid_image(data['vcImageId']) flavour = self.convert_name_to_uid_flavour(data['computeFlavourId']) external_network_name = conf.cfg.CONF.nfvi_pop.floating_network networks = neutron.list_networks( name=external_network_name)['networks'] if len(networks) > 1: message = "More then one networks with name:" + external_network_name raise Exception(message) if len(networks) == 0: message = "No networks with name:" + external_network_name print(message) raise Exception(message) floating_network_id = networks[0]['id'] _nics = [] interfaceData = data.get('interfaceData') for item in interfaceData: port_body = { "port": { "admin_state_up": "true", "network_id": item['networkId'], "port_security_enabled": "false", } } if 'ipAddress' in item.keys() and item['ipAddress'] != "": port_body['port'].update( {"fixed_ips": [{ "ip_address": item['ipAddress'] }]}) if 'macAddress' in item.keys() and item['macAddress'] != "": port_body['port'].update({"mac_address": item['macAddress']}) port = neutron.create_port(port_body) _nics.append({'port-id': port['port']['id']}) if 'floatingIP' in item.keys(): floating_body = \ { "floatingip": { "floating_network_id": floating_network_id, "port_id": port['port']['id'] } } if item['floatingIP'] != "": floating_body['floatingip'].update( {'floating_ip_address': item['floatingIP']}) floating_ip = neutron.create_floatingip(floating_body) if 'metadata' in data: metadata = convert_metadata_array_to_dict(data['metadata']) if 'key-name' in metadata.keys(): key_name = metadata['key-name'] else: key_name = None if 'userData' in data and 'content' in data['userData'].keys(): user_data = data['userData']['content'] else: user_data = None kwargs = dict( meta=None, files={}, reservation_id=None, min_count=1, max_count=1, security_groups=[], userdata=user_data, # key_name=None, key_name=key_name, availability_zone=None, block_device_mapping_v2=[], nics=_nics, scheduler_hints={}, config_drive=None, ) #import ipdb; ipdb.set_trace() server = nova.servers.create(name, image, flavour, **kwargs) # wait the vm to be up to see all information in server params (including server.networks) count = 0 max_count = 25 time.sleep(27) while (server.status == 'BUILD') and (count < max_count): time.sleep(3) # to avoid to access openstack to often # the try is to avoid crash if the server doesn't yet exist just wait try: server = nova.servers.get(server.id) except Exception: pass count = count + 1 # add foating ip address if there is one in metadata params if metadata and 'floating-ip' in metadata.keys(): admin_ipaddr = None # get the local admin ip address to bound the floating ip address if 'admin-network-name' in metadata.keys( ) and metadata['admin-network-name'] in server.networks.keys(): # the local ip address is always index 0 hence the hardcoded value [0] admin_ipaddr = server.networks[ metadata['admin-network-name']][0] else: # TODO if we stop we probably need to delete the vtep vm because if we try another time # it won't work as the vm will already exist so we have to think of a way to clean up if # ther is a problem return flask.jsonify( 'Error wrong admin-network-name, expecting: ' + str(server.networks.keys())), OK admin_network_id = neutron.list_networks( name=metadata['admin-network-name'])['networks'][0]['id'] # get the port_id of the vm_vtp admin interface ip for floating ip mapping ports = neutron.list_ports(network_id=admin_network_id)['ports'] for port in ports: for fixed_ip in port['fixed_ips']: if (fixed_ip['ip_address'] == admin_ipaddr): port_id = port['id'] break body_update_floatingip = {"floatingip": {"port_id": port_id}} # get the id of the floating ip that we want to bind to the the vtep_vm floatingip_id = neutron.list_floatingips( floating_ip_address=metadata['floating-ip'] )['floatingips'][0]['id'] # attach the floating ip to the interface of the vm float_update = neutron.update_floatingip( floatingip_id, body=body_update_floatingip) print(float_update) #import ipdb; ipdb.set_trace() server_ports = neutron.list_ports(device_id=server.id)['ports'] data = extract_virtual_compute(server, server_ports) print(data) print('-------------------------------------------------') return flask.jsonify(data), CREATED
def delete(self, id): config = flask.current_app.osloconfig glance = OpenStackClients(config).glance() glance.images.delete(id) return flask.jsonify(id), OK
def post(self): data = flask.request.get_json() config = flask.current_app.osloconfig neutron = OpenStackClients(config).neutron() name = data['networkResourceName'] # id = data['reservationId'] if data['networkResourceType'] == 'network': # network_data = data.get('typeNetworkData', None) network = {'name': name, 'admin_state_up': True} network = neutron.create_network({'network': network}) data = NetworkResource(network['network'], 'network', neutron).export() elif data['networkResourceType'] == 'subnet': subnet_data = data.get('typeSubnetData', None) if 'metadata' in subnet_data: if 'dns' in subnet_data['metadata']: meta_dns = subnet_data['metadata']['dns'] elif 'subnet_type' in subnet_data['metadata'] and subnet_data[ 'metadata']['subnet_type'] == 'admin': meta_dns = conf.cfg.CONF.vtep.vtep_dns else: # we are not supposed to reach this case, but just in case meta_dns = [] else: meta_dns = [] ip_versions = {'IPv4': 4, 'IPv6': 6} body_create_subnet = { 'subnet': { 'name': name, 'enable_dhcp': subnet_data['isDhcpEnabled'], 'network_id': subnet_data['networkId'], # 'segment_id': None, # 'project_id': '4fd44f30292945e481c7b8a0c8908869', # 'tenant_id': '4fd44f30292945e481c7b8a0c8908869', 'dns_nameservers': meta_dns, # 'allocation_pools': [ # { # 'start': '192.168.199.2', # 'end': '192.168.199.254' # } # ], 'host_routes': [], 'ip_version': ip_versions[subnet_data['ipVersion']], 'gateway_ip': subnet_data['gatewayIp'], 'cidr': subnet_data['cidr'], # 'id': '3b80198d-4f7b-4f77-9ef5-774d54e17126', # 'created_at': '2016-10-10T14:35:47Z', 'description': '', # 'ipv6_address_mode': None, # 'ipv6_ra_mode': None, # 'revision_number': 1, # 'service_types': [], 'subnetpool_id': None, # 'tags': ['tag1,tag2'], # 'updated_at': '2016-10-10T14:35:47Z' } } subnet = neutron.create_subnet(body=body_create_subnet) data = NetworkResource(subnet['subnet'], 'subnet', neutron).export() elif data['networkResourceType'] == 'network-port': port_data = data.get('typeNetworkPortData', None) if 'metadata' in port_data: # floating_ip or vtep if port_data['metadata']['type'] == "floating_ip": body_create_floatingip = { "floatingip": { "floating_network_id": port_data['networkId'] # external network } } # request a floatingip for this project (on the network external) # this address won't be bind to any interface, it's done when the vm is created floating = neutron.create_floatingip( body=body_create_floatingip) data = NetworkResource(floating['floatingip'], 'float', neutron).export() # add the subnet of the admin network to the project's router body_add_inerface_router = { "subnet_id": port_data['metadata']['subnet_id'] } # If there is no gateway interface for this subnet, it means that we need to add # this subnet to the router if not subnet_has_gateway_interface( neutron, port_data['metadata']['subnet_id']): # If there is more than one router for a project, we can ad the name= of the router # in the config file router = neutron.list_routers( project_id=conf.cfg.CONF.DEFAULT.project_id) # TODO store the router_id in the config file ? router_id = router['routers'][0]['id'] neutron.add_interface_router(router_id, body_add_inerface_router) elif port_data['metadata']['type'] == "vtep": port_data = data.get('typeNetworkPortData', None) nova = OpenStackClients(config).nova() vtep_name = conf.cfg.CONF.vtep.vtep_name vtep_image = conf.cfg.CONF.vtep.vtep_image vtep_flavor = conf.cfg.CONF.vtep.vtep_flavor admin_network_id = port_data['metadata'][ 'admin_interface']['network_id'] internal_ipaddr = port_data['metadata'][ 'internal_interface']['fixed_ip'] internal_network_id = port_data['metadata'][ 'internal_interface']['network_id'] # add the network interfaces _nics = [] _nics.append({'net-id': admin_network_id}) _nics.append({ 'net-id': internal_network_id, 'v4-fixed-ip': internal_ipaddr }) # the connection to the vm is done with ssh key and user ubuntu # ssh -i vim-manager-key ubuntu@floating_ip key_name = 'vim-manager-key' # param for cloudinit user_data vlan_id = port_data['segmentId'] remote_floating_ip = port_data['metadata'][ 'remote_floating_ip'] kwargs = dict( meta=None, files={}, reservation_id=None, min_count=1, max_count=1, security_groups=[], userdata=generate_cloudinit_string( vlan_id, remote_floating_ip, internal_ipaddr), key_name=key_name, availability_zone=None, block_device_mapping_v2=[], nics=_nics, scheduler_hints={}, config_drive=None, ) # create the vm server = nova.servers.create(vtep_name, vtep_image, vtep_flavor, **kwargs) # TODO not the best way, but we need to have the vm up and running to look for the port max_retry = 0 while server.status == 'BUILD': time.sleep(6) # to avoid to access openstack to often # the try is to avoid crash if the server doesn't yet exist just wait try: server = nova.servers.find(name=vtep_name) except Exception: pass max_retry = max_retry + 1 if max_retry > 10: break # get the local admin ip address to bound the floating ip address if data['networkResourceName'] in server.networks.keys(): # the local ip address is always index 0 hence the hardcoded value [0] admin_ipaddr = server.networks[ data['networkResourceName']][0] else: # TODO if we stop we probably need to delete the vtep vm because if we try another time # it won't work as the vm will already exist so we have to think of a way to clean up if # ther is a problem return flask.jsonify( 'Error wrong networkResourceName, expecting: ' + str(server.networks.keys())), OK # get the port_id of the vm_vtp admin interface ip for floating ip mapping ports = neutron.list_ports( network_id=admin_network_id)['ports'] for port in ports: for fixed_ip in port['fixed_ips']: if (fixed_ip['ip_address'] == admin_ipaddr): port_id = port['id'] break body_update_floatingip = { "floatingip": { "port_id": port_id } } # get the id of the floating ip that we want to bind to the the vtep_vm floatingip_id = neutron.list_floatingips( floating_ip_address=port_data['metadata'] ['local_floating_ip'])['floatingips'][0]['id'] # attach the floating ip to the interface of the vm float_update = neutron.update_floatingip( floatingip_id, body=body_update_floatingip) # disable port security for internal port (the MAC address of the the bridge interface # br_vtp is not known by openstack so if the port security is true, the traffic is blocked) # get the internal interface port id ports = neutron.list_ports( network_id=internal_network_id)['ports'] for port in ports: for fixed_ip in port['fixed_ips']: if (fixed_ip['ip_address'] == internal_ipaddr): port_id = port['id'] break body_update_security_port = { "port": { "security_groups": [], # no security group "port_security_enabled": False } } # update port with security disable port_update = neutron.update_port( port_id, body=body_update_security_port) print(port_update) # import ipdb; ipdb.set_trace() data = NetworkResource(float_update['floatingip'], 'float', neutron).export() else: # regular port creation body_create_port = { "port": { 'name': name, "admin_state_up": True, "network_id": port_data['networkId'], } } port = neutron.create_port(body=body_create_port) data = NetworkResource(port['port'], 'port', neutron).export() return flask.jsonify(data), CREATED