X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osmclient%2Fsol005%2Fvim.py;h=1af7cc760933a5ce44c8bf9ce06e8bc337992911;hb=refs%2Fchanges%2F32%2F8232%2F19;hp=dc107effb103af60b445f7b5b041be5b883054d4;hpb=da765d5ec750a9de0e4123d60ff211ca86a2ad61;p=osm%2Fosmclient.git diff --git a/osmclient/sol005/vim.py b/osmclient/sol005/vim.py index dc107ef..1af7cc7 100644 --- a/osmclient/sol005/vim.py +++ b/osmclient/sol005/vim.py @@ -19,6 +19,7 @@ OSM vim API handling """ from osmclient.common import utils +from osmclient.common import wait as WaitForStatus from osmclient.common.exceptions import ClientException from osmclient.common.exceptions import NotFound import yaml @@ -34,7 +35,33 @@ class Vim(object): self._apiResource = '/vim_accounts' self._apiBase = '{}{}{}'.format(self._apiName, self._apiVersion, self._apiResource) - def create(self, name, vim_access, sdn_controller=None, sdn_port_mapping=None): + + # VIM '--wait' option + def _wait(self, id, deleteFlag=False): + self._client.get_token() + # Endpoint to get operation status + apiUrlStatus = '{}{}{}'.format(self._apiName, self._apiVersion, '/vim_accounts') + # Wait for status for VIM instance creation/deletion + WaitForStatus.wait_for_status( + 'VIM', + str(id), + WaitForStatus.TIMEOUT_VIM_OPERATION, + apiUrlStatus, + self._http.get2_cmd, + deleteFlag=deleteFlag) + + def _get_id_for_wait(self, name): + # Returns id of name, or the id itself if given as argument + for vim in self.list(): + if name == vim['uuid']: + return vim['uuid'] + for vim in self.list(): + if name == vim['name']: + return vim['uuid'] + return '' + + def create(self, name, vim_access, sdn_controller=None, sdn_port_mapping=None, wait=False): + self._client.get_token() if 'vim-type' not in vim_access: #'openstack' not in vim_access['vim-type']): raise Exception("vim type not provided") @@ -43,7 +70,7 @@ class Vim(object): vim_account['name'] = name vim_account = self.update_vim_account_dict(vim_account, vim_access) - vim_config = {'hello': 'hello'} + vim_config = {} if 'config' in vim_access and vim_access['config'] is not None: vim_config = yaml.safe_load(vim_access['config']) if sdn_controller: @@ -58,24 +85,36 @@ class Vim(object): 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: - raise ClientException('failed to create vim {}: {}'.format( - name, resp)) + #print('HTTP CODE: {}'.format(http_code)) + #print('RESP: {}'.format(resp)) + if http_code in (200, 201, 202, 204): + if resp: + resp = json.loads(resp) + if not resp or 'id' not in resp: + raise ClientException('unexpected response from server - {}'.format( + resp)) + if wait: + # Wait for status for VIM instance creation + self._wait(resp.get('id')) + print(resp['id']) else: - print resp['id'] + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException("failed to create vim {} - {}".format(name, msg)) - def update(self, vim_name, vim_account, sdn_controller, sdn_port_mapping): + def update(self, vim_name, vim_account, sdn_controller, sdn_port_mapping, wait=False): + self._client.get_token() vim = self.get(vim_name) - + vim_id_for_wait = self._get_id_for_wait(vim_name) vim_config = {} if 'config' in vim_account: - if config=="" and (sdncontroller or sdn_port_mapping): + if vim_account.get('config')=="" and (sdn_controller or sdn_port_mapping): raise ClientException("clearing config is incompatible with updating SDN info") - if config=="": + if vim_account.get('config')=="": vim_config = None else: vim_config = yaml.safe_load(vim_account['config']) @@ -87,16 +126,27 @@ class Vim(object): 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']), + http_code, resp = self._http.patch_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) - if not resp or 'id' not in resp: - raise ClientException('failed to update vim: '.format(resp)) + # print('HTTP CODE: {}'.format(http_code)) + # print('RESP: {}'.format(resp)) + if http_code in (200, 201, 202, 204): + if wait: + # In this case, 'resp' always returns None, so 'resp['id']' cannot be used. + # Use the previously obtained id instead. + wait_id = vim_id_for_wait + # Wait for status for VI instance update + self._wait(wait_id) + else: + pass else: - print resp['id'] + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException("failed to update vim {} - {}".format(vim_name, msg)) def update_vim_account_dict(self, vim_account, vim_access): vim_account['vim_type'] = vim_access['vim-type'] @@ -115,7 +165,8 @@ class Vim(object): return vim['uuid'] raise NotFound("vim {} not found".format(name)) - def delete(self, vim_name, force=False): + def delete(self, vim_name, force=False, wait=False): + self._client.get_token() vim_id = vim_name if not utils.validate_uuid4(vim_name): vim_id = self.get_id(vim_name) @@ -124,19 +175,35 @@ class Vim(object): querystring = '?FORCE=True' http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase, vim_id, querystring)) - if resp: - resp = json.loads(resp) - #print 'RESP: {}'.format(resp) + #print('HTTP CODE: {}'.format(http_code)) + #print('RESP: {}'.format(resp)) if http_code == 202: - print 'Deletion in progress' + if wait: + # When deleting an account, 'resp' may be None. + # In such a case, the 'id' from 'resp' cannot be used, so use 'vim_id' instead + wait_id = vim_id + if resp: + resp = json.loads(resp) + wait_id = resp.get('id') + # Wait for status for VIM account deletion + self._wait(wait_id, deleteFlag=True) + else: + print('Deletion in progress') elif http_code == 204: - print 'Deleted' + print('Deleted') else: - raise ClientException("failed to delete vim {} - {}".format(vim_name, resp)) + msg = "" + if resp: + try: + msg = json.loads(resp) + except ValueError: + msg = resp + raise ClientException("failed to delete vim {} - {}".format(vim_name, msg)) def list(self, filter=None): """Returns a list of VIM accounts """ + self._client.get_token() filter_string = '' if filter: filter_string = '?{}'.format(filter) @@ -152,6 +219,7 @@ class Vim(object): def get(self, name): """Returns a VIM account based on name or id """ + self._client.get_token() vim_id = name if not utils.validate_uuid4(name): vim_id = self.get_id(name)