X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osmclient%2Fsol005%2Fwim.py;h=0ba8ab4279bfc1effb8d6908a64d61abc4dcb92b;hb=refs%2Fchanges%2F38%2F8738%2F9;hp=943ceb4049a96ffcecc9f6a52bd66ae457e7b524;hpb=0eefdebca0e3eefaf207bc22bebf5128a7b7717a;p=osm%2Fosmclient.git diff --git a/osmclient/sol005/wim.py b/osmclient/sol005/wim.py index 943ceb4..0ba8ab4 100644 --- a/osmclient/sol005/wim.py +++ b/osmclient/sol005/wim.py @@ -24,12 +24,14 @@ from osmclient.common.exceptions import ClientException from osmclient.common.exceptions import NotFound import yaml import json +import logging class Wim(object): def __init__(self, http=None, client=None): self._http = http self._client = client + self._logger = logging.getLogger('osmclient') self._apiName = '/admin' self._apiVersion = '/v1' self._apiResource = '/wim_accounts' @@ -37,21 +39,26 @@ class Wim(object): self._apiVersion, self._apiResource) # WIM '--wait' option - def _wait(self, id, deleteFlag=False): + def _wait(self, id, wait_time, deleteFlag=False): + self._logger.debug("") self._client.get_token() # Endpoint to get operation status apiUrlStatus = '{}{}{}'.format(self._apiName, self._apiVersion, '/wim_accounts') # Wait for status for WIM instance creation/deletion + if isinstance(wait_time, bool): + wait_time = WaitForStatus.TIMEOUT_WIM_OPERATION WaitForStatus.wait_for_status( 'WIM', str(id), - WaitForStatus.TIMEOUT_WIM_OPERATION, + wait_time, 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 + """Returns id of name, or the id itself if given as argument + """ + self._logger.debug("") for wim in self.list(): if name == wim['uuid']: return wim['uuid'] @@ -61,6 +68,7 @@ class Wim(object): return '' def create(self, name, wim_input, wim_port_mapping=None, wait=False): + self._logger.debug("") self._client.get_token() if 'wim_type' not in wim_input: raise Exception("wim type not provided") @@ -82,26 +90,27 @@ class Wim(object): postfields_dict=wim_account) #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 WIM instance creation - self._wait(resp.get('id')) - print(resp['id']) - else: - msg = "" - if resp: - try: - msg = json.loads(resp) - except ValueError: - msg = resp - raise ClientException("failed to create wim {} - {}".format(name, msg)) + #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 WIM instance creation + self._wait(resp.get('id'), wait) + print(resp['id']) + #else: + # msg = "" + # if resp: + # try: + # msg = json.loads(resp) + # except ValueError: + # msg = resp + # raise ClientException("failed to create wim {} - {}".format(name, msg)) def update(self, wim_name, wim_account, wim_port_mapping=None, wait=False): + self._logger.debug("") self._client.get_token() wim = self.get(wim_name) wim_id_for_wait = self._get_id_for_wait(wim_name) @@ -122,26 +131,27 @@ class Wim(object): postfields_dict=wim_account) #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 = wim_id_for_wait - # Wait for status for WIM instance update - self._wait(wait_id) - else: - pass - else: - msg = "" - if resp: - try: - msg = json.loads(resp) - except ValueError: - msg = resp - raise ClientException("failed to update wim {} - {}".format(wim_name, msg)) + #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 = wim_id_for_wait + # Wait for status for WIM instance update + self._wait(wait_id, wait) + # else: + # pass + #else: + # msg = "" + # if resp: + # try: + # msg = json.loads(resp) + # except ValueError: + # msg = resp + # raise ClientException("failed to update wim {} - {}".format(wim_name, msg)) def update_wim_account_dict(self, wim_account, wim_input): - print(wim_input) + self._logger.debug("") + self._logger.debug(str(wim_input)) wim_account['wim_type'] = wim_input['wim_type'] wim_account['description'] = wim_input['description'] wim_account['wim_url'] = wim_input['url'] @@ -152,12 +162,14 @@ class Wim(object): def get_id(self, name): """Returns a WIM id from a WIM name """ + self._logger.debug("") for wim in self.list(): if name == wim['name']: return wim['uuid'] raise NotFound("wim {} not found".format(name)) def delete(self, wim_name, force=False, wait=False): + self._logger.debug("") self._client.get_token() wim_id = wim_name wim_id_for_wait = self._get_id_for_wait(wim_name) @@ -180,32 +192,33 @@ class Wim(object): resp = json.loads(resp) wait_id = resp.get('id') # Wait for status for WIM account deletion - self._wait(wait_id, deleteFlag=True) + self._wait(wait_id, wait, deleteFlag=True) else: print('Deletion in progress') elif http_code == 204: print('Deleted') else: - msg = "" - if resp: - try: - msg = json.loads(resp) - except ValueError: - msg = resp + msg = resp or "" + # if resp: + # try: + # msg = json.loads(resp) + # except ValueError: + # msg = resp raise ClientException("failed to delete wim {} - {}".format(wim_name, msg)) def list(self, filter=None): """Returns a list of VIM accounts """ + self._logger.debug("") self._client.get_token() filter_string = '' if filter: filter_string = '?{}'.format(filter) - resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string)) + _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string)) if not resp: return list() wim_accounts = [] - for datacenter in resp: + for datacenter in json.loads(resp): wim_accounts.append({"name": datacenter['name'], "uuid": datacenter['_id'] if '_id' in datacenter else None}) return wim_accounts @@ -213,14 +226,18 @@ class Wim(object): def get(self, name): """Returns a VIM account based on name or id """ + self._logger.debug("") self._client.get_token() wim_id = name if not utils.validate_uuid4(name): wim_id = self.get_id(name) - resp = self._http.get_cmd('{}/{}'.format(self._apiBase,wim_id)) - if not resp or '_id' not in resp: - raise ClientException('failed to get wim info: '.format( - resp)) - else: + try: + _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase,wim_id)) + if resp: + resp = json.loads(resp) + if not resp or '_id' not in resp: + raise ClientException('failed to get wim info: '.format(resp)) return resp - raise NotFound("wim {} not found".format(name)) + except NotFound: + raise NotFound("wim '{}' not found".format(name)) +