def refresh_tenant_vms_and_nets(self, vmDict, netDict): '''Refreshes the status of the dictionaries of VM instances and nets passed as arguments. It modifies the dictionaries''' '''Returns: - result: 0 if all elements could be refreshed (even if its status didn't change) n>0, the number of elements that couldn't be refreshed, <0 if error (foreseen) - error_msg: text with reference to possible errors ''' #vms_refreshed = [] #nets_refreshed = [] vms_unrefreshed = [] nets_unrefreshed = [] for vm_id in vmDict: print "VIMConnector refresh_tenant_vms and nets: Getting tenant VM instance information from VIM" headers_req = {'content-type': 'application/json'} url = self.url+'/'+self.tenant+'/servers/'+ vm_id print url try: vim_response = requests.get(url, headers = headers_req) except requests.exceptions.RequestException, e: print "VIMConnector refresh_tenant_elements. Exception: ", e.args vms_unrefreshed.append(vm_id) continue #print vim_response #print vim_response.status_code if vim_response.status_code == 200: #print vim_response.json() #print json.dumps(vim_response.json(), indent=4) res,http_content = af.format_in(vim_response, openmano_schemas.new_vminstance_response_schema) if res: #print json.dumps(http_content, indent=4) #OLD: #status = http_content['server']['status'] #if vmDict[vm_id] != status: # vmDict[vm_id] = status # vms_refreshed.append(vm_id) #NEW: vmDict[vm_id] = http_content['server']['status'] #print http_content['server']['hostId'] else: vms_unrefreshed.append(vm_id) else: #print vim_response.text jsonerror = af.format_jsonerror(vim_response) print 'VIMConnector refresh_tenant_vms_and_nets. Error in VIM "%s": not possible to get VM instance. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror) vms_unrefreshed.append(vm_id)
class vimconnector(): def new_host(self, vimURIadmin, host_data): '''Adds a new host to VIM''' '''Returns status code of the VIM response''' print "VIMConnector: Adding a new host" headers_req = {'content-type': 'application/json'} payload_req = host_data try: vim_response = requests.post(vimURIadmin + '/hosts', headers=headers_req, data=payload_req) except requests.exceptions.RequestException, e: print "new_host Exception: ", e.args return -HTTP_Not_Found, str(e.args[0]) print vim_response #print vim_response.status_code if vim_response.status_code == 200: #print vim_response.json() #print json.dumps(vim_response.json(), indent=4) res, http_content = af.format_in( vim_response, openmano_schemas.new_host_response_schema) #print http_content if res: r = af.remove_extra_items( http_content, openmano_schemas.new_host_response_schema) if r is not None: print "Warning: remove extra items ", r #print http_content host_id = http_content['host']['id'] #print "Host id: ",host_id return vim_response.status_code, host_id else: return -HTTP_Bad_Request, http_content else: #print vim_response.text jsonerror = af.format_jsonerror(vim_response) text = 'Error in VIM "%s": not possible to add new host. HTTP Response: %d. Error: %s' % ( vimURIadmin, vim_response.status_code, jsonerror) #print text return -vim_response.status_code, text
if vim_response.status_code == 200: #print vim_response.json() #print json.dumps(vim_response.json(), indent=4) res, http_content = af.format_in(vim_response, openmano_schemas.new_port_response_schema) #print http_content if res: r = af.remove_extra_items(http_content, openmano_schemas.new_port_response_schema) if r is not None: print "Warning: remove extra items ", r #print http_content port_id = http_content['port']['id'] print "Port id: ",port_id return vim_response.status_code,port_id else: return -HTTP_Bad_Request,http_content else: #print vim_response.text jsonerror = af.format_jsonerror(vim_response) text = 'Error in VIM "%s": not possible to add new external port. HTTP Response: %d. Error: %s' % (self.url_admin, vim_response.status_code, jsonerror) #print text return -vim_response.status_code,text def new_external_network(self,net_name,net_type): '''Adds a external network to VIM (shared)''' '''Returns the network identifier''' print "VIMConnector: Adding external shared network to VIM (type " + net_type + "): "+ net_name headers_req = {'content-type': 'application/json'} payload_req = '{"network":{"name": "' + net_name + '","shared":true,"type": "' + net_type + '"}}' try: vim_response = requests.post(self.url+'/networks', headers = headers_req, data=payload_req) except requests.exceptions.RequestException, e: print "new_external_network Exception: ", e.args
res, http_content = af.format_in( vim_response, openmano_schemas.new_port_response_schema) #print http_content if res: r = af.remove_extra_items( http_content, openmano_schemas.new_port_response_schema) if r is not None: print "Warning: remove extra items ", r #print http_content port_id = http_content['port']['id'] print "Port id: ", port_id return vim_response.status_code, port_id else: return -HTTP_Bad_Request, http_content else: #print vim_response.text jsonerror = af.format_jsonerror(vim_response) text = 'Error in VIM "%s": not possible to add new external port. HTTP Response: %d. Error: %s' % ( vimURIadmin, vim_response.status_code, jsonerror) #print text return -vim_response.status_code, text def new_external_network(self, vimURI, net_name, net_type): '''Adds a external network to VIM (shared)''' '''Returns the network identifier''' print "VIMConnector: Adding external shared network to VIM (type " + net_type + "): " + net_name headers_req = {'content-type': 'application/json'} payload_req = '{"network":{"name": "' + net_name + '","shared":true,"type": "' + net_type + '"}}' try: vim_response = requests.post(vimURI + '/networks', headers=headers_req,