Exemplo n.º 1
0
    def get(self, request, vimid="", servicetype="identity", requri='projects'):
        self._logger.info("vimid, servicetype, requri> %s,%s,%s"
                     % (vimid, servicetype, requri))
        self._logger.debug("META, data> %s , %s" % (request.META, request.data))

        tmp_auth_token = request.META.get('HTTP_X_AUTH_TOKEN', None)

        resp = super(GetTenants,self).get(request, vimid, servicetype, requri)
        if resp.status_code == status.HTTP_200_OK:
            content =  resp.data
            return Response(headers={'X-Subject-Token': tmp_auth_token}, data={'tenants': content['projects'],'tenants_links':[]},
                            status=resp.status_code)
        else:
            # Get the specified tenant id
            specified_project_idorname = request.META.get("Project", None)

            viminfo = VimDriverUtils.get_vim_info(vimid)
            session = None
            if specified_project_idorname:
                try:
                    # check if specified with tenant id
                    session = VimDriverUtils.get_session(
                        viminfo, tenant_name=None,
                        tenant_id=specified_project_idorname
                    )
                except Exception as e:
                    pass

                if not sess:
                    try:
                        # check if specified with tenant name
                        session = VimDriverUtils.get_session(
                            viminfo, tenant_name=specified_project_idorname,
                            tenant_id=None
                        )
                    except Exception as e:
                        pass

            if not session:
                session = VimDriverUtils.get_session(
                    viminfo, tenant_name=viminfo['tenant'])
            tmp_auth_state = VimDriverUtils.get_auth_state(session)
            tmp_auth_info = json.loads(tmp_auth_state)
            tmp_auth_data = tmp_auth_info['body']
            tenant = tmp_auth_data['token']['project']
            content =  {'projects': [
                {
                    'is_domain': False,
                    'description': 'tenant info provisioned by VIM onborading',
                    'enabled': True,
                    'domain_id': viminfo['domain'],
                    'parent_id': 'default',
                    'id': tenant['id'],
                    'name': tenant['name']
                }
            ]}
            return Response(headers={'X-Subject-Token': tmp_auth_token}, data={'tenants': content['projects'],'tenants_links':[]},
                            status=status.HTTP_200_OK)
Exemplo n.º 2
0
    def _discover_tenants(self, vimid="", session=None, viminfo=None):
        try:
            # iterate all projects and populate them into AAI
            cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
            for tenant in self._get_list_resources("projects", "identity",
                                                   session, viminfo, vimid,
                                                   "projects"):
                tenant_info = {
                    'tenant-id': tenant['id'],
                    'tenant-name': tenant['name'],
                }
                self._update_resoure(cloud_owner, cloud_region_id,
                                     tenant['id'], tenant_info, "tenant")
            return 0, "succeed"
        except VimDriverNewtonException as e:
            self._logger.error(
                "VimDriverNewtonException: status:%s, response:%s" %
                (e.http_status, e.content))
            return (e.http_status, e.content)
        except HttpError as e:
            if e.http_status == status.HTTP_403_FORBIDDEN:
                ### get the tenant information from the token response
                try:
                    ### get tenant info from the session
                    tmp_auth_state = VimDriverUtils.get_auth_state(session)
                    tmp_auth_info = json.loads(tmp_auth_state)
                    tmp_auth_data = tmp_auth_info['body']
                    tenant = tmp_auth_data['token']['project']
                    tenant_info = {
                        'tenant-id': tenant['id'],
                        'tenant-name': tenant['name'],
                    }

                    self._update_resoure(cloud_owner, cloud_region_id,
                                         tenant['id'], tenant_info, "tenant")

                    return 0, "succeed"

                except Exception as ex:
                    self._logger.error(traceback.format_exc())
                    return (11, str(ex))
            else:
                self._logger.error("HttpError: status:%s, response:%s" %
                                   (e.http_status, e.response.json()))
                return (e.http_status, e.response.json())
        except Exception as e:
            self._logger.error(traceback.format_exc())
            return (11, str(e))
    def post(self, request, vimid=""):
        self._logger.info("vimid> %s" % vimid)
        self._logger.debug("META> %s" % request.META)
        self._logger.debug("data> %s" % request.data)

        sess = None
        resp = None
        resp_body = None
        try:
            tenant_name = request.data.get("tenant_name")
            tenant_id = request.data.get("tenant_id")

            #backward support for keystone v2.0 API
            if not tenant_name and request.data.get("auth"):
                tenant_name = request.data["auth"].get("tenantName")
                tenant_id = request.data["auth"].get("tenantId")

            #keystone v3 API
            if not tenant_name and request.data.get("auth") \
                    and request.data["auth"].get("scope")\
                    and request.data["auth"]["scope"].get("project"):
                if request.data["auth"]["scope"]["project"].get("name"):
                    tenant_name = request.data["auth"]["scope"]["project"].get(
                        "name")
                else:
                    tenant_id = request.data["auth"]["scope"]["project"].get(
                        "id")

            # Get the specified tenant id
            specified_project_idorname = request.META.get("Project", None)

            # prepare request resource to vim instance
            vim = VimDriverUtils.get_vim_info(vimid)
            sess = None
            if specified_project_idorname:
                try:
                    # check if specified with tenant id
                    sess = VimDriverUtils.get_session(
                        vim,
                        tenant_name=None,
                        tenant_id=specified_project_idorname)
                except Exception as e:
                    pass

                if not sess:
                    try:
                        # check if specified with tenant name
                        sess = VimDriverUtils.get_session(
                            vim,
                            tenant_name=specified_project_idorname,
                            tenant_id=None)
                    except Exception as e:
                        pass

            if not sess:
                sess = VimDriverUtils.get_session(vim,
                                                  tenant_name=tenant_name,
                                                  tenant_id=tenant_id)

            #tmp_auth_state = VimDriverUtils.get_auth_state(vim, sess)
            tmp_auth_state = VimDriverUtils.get_auth_state(sess)
            tmp_auth_info = json.loads(tmp_auth_state)
            tmp_auth_token = tmp_auth_info['auth_token']
            tmp_auth_data = tmp_auth_info['body']

            #store the auth_state, redis/memcached
            #set expiring in 1 hour

            #update the catalog
            tmp_auth_data['token'][
                'catalog'], tmp_metadata_catalog = ProxyUtils.update_catalog(
                    vimid, tmp_auth_data['token']['catalog'],
                    self.proxy_prefix)

            VimDriverUtils.update_token_cache(tmp_auth_token, tmp_auth_state,
                                              json.dumps(tmp_metadata_catalog))

            tmp_auth_data['token'][
                'catalog'] = ProxyUtils.update_catalog_dnsaas(
                    vimid, tmp_auth_data['token']['catalog'],
                    self.proxy_prefix, vim)

            resp = Response(headers={'X-Subject-Token': tmp_auth_token},
                            data=tmp_auth_data,
                            status=status.HTTP_201_CREATED)

            self._logger.info("RESP with status> %s" % status.HTTP_201_CREATED)

            return resp
        except VimDriverNewtonException as e:

            self._logger.error("Plugin exception> status:%s,error:%s" %
                               (e.status_code, e.content))
            return Response(data={'error': e.content}, status=e.status_code)
        except HttpError as e:
            self._logger.error("HttpError: status:%s, response:%s" %
                               (e.http_status, e.response.json()))
            return Response(data=e.response.json(), status=e.http_status)
        except Exception as e:
            self._logger.error(traceback.format_exc())

            return Response(data={'error': str(e)},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)