update vim config
[osm/osmclient.git] / osmclient / v1 / vim.py
index 334e89e..dd6a5e0 100644 (file)
@@ -20,7 +20,8 @@ OSM vim API handling
 
 from osmclient.common.exceptions import ClientException
 from osmclient.common.exceptions import NotFound
-import json
+import yaml 
+import time
 
 
 class Vim(object):
@@ -44,8 +45,12 @@ class Vim(object):
                                               datacenter['uuid']), vim_account)
 
     def _detach(self, vim_name):
+        tenant_name = 'osm'
+        tenant = self._get_ro_tenant()
+        if tenant is None:
+            raise ClientException("tenant {} not found".format(tenant_name))
         return self._ro_http.delete_cmd('openmano/{}/datacenters/{}'
-                                        .format('osm', vim_name))
+                                        .format(tenant["uuid"], vim_name))
 
     def create(self, name, vim_access):
         vim_account = {}
@@ -60,17 +65,8 @@ class Vim(object):
         vim_account['datacenter']['type'] = vim_access['vim-type']
 
         vim_config = {}
-        vim_config['use_floating_ip'] = False
-
-        if ('floating_ip_pool' in vim_access and
-           vim_access['floating_ip_pool'] is not None):
-            vim_config['use_floating_ip'] = True
-
-        if 'keypair' in vim_access and vim_access['keypair'] is not None:
-            vim_config['keypair'] = vim_access['keypair']
-        elif 'config' in vim_access and vim_access['config'] is not None:
-            if any(var in vim_access['config'] for var in ["admin_password","admin_username","orgname","nsx_user","nsx_password","nsx_manager","vcenter_ip","vcenter_port","vcenter_user","vcenter_password"]):
-                vim_config = json.loads(vim_access['config'])
+        if 'config' in vim_access and vim_access['config'] is not None:
+           vim_config = yaml.load(vim_access['config'])
 
         vim_account['datacenter']['config'] = vim_config
 
@@ -81,6 +77,27 @@ class Vim(object):
             raise ClientException("failed to create vim")
         else:
             self._attach(name, vim_account)
+            self._update_ro_accounts()
+
+
+    def _update_ro_accounts(self):
+        get_ro_accounts = self._http.get_cmd('api/operational/{}ro-account'
+                    .format(self._client.so_rbac_project_path))
+        if not get_ro_accounts or 'rw-ro-account:ro-account' not in get_ro_accounts:
+            return
+        for account in get_ro_accounts['rw-ro-account:ro-account']['account']:
+            if account['ro-account-type'] == 'openmano':
+                # Refresh the Account Status
+                refresh_body = {"input": {
+                                            "ro-account": account['name'], 
+                                            "project-name": self._client._so_project
+                                        }
+                                }
+                refresh_status = self._http.post_cmd('api/operations/update-ro-account-status',
+                    refresh_body)
+                if refresh_status and 'error' in refresh_status:
+                    raise ClientException("Failed to refersh RO Account Status")
+    
 
     def update_vim_account_dict(self, vim_account, vim_access, vim_config):
         if vim_access['vim-type'] == 'vmware':
@@ -124,12 +141,19 @@ class Vim(object):
         self._detach(vim_name)
         # detach.  continue if error,
         # it could be the datacenter is left without attachment
+        resp = self._ro_http.delete_cmd('openmano/datacenters/{}'
+                                                    .format(vim_name))
+        if 'result' not in resp:
+            raise ClientException("failed to delete vim {} - {}".format(vim_name, resp))
+        self._update_ro_accounts()
+
+    def list(self, ro_update):
+        if ro_update:
+            self._update_ro_accounts()
+            # the ro_update needs to be made synchronous, for now this works around the issue
+            # and waits a resonable amount of time for the update to finish
+            time.sleep(2)
 
-        if 'result' not in self._ro_http.delete_cmd('openmano/datacenters/{}'
-                                                    .format(vim_name)):
-            raise ClientException("failed to delete vim {}".format(vim_name))
-
-    def list(self):
         if self._client._so_version == 'v3':
             resp = self._http.get_cmd('v1/api/operational/{}ro-account-state'
                     .format(self._client.so_rbac_project_path))
@@ -139,6 +163,10 @@ class Vim(object):
 
             ro_accounts = resp['rw-ro-account:ro-account-state']
             for ro_account in ro_accounts['account']:
+                if 'datacenters' not in ro_account:
+                    continue
+                if 'datacenters' not in ro_account['datacenters']:
+                    continue
                 for datacenter in ro_account['datacenters']['datacenters']:
                     datacenters.append({"name": datacenter['name'], "uuid": datacenter['uuid']
                         if 'uuid' in datacenter else None}) 
@@ -210,6 +238,10 @@ class Vim(object):
 
             ro_accounts = resp['rw-ro-account:ro-account-state']
             for ro_account in ro_accounts['account']:
+                if 'datacenters' not in ro_account:
+                    continue
+                if 'datacenters' not in ro_account['datacenters']:
+                    continue
                 for datacenter in ro_account['datacenters']['datacenters']:
                     if datacenter['name'] == name:
                         return datacenter, ro_account['name']