X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osmclient%2Fsol005%2Fpdud.py;h=e47dadde02122ae42875c0282828d72aa8b83d52;hb=fcfed2f9c065ba92f775fb6f4563d5d4b64e7797;hp=b59b91dd048f4df41a9a5d522a65320ba06e9767;hpb=2cc451122a28672aa0b928688fc76d633d5ece81;p=osm%2Fosmclient.git diff --git a/osmclient/sol005/pdud.py b/osmclient/sol005/pdud.py index b59b91d..e47dadd 100644 --- a/osmclient/sol005/pdud.py +++ b/osmclient/sol005/pdud.py @@ -22,106 +22,129 @@ from osmclient.common.exceptions import NotFound from osmclient.common.exceptions import ClientException from osmclient.common import utils import json +import logging class Pdu(object): - def __init__(self, http=None, client=None): self._http = http self._client = client - self._apiName = '/pdu' - self._apiVersion = '/v1' - self._apiResource = '/pdu_descriptors' - self._apiBase = '{}{}{}'.format(self._apiName, - self._apiVersion, self._apiResource) + self._logger = logging.getLogger("osmclient") + self._apiName = "/pdu" + self._apiVersion = "/v1" + self._apiResource = "/pdu_descriptors" + self._apiBase = "{}{}{}".format( + self._apiName, self._apiVersion, self._apiResource + ) + + def _get_vim_account(self, vim_account): + vim = self._client.vim.get(vim_account) + if vim is None: + raise NotFound("cannot find vim account '{}'".format(vim_account)) + return vim def list(self, filter=None): - filter_string = '' + 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)) + filter_string = "?{}".format(filter) + _, resp = self._http.get2_cmd("{}{}".format(self._apiBase, filter_string)) if resp: - return resp + return json.loads(resp) return list() def get(self, name): + self._logger.debug("") + self._client.get_token() if utils.validate_uuid4(name): for pdud in self.list(): - if name == pdud['_id']: + if name == pdud["_id"]: return pdud else: for pdud in self.list(): - if 'name' in pdud and name == pdud['name']: + if "name" in pdud and name == pdud["name"]: return pdud raise NotFound("pdud {} not found".format(name)) def get_individual(self, name): + self._logger.debug("") pdud = self.get(name) # It is redundant, since the previous one already gets the whole pdudInfo # The only difference is that a different primitive is exercised - resp = self._http.get_cmd('{}/{}'.format(self._apiBase, pdud['_id'])) - #print yaml.safe_dump(resp) + try: + _, resp = self._http.get2_cmd("{}/{}".format(self._apiBase, pdud["_id"])) + except NotFound: + raise NotFound("pdu '{}' not found".format(name)) + # print(yaml.safe_dump(resp)) if resp: - return resp - raise NotFound("pdu {} not found".format(name)) + return json.loads(resp) + raise NotFound("pdu '{}' not found".format(name)) def delete(self, name, force=False): + self._logger.debug("") pdud = self.get(name) - querystring = '' + querystring = "" if force: - querystring = '?FORCE=True' - http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase, - pdud['_id'], querystring)) - #print 'HTTP CODE: {}'.format(http_code) - #print 'RESP: {}'.format(resp) + querystring = "?FORCE=True" + http_code, resp = self._http.delete_cmd( + "{}/{}{}".format(self._apiBase, pdud["_id"], querystring) + ) + # print('HTTP CODE: {}'.format(http_code)) + # print('RESP: {}'.format(resp)) if http_code == 202: - print('Deletion in progress') + print("Deletion in progress") elif http_code == 204: - print('Deleted') + 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 pdu {} - {}".format(name, msg)) def create(self, pdu, update_endpoint=None): - headers= self._client._headers - headers['Content-Type'] = 'text/plain' - http_header = ['{}: {}'.format(key,val) - for (key,val) in list(headers.items())] + self._logger.debug("") + + if pdu["vim_accounts"]: + vim_account_list = [] + for vim_account in pdu["vim_accounts"]: + vim = self._get_vim_account(vim_account) + vim_account_list.append(vim["_id"]) + pdu["vim_accounts"] = vim_account_list + + self._client.get_token() + headers = self._client._headers + headers["Content-Type"] = "application/yaml" + http_header = [ + "{}: {}".format(key, val) for (key, val) in list(headers.items()) + ] self._http.set_http_header(http_header) if update_endpoint: - http_code, resp = self._http.put_cmd(endpoint=update_endpoint, postfields_dict=pdu) + http_code, resp = self._http.patch_cmd( + endpoint=update_endpoint, postfields_dict=pdu + ) else: - self._apiResource = '/pdu_descriptors_content' - self._apiBase = '{}{}{}'.format(self._apiName, - self._apiVersion, self._apiResource) endpoint = self._apiBase - #endpoint = '{}{}'.format(self._apiBase,ow_string) - http_code, resp = self._http.post_cmd(endpoint=endpoint, postfields_dict=pdu) - #print 'HTTP CODE: {}'.format(http_code) - #print 'RESP: {}'.format(resp) - if http_code in (200, 201, 202, 204): + # endpoint = '{}{}'.format(self._apiBase,ow_string) + http_code, resp = self._http.post_cmd( + endpoint=endpoint, postfields_dict=pdu + ) + if http_code in (200, 201, 202): if resp: resp = json.loads(resp) - if not resp or 'id' not in resp: - raise ClientException('unexpected response from server: '.format( - resp)) - print(resp['id']) - else: - msg = "Error {}".format(http_code) - if resp: - try: - msg = "{} - {}".format(msg, json.loads(resp)) - except ValueError: - msg = "{} - {}".format(msg, resp) - raise ClientException("failed to create/update pdu - {}".format(msg)) + if not resp or "id" not in resp: + raise ClientException( + "unexpected response from server: {}".format(resp) + ) + print(resp["id"]) + elif http_code == 204: + print("Updated") - def update(self, name, filename): + def update(self, name, pdu): + self._logger.debug("") pdud = self.get(name) - endpoint = '{}/{}'.format(self._apiBase, pdud['_id']) - self.create(filename=filename, update_endpoint=endpoint) - + endpoint = "{}/{}".format(self._apiBase, pdud["_id"]) + self.create(pdu=pdu, update_endpoint=endpoint)