Improved PDU management
[osm/osmclient.git] / osmclient / sol005 / pdud.py
index df5bad1..e47dadd 100644 (file)
@@ -37,6 +37,12 @@ class Pdu(object):
             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):
         self._logger.debug("")
         self._client.get_token()
@@ -101,6 +107,14 @@ class Pdu(object):
 
     def create(self, pdu, update_endpoint=None):
         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"
@@ -109,7 +123,7 @@ class Pdu(object):
         ]
         self._http.set_http_header(http_header)
         if update_endpoint:
-            http_code, resp = self._http.put_cmd(
+            http_code, resp = self._http.patch_cmd(
                 endpoint=update_endpoint, postfields_dict=pdu
             )
         else:
@@ -118,25 +132,19 @@ class Pdu(object):
             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):
-        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 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"])
+        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)
+        self.create(pdu=pdu, update_endpoint=endpoint)