Populate system info into cloud-extra-info 02/102502/7
authorBin Yang <bin.yang@windriver.com>
Thu, 27 Feb 2020 14:22:47 +0000 (22:22 +0800)
committerBin Yang <bin.yang@windriver.com>
Thu, 27 Feb 2020 16:41:29 +0000 (00:41 +0800)
Change-Id: I9480169439d340fedf5b01fe03d6a8e2f3db8fcb
Issue-ID: MULTICLOUD-830
Signed-off-by: Bin Yang <bin.yang@windriver.com>
share/common/msapi/extsys.py
share/common/msapi/helper.py
share/starlingx_base/registration/registration.py

index 88118aa..49a2d32 100644 (file)
@@ -46,13 +46,12 @@ def get_vim_by_id(vim_id):
                 status_code2, content2)
         tmp_authinfo = json.JSONDecoder().decode(content2)
 
-        # get the first auth info by default
-        tmp_authinfo = tmp_authinfo['esr-system-info'][0] if tmp_authinfo \
-                                                             and tmp_authinfo.get('esr-system-info', None) else None
-
         #convert vim information
-        if tmp_viminfo and tmp_authinfo:
+        if tmp_viminfo and tmp_authinfo and tmp_authinfo.get('esr-system-info'):
             viminfo = {}
+            # get the first auth info by default
+            tmp_authinfo = tmp_authinfo['esr-system-info'][0]
+
             viminfo['vimId'] = vim_id
             viminfo['resource-version'] = tmp_viminfo.get('resource-version')
             viminfo['cloud_owner'] = cloud_owner
@@ -62,16 +61,22 @@ def get_vim_by_id(vim_id):
             viminfo['version'] = tmp_viminfo.get('cloud-region-version')
             viminfo['cloud_extra_info'] = tmp_viminfo.get('cloud-extra-info')
 
-            viminfo['userName'] = tmp_authinfo['user-name']
-            viminfo['password'] = tmp_authinfo['password']
-            viminfo['domain'] = tmp_authinfo.get('cloud-domain')
-            viminfo['url'] = tmp_authinfo.get('service-url')
-            viminfo['tenant'] = tmp_authinfo.get('default-tenant')
-            viminfo['cacert'] = tmp_authinfo.get('ssl-cacert')
-            viminfo['insecure'] = tmp_authinfo.get('ssl-insecure')
+            viminfo['userName'] = tmp_authinfo.get('user-name', "")
+            viminfo['password'] = tmp_authinfo.get('password', "")
+            viminfo['domain'] = tmp_authinfo.get('cloud-domain', "")
+            viminfo['url'] = tmp_authinfo.get('service-url', "")
+            viminfo['tenant'] = tmp_authinfo.get('default-tenant', "")
+            viminfo['cacert'] = tmp_authinfo.get('ssl-cacert', "")
+            viminfo['insecure'] = tmp_authinfo.get('ssl-insecure', True)
             viminfo["complex-name"] = tmp_viminfo.get("complex-name")
-            viminfo['openstack_region_id'] = tmp_viminfo.get("cloud-epa-caps") \
-                if tmp_viminfo.get("cloud-epa-caps") else cloud_region_id
+            # move the openstack region id store location, but keep backward compatibility
+            viminfo['openstack_region_id'] = tmp_authinfo.get("openstack-region-id") \
+                or tmp_viminfo.get("cloud-epa-caps", cloud_region_id)
+            try:
+                viminfo['cloud_extra_info_json'] = json.loads(
+                    viminfo.get('cloud_extra_info', {}))
+            except Exception:
+                pass
 
             return viminfo
     return None
index 7cfe77b..41a9e5a 100644 (file)
@@ -90,8 +90,8 @@ class MultiCloudAAIHelper(object):
         # identity service should not filtered by region since it is might be first call
         # to figure out available region list
         if service_type != 'identity':
-            service['region_name'] = viminfo['openstack_region_id']\
-                if viminfo.get('openstack_region_id') else viminfo['cloud_region_id']
+            service['region_name'] = viminfo.get('openstack_region_id') \
+                or viminfo['cloud_region_id']
 
         self._logger.debug("making request with URI:%s,%s" % (resource_url, service))
         resp = session.get(resource_url, endpoint_filter=service)
index c45db66..73c9be1 100644 (file)
@@ -161,20 +161,19 @@ class RegistryHelper(newton_registration.RegistryHelper):
                 "Cloud Region not found in AAI: %s" % vimid
             )
 
-        cloud_extra_info_str = viminfo['cloud_extra_info']
-        cloud_extra_info = None
-        try:
-            cloud_extra_info = json.loads(cloud_extra_info_str) \
-                if cloud_extra_info_str else None
-        except Exception as ex:
-            logger.error("Can not convert cloud extra info %s %s" % (
-                str(ex), cloud_extra_info_str))
-            pass
-
-        region_specified = cloud_extra_info.get(
-            "openstack-region-id", None) if cloud_extra_info else None
+        # cloud_extra_info_str = viminfo.get('cloud_extra_info', {})
+        # cloud_extra_info = {}
+        # try:
+        #     cloud_extra_info = json.loads(cloud_extra_info_str)
+        # except Exception as ex:
+        #     logger.error("Can not convert cloud extra info %s %s" % (
+        #         str(ex), cloud_extra_info_str))
+        #     pass
+        cloud_extra_info = viminfo.get("cloud_extra_info_json",{})
+
+        region_specified = cloud_extra_info.get("openstack-region-id", None)
         multi_region_discovery = cloud_extra_info.get(
-            "multi-region-discovery", None) if cloud_extra_info else None
+            "multi-region-discovery", None)
 
         sess = None
         if project_idorname:
@@ -238,7 +237,7 @@ class RegistryHelper(newton_registration.RegistryHelper):
 
                 try:
                     self._update_cloud_region(
-                        cloud_owner, gen_cloud_region_id, regionid, viminfo)
+                        cloud_owner, gen_cloud_region_id, regionid, viminfo, sess)
                 except Exception as e:
                     self._logger.debug("update cloud region fails %s" % str(e))
 
@@ -252,14 +251,15 @@ class RegistryHelper(newton_registration.RegistryHelper):
         # update the specified region
         try:
             self._update_cloud_region(cloud_owner, cloud_region_id,
-                                      region_specified, viminfo)
+                                      region_specified, viminfo, sess)
+            #re-fetch viminfo
+            viminfo = VimDriverUtils.get_vim_info(vimid)
         except Exception as e:
             self._logger.debug("update cloud region fails %s" % str(e))
 
         # update k8s connectivity
         try:
-            self._update_k8s_info(cloud_owner, cloud_region_id, viminfo,
-                cloud_extra_info, sess)
+            self._update_k8s_info(cloud_owner, cloud_region_id, viminfo, sess)
         except Exception as e:
             self.__logger.debug("update k8s info failes %s" % str(e))
             # continue the registration without reporting error
@@ -313,9 +313,20 @@ class RegistryHelper(newton_registration.RegistryHelper):
                     "cloud_region_id": cloud_region_id
                 })
 
-            # Note1: The intent is to populate the openstack region id into property: cloud-region.esr-system-info.openstackRegionId
-            # Note2: As temp solution: the openstack region id was put into AAI cloud-region["cloud-epa-caps"]
 
+            # update cloud_extra_info
+            oldregionid = viminfo.get('openstack_region_id')
+            # update openstack region id so the call will go the specified reigon
+            viminfo['openstack_region_id'] = openstack_region_id
+            cloud_extra_info = viminfo.get("cloud_extra_info_json", {})
+            system_info = self._get_system_info(cloud_owner, cloud_region_id, viminfo, session)
+            if system_info:
+                cloud_extra_info["isystem"] = system_info
+            # recover the openstack region id of viminfo
+            viminfo['openstack_region_id'] = oldregionid
+
+            # Note1: populate the openstack region id into property
+            # : cloud-region.esr-system-info.openstack-region-id
             resource_info = {
                 "cloud-owner": cloud_owner,
                 "cloud-region-id": cloud_region_id,
@@ -326,7 +337,7 @@ class RegistryHelper(newton_registration.RegistryHelper):
                     if self.proxy_prefix[-3:] == "/v0" else
                     self.proxy_prefix + "/%s/%s/identity/v2.0" % (cloud_owner, cloud_region_id),
                 "complex-name": viminfo["complex-name"],
-                "cloud-extra-info": viminfo["cloud_extra_info"],
+                "cloud-extra-info": json.dumps(cloud_extra_info),
                 "cloud-epa-caps": openstack_region_id,
                 "esr-system-info-list": {
                     "esr-system-info": [
@@ -339,8 +350,8 @@ class RegistryHelper(newton_registration.RegistryHelper):
                             "ssl-cacert": viminfo["cacert"],
                             "ssl-insecure": viminfo["insecure"],
                             "cloud-domain": viminfo["domain"],
-                            "default-tenant": viminfo["tenant"]
-
+                            "default-tenant": viminfo["tenant"],
+                            "openstack-region-id": openstack_region_id
                         }
                     ]
                 }
@@ -432,16 +443,41 @@ class RegistryHelper(newton_registration.RegistryHelper):
             return []
 
 
-    def _update_k8s_info(self, cloud_owner, cloud_region_id,
-        viminfo, cloud_extra_info, session=None):
+    def _get_system_info(self, cloud_owner, cloud_region_id,  viminfo, session=None):
+        '''
+        query and populate system info into cloud_extra_info
+        '''
+        system_info = {}
         try:
             vimid = extsys.encode_vim_id(cloud_owner, cloud_region_id)
 
             # check system version of starlingx
-            systeminfo = self._get_list_resources(
+            isysteminfo = self._get_list_resources(
                 "/isystems", "platform", session, viminfo, vimid,
                 "isystems")
-            systemversion = systeminfo[0].get("software_version", None) if systeminfo else None
+            system_info['software_version'] = isysteminfo[0].get("software_version", None) if isysteminfo else None
+            system_info['system_mode'] = isysteminfo[0].get("system_mode", None) if isysteminfo else None
+            system_info['system_type'] = isysteminfo[0].get("system_type", None) if isysteminfo else None
+            return system_info
+        except HttpError as e:
+            self._logger.error("HttpError: status:%s, response:%s"
+                               % (e.http_status, e.response.json()))
+        except Exception:
+            self._logger.error(traceback.format_exc())
+
+        return None
+
+
+    def _update_k8s_info(self, cloud_owner, cloud_region_id,
+        viminfo, session=None):
+        try:
+            cloud_extra_info = viminfo.get("cloud_extra_info_json",{})
+
+            vimid = extsys.encode_vim_id(cloud_owner, cloud_region_id)
+
+            # check system version of starlingx
+            system_info = cloud_extra_info.get("isystem", {})
+            systemversion = system_info.get("software_version", None)
             if not systemversion:
                 self._logger.warn("query system version fails")
                 return
@@ -460,21 +496,14 @@ class RegistryHelper(newton_registration.RegistryHelper):
                 return
 
             # check if user token provided to access k8s platform
-            k8s_apitoken = cloud_extra_info.get(
-                "k8s-apitoken", None) if cloud_extra_info else None
-            k8s_apiserver = cloud_extra_info.get(
-                "k8s-apiserver", None) if cloud_extra_info else None
-            if not k8s_apitoken:
-                self._logger.warn("k8s-apitoken is not provided,"\
-                    "k8s connectivity must be provisioned in other ways")
-                return
-
-            if not k8s_apiserver:
-                self._logger.warn("k8s-apiserver is not provided,"\
+            k8s_apitoken = cloud_extra_info.get("k8s-apitoken", None)
+            k8s_apiserver = cloud_extra_info.get("k8s-apiserver", None)
+            if not k8s_apitoken or not k8s_apiserver:
+                self._logger.warn("k8s-apitoken or k8s-apiserver is not provided,"\
                     "k8s connectivity must be provisioned in other ways")
                 return
 
-            # now create kube config
+            # now create kube config with following template
             kubecfgdata = {
                 "apiVersion": "v1",
                 "clusters":