From 34fe8cbca786d59ccc2550a1f20b4b7d20b3aae7 Mon Sep 17 00:00:00 2001 From: Xiaohua Zhang Date: Mon, 8 Apr 2019 07:21:30 +0000 Subject: Update API handler for multi-tenant supports Change-Id: I5c46a71605ed9f5f9e4dfd04b0383c1d5874f3da Issue-ID: MULTICLOUD-543 Signed-off-by: Xiaohua Zhang --- share/starlingx_base/registration/registration.py | 73 +++++++++++++++++++---- share/starlingx_base/resource/capacity.py | 24 ++++---- share/starlingx_base/resource/infra_workload.py | 1 + 3 files changed, 76 insertions(+), 22 deletions(-) (limited to 'share/starlingx_base') diff --git a/share/starlingx_base/registration/registration.py b/share/starlingx_base/registration/registration.py index 21ab1948..507f0fbf 100644 --- a/share/starlingx_base/registration/registration.py +++ b/share/starlingx_base/registration/registration.py @@ -47,6 +47,9 @@ class APIv0Registry(newton_registration.Registry): def post(self, request, vimid=""): self._logger.info("registration with : %s" % vimid) + # Get the specified tenant id + specified_project_idorname = request.META.get("Project", None) + # vim registration will trigger the start the audit of AZ capacity worker_self = InfraResourceAuditor( settings.MULTICLOUD_API_V1_PREFIX, @@ -55,7 +58,7 @@ class APIv0Registry(newton_registration.Registry): backlog_item = { "id": vimid, "worker": worker_self.azcap_audit, - "payload": (worker_self, vimid), + "payload": (worker_self, vimid, specified_project_idorname), "repeat": 10*1000000, # repeat every 10 seconds } gAZCapAuditThread.add(backlog_item) @@ -132,11 +135,18 @@ class RegistryHelper(newton_registration.RegistryHelper): super(RegistryHelper, self).__init__(multicloud_prefix, aai_base_url) # self._logger = logger - def registryV0(self, vimid=""): + def registryV0(self, vimid="", project_idorname=None): ''' extend base method ''' viminfo = VimDriverUtils.get_vim_info(vimid) + + if not viminfo: + return ( + 10, + "Cloud Region not found in AAI: %s" % vimid + ) + cloud_extra_info_str = viminfo['cloud_extra_info'] cloud_extra_info = None try: @@ -152,9 +162,30 @@ class RegistryHelper(newton_registration.RegistryHelper): multi_region_discovery = cloud_extra_info.get( "multi-region-discovery", None) if cloud_extra_info else None - # set the default tenant since there is no tenant info in the VIM yet - sess = VimDriverUtils.get_session( - viminfo, tenant_name=viminfo['tenant']) + if project_idorname: + try: + # check if specified with tenant id + sess = VimDriverUtils.get_session( + viminfo, tenant_name=None, + tenant_id=project_idorname + ) + except Exception as e: + pass + + if not sess: + try: + # check if specified with tenant name + sess = VimDriverUtils.get_session( + viminfo, tenant_name=project_idorname, + tenant_id=None + ) + except Exception as e: + pass + + if not sess: + # set the default tenant since there is no tenant info in the VIM yet + sess = VimDriverUtils.get_session( + viminfo, tenant_name=viminfo.get('tenant', None)) # discover the regions, expect it always returns a list (even empty list) cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid) @@ -205,7 +236,6 @@ class RegistryHelper(newton_registration.RegistryHelper): return 0 - def unregistry(self, vimid=""): '''extend base method''' @@ -367,16 +397,37 @@ class InfraResourceAuditor(newton_registration.RegistryHelper): self._logger = logger # super(InfraResourceAuditor, self).__init__(); - def azcap_audit(self, vimid): + def azcap_audit(self, vimid, project_idorname=None): viminfo = VimDriverUtils.get_vim_info(vimid) if not viminfo: self._logger.warn("azcap_audit no valid vimid: %s" % vimid) return - session = VimDriverUtils.get_session( - viminfo, - tenant_name=viminfo['tenant'] - ) + if project_idorname: + try: + # check if specified with tenant id + sess = VimDriverUtils.get_session( + viminfo, tenant_name=None, + tenant_id=project_idorname + ) + except Exception as e: + pass + + if not sess: + try: + # check if specified with tenant name + sess = VimDriverUtils.get_session( + viminfo, tenant_name=project_idorname, + tenant_id=None + ) + except Exception as e: + pass + + if not sess: + session = VimDriverUtils.get_session( + viminfo, + tenant_name=viminfo['tenant'] + ) # now retrieve the latest AZ cap info try: diff --git a/share/starlingx_base/resource/capacity.py b/share/starlingx_base/resource/capacity.py index 46e5eba5..cbdedaa3 100644 --- a/share/starlingx_base/resource/capacity.py +++ b/share/starlingx_base/resource/capacity.py @@ -41,7 +41,10 @@ class CapacityCheck(newton_capacity.CapacityCheck): self._logger.debug("META> %s" % request.META) try: - hasEnoughResource = self.get_tenant_cap_info(vimid, request.data) + + # Get the specified tenant id + specified_project_idorname = request.META.get("Project", None) + hasEnoughResource = self.get_tenant_cap_info(vimid, request.data, specified_project_idorname) azCapInfo = self.get_az_cap_info(vimid) self._logger.info("RESP with data> result:%s" % hasEnoughResource) return Response(data={'result': hasEnoughResource, 'AZs': azCapInfo}, status=status.HTTP_200_OK) @@ -52,15 +55,14 @@ class CapacityCheck(newton_capacity.CapacityCheck): def get_az_cap_info(self, vimid): azCapInfo = [] - viminfo = VimDriverUtils.get_vim_info(vimid) - if not viminfo: - self._logger.warn("azcap_audit no valid vimid: %s" % vimid) - return - - session = VimDriverUtils.get_session( - viminfo, - tenant_name=viminfo['tenant'] - ) + # viminfo = VimDriverUtils.get_vim_info(vimid) + # if not viminfo: + # self._logger.warn("azcap_audit no valid vimid: %s" % vimid) + # return + # session = VimDriverUtils.get_session( + # viminfo, + # tenant_name=viminfo.get('tenant', None) + # ) try: # get list of AZ vimAzCacheKey = "cap_azlist_" + vimid @@ -87,7 +89,7 @@ class CapacityCheck(newton_capacity.CapacityCheck): return azCapInfoList except Exception as e: return azCapInfo - pass + class APIv1CapacityCheck(CapacityCheck): def __init__(self): diff --git a/share/starlingx_base/resource/infra_workload.py b/share/starlingx_base/resource/infra_workload.py index 745e9f03..6b064856 100644 --- a/share/starlingx_base/resource/infra_workload.py +++ b/share/starlingx_base/resource/infra_workload.py @@ -468,6 +468,7 @@ class InfraWorkloadHelper(infra_workload_helper.InfraWorkloadHelper): The template for workload will be fetched from sdc client :param vimid: :param workload_data: + :param project_idorname: tenant id or name :return: result code, status enum, status reason result code: 0-ok, otherwise error status enum: "CREATE_IN_PROGRESS", "CREATE_FAILED" -- cgit 1.2.3-korg