From: garciadeblas Date: Thu, 17 May 2018 13:27:21 +0000 (+0200) Subject: fix vim-update by using PUT and proper handling of SDN info X-Git-Tag: v4.0.0~3 X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2Fosmclient.git;a=commitdiff_plain;h=9c79a482e22b9004d7a86fca1781ad4bf7bd32fc fix vim-update by using PUT and proper handling of SDN info Change-Id: I6cdef6caa7e4456ef89997cdc4e0601641b47490 Signed-off-by: garciadeblas --- diff --git a/osmclient/scripts/osm.py b/osmclient/scripts/osm.py index 76daaa4..fd92477 100755 --- a/osmclient/scripts/osm.py +++ b/osmclient/scripts/osm.py @@ -814,6 +814,8 @@ def ns_delete(ctx, name, force): @click.option('--description', default='no description', help='human readable description') +@click.option('--sdn_controller', default=None, help='Name or id of the SDN controller associated to this VIM account') +@click.option('--sdn_port_mapping', default=None, help="File describing the port mapping between compute nodes' ports and switch ports") @click.pass_context def vim_create(ctx, name, @@ -823,19 +825,28 @@ def vim_create(ctx, tenant, config, account_type, - description): + description, + sdn_controller, + sdn_port_mapping): '''creates a new VIM account ''' - vim = {} - vim['vim-username'] = user - vim['vim-password'] = password - vim['vim-url'] = auth_url - vim['vim-tenant-name'] = tenant - vim['config'] = config - vim['vim-type'] = account_type - vim['description'] = description try: - ctx.obj.vim.create(name, vim) + if sdn_controller: + check_client_version(ctx.obj, '--sdn_controller') + if sdn_port_mapping: + check_client_version(ctx.obj, '--sdn_port_mapping') + vim = {} + vim['vim-username'] = user + vim['vim-password'] = password + vim['vim-url'] = auth_url + vim['vim-tenant-name'] = tenant + vim['vim-type'] = account_type + vim['description'] = description + vim ['config'] = config + if sdn_controller or sdn_port_mapping: + ctx.obj.vim.create(name, vim, sdn_controller, sdn_port_mapping) + else: + ctx.obj.vim.create(name, vim) except ClientException as inst: print(inst.message) exit(1) @@ -850,9 +861,9 @@ def vim_create(ctx, @click.option('--tenant', help='VIM tenant name') @click.option('--config', help='VIM specific config parameters') @click.option('--account_type', help='VIM type') -@click.option('--sdn_controller', help='Name or id of the SDN controller associated to this VIM account') -@click.option('--sdn_port_mapping', default=None, help="File describing the port mapping between compute nodes' ports and switch ports") @click.option('--description', help='human readable description') +@click.option('--sdn_controller', default=None, help='Name or id of the SDN controller associated to this VIM account') +@click.option('--sdn_port_mapping', default=None, help="File describing the port mapping between compute nodes' ports and switch ports") @click.pass_context def vim_update(ctx, name, @@ -863,36 +874,25 @@ def vim_update(ctx, tenant, config, account_type, - description): + description, + sdn_controller, + sdn_port_mapping): '''updates a VIM account NAME: name or ID of the VIM account ''' - vim = {} - if newname: vim['name'] = newname - if user: vim['vim_user'] = user - if password: vim['vim_password'] = password - if auth_url: vim['vim_url'] = auth_url - if tenant: vim['vim-tenant-name'] = tenant - if account_type: vim['vim_type'] = account_type - if description: vim['description'] = description - config_dict = {} - if config is not None: - if config=="" and (sdncontroller or sdn_port_mapping): - raise ClientException("clearing config is incompatible with updating SDN info") - if config=="": - vim['config'] = None - else: - config_dict = yaml.safe_load(config) - if sdn_controller: config_dict['sdn_controller'] = sdn_controller - if sdn_port_mapping: - with open(sdn_port_mapping, 'r') as f: - config_dict['sdn_port_mapping'] = yaml.safe_load(f.read()) - if 'config' not in vim and config_dict: - vim['config'] = yaml.safe_dump(config_dict) try: check_client_version(ctx.obj, ctx.command.name) - ctx.obj.vim.update(name, vim) + vim = {} + if newname: vim['name'] = newname + if user: vim['vim_user'] = user + if password: vim['vim_password'] = password + if auth_url: vim['vim_url'] = auth_url + if tenant: vim['vim-tenant-name'] = tenant + if account_type: vim['vim_type'] = account_type + if description: vim['description'] = description + if config: vim['config'] = config + ctx.obj.vim.update(name, vim, sdn_controller, sdn_port_mapping) except ClientException as inst: print(inst.message) exit(1) diff --git a/osmclient/sol005/client.py b/osmclient/sol005/client.py index ca8f151..cec7436 100644 --- a/osmclient/sol005/client.py +++ b/osmclient/sol005/client.py @@ -58,9 +58,9 @@ class Client(object): if ro_host is None: ro_host = host - ro_http_client = http.Http('http://{}:{}/'.format(ro_host, ro_port)) + ro_http_client = http.Http('http://{}:{}/openmano'.format(ro_host, ro_port)) ro_http_client.set_http_header( - ['Accept: application/vnd.yand.data+json', + ['Accept: application/json', 'Content-Type: application/json']) self._http_client = http.Http( diff --git a/osmclient/sol005/http.py b/osmclient/sol005/http.py index 5311ca8..4debf0d 100644 --- a/osmclient/sol005/http.py +++ b/osmclient/sol005/http.py @@ -60,11 +60,10 @@ class Http(http.Http): data = BytesIO() curl_cmd = self._get_curl_cmd(endpoint) if put_method: - curl_cmd.setopt(pycurl.PUT, 1) + curl_cmd.setopt(pycurl.CUSTOMREQUEST, "PUT") elif patch_method: curl_cmd.setopt(pycurl.CUSTOMREQUEST, "PATCH") - else: - curl_cmd.setopt(pycurl.POST, 1) + curl_cmd.setopt(pycurl.POST, 1) curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write) if postfields_dict is not None: diff --git a/osmclient/sol005/sdncontroller.py b/osmclient/sol005/sdncontroller.py index 29b8f68..04b0775 100644 --- a/osmclient/sol005/sdncontroller.py +++ b/osmclient/sol005/sdncontroller.py @@ -48,7 +48,7 @@ class SdnController(object): def update(self, name, sdn_controller): sdnc = self.get(name) - http_code, resp = self._http.patch_cmd(endpoint='{}/{}'.format(self._apiBase,sdnc['_id']), + http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,sdnc['_id']), postfields_dict=sdn_controller) if resp: resp = json.loads(resp) diff --git a/osmclient/sol005/vim.py b/osmclient/sol005/vim.py index ec1ccad..9d10302 100644 --- a/osmclient/sol005/vim.py +++ b/osmclient/sol005/vim.py @@ -34,24 +34,32 @@ class Vim(object): self._apiResource = '/vims' self._apiBase = '{}{}{}'.format(self._apiName, self._apiVersion, self._apiResource) - def create(self, name, vim_access): + def create(self, name, vim_access, sdn_controller, sdn_port_mapping): if 'vim-type' not in vim_access: #'openstack' not in vim_access['vim-type']): raise Exception("vim type not provided") vim_account = {} - vim_config = {'hello': 'hello'} vim_account['name'] = name vim_account = self.update_vim_account_dict(vim_account, vim_access) vim_config = {} if 'config' in vim_access and vim_access['config'] is not None: - vim_config = yaml.load(vim_access['config']) - - vim_account['config'] = vim_config + vim_config = json.loads(vim_access['config']) + if sdn_controller: + sdnc = self._client.sdnc.get(sdn_controller) + vim_config['sdn-controller'] = sdnc['_id'] + if sdn_port_mapping: + with open(sdn_port_mapping, 'r') as f: + vim_config['sdn-port-mapping'] = yaml.safe_load(f.read()) + if vim_config: + vim_account['config'] = vim_config + #vim_account['config'] = json.dumps(vim_config) http_code, resp = self._http.post_cmd(endpoint=self._apiBase, postfields_dict=vim_account) + #print 'HTTP CODE: {}'.format(http_code) + #print 'RESP: {}'.format(resp) if resp: resp = json.loads(resp) if not resp or 'id' not in resp: @@ -60,14 +68,31 @@ class Vim(object): else: print resp['id'] - def update(self, vim_name, vim_account): + def update(self, vim_name, vim_account, sdn_controller, sdn_port_mapping): vim = self.get(vim_name) - #http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,vim['_id']), - http_code, resp = self._http.patch_cmd(endpoint='{}/{}'.format(self._apiBase,vim['_id']), + + vim_config = {} + if 'config' in vim_account: + if config=="" and (sdncontroller or sdn_port_mapping): + raise ClientException("clearing config is incompatible with updating SDN info") + if config=="": + vim_config = None + else: + vim_config = json.loads(vim_account['config']) + if sdn_controller: + sdnc = self._client.sdnc.get(sdn_controller) + vim_config['sdn-controller'] = sdnc['_id'] + if sdn_port_mapping: + with open(sdn_port_mapping, 'r') as f: + vim_config['sdn-port-mapping'] = yaml.safe_load(f.read()) + vim_account['config'] = vim_config + #vim_account['config'] = json.dumps(vim_config) + http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,vim['_id']), postfields_dict=vim_account) + #print 'HTTP CODE: {}'.format(http_code) + #print 'RESP: {}'.format(resp) if resp: resp = json.loads(resp) - #print 'RESP: {}'.format(resp) if not resp or 'id' not in resp: raise ClientException('failed to update vim: '.format(resp)) else: