Exemplo n.º 1
0
    def post(self, request, vimid="", tenantid="", serverid=""):
        logger.debug("ServerAction--post::> %s" % request.data)
        logger.debug("vimid=%s, tenantid=%s, serverid=%s", vimid, tenantid, serverid)
        try:
            # prepare request resource to vim instance
            vim = VimDriverUtils.get_vim_info(vimid)
            sess = VimDriverUtils.get_session(vim, tenantid)

            # operate server now
            req_resouce = "servers/{server_id}/action".format(server_id=serverid)
            req_body = json.JSONEncoder().encode(request.data)
            resp = sess.post(req_resouce, data=req_body,
                             endpoint_filter=self.service,
                             headers={"Content-Type": "application/json",
                                      "Accept": "application/json"})
            resp_body = resp.json()

            return Response(data=resp_body, status=resp.status_code)
        except VimDriverNewtonException as e:
            return Response(data={'error': e.content}, status=e.status_code)
        except HttpError as e:
            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:
            logger.error(traceback.format_exc())
            return Response(data={'error': str(e)},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 2
0
    def get_alarms(self, query="", vimid="", tenantid="", alarmid=""):
        logger.debug("alarms--get_alarms::> %s" % alarmid)

        # prepare request resource to vim instance
        req_resouce = "/v2/alarms"
        if alarmid:
            req_resouce += "/%s" % alarmid
        elif query:
            req_resouce += "?%s" % query

        vim = VimDriverUtils.get_vim_info(vimid)
        vim["domain"] = "Default"
        sess = VimDriverUtils.get_session(vim, tenantid)
        resp = sess.get(req_resouce, endpoint_filter=self.service)
        content = resp.json()
        vim_dict = {
            "vimName": vim["name"],
            "vimId": vim["vimId"],
            "tenantId": tenantid,
        }
        '''if not alarmid:
            # convert the key naming in alarms
            for alarm in content["alarms"]:
                VimDriverUtils.replace_key_by_mapping(alarm,
                                                      self.keys_mapping)
        else:
            # convert the key naming in the alarm specified by id
            #alarm = content.pop("alarm", None)
            VimDriverUtils.replace_key_by_mapping(content,
                                                  self.keys_mapping)
            #content.update(alarm)'''

        return content, resp.status_code
Exemplo n.º 3
0
    def get_hypervisors(self,
                        query="",
                        vimid="",
                        tenantid="",
                        hypervisorid=""):

        req_resource = "/os-hypervisors"

        vim = VimDriverUtils.get_vim_info(vimid)
        vim["domain"] = "Default"
        sess = VimDriverUtils.get_session(vim, tenantid)

        self.service['region_name'] = vim['openstack_region_id'] \
            if vim.get('openstack_region_id') \
            else vim['cloud_region_id']

        logger.info("making request with URI:%s" % req_resouce)

        resp = sess.get(req_resource, endpoint_filter=self.service)

        logger.info("request returns with status %s" % resp.status_code)
        if resp.status_code == status.HTTP_200_OK:
            logger.debug("with content:%s" % resp.json())
            pass

        content = resp.json()

        return content, resp.status_code
Exemplo n.º 4
0
    def delete(self, request, vimid="", tenantid="", serverid="", portid=""):
        logger.debug("ServerOsInterfacePort--delete::portid=%s", portid)
        logger.debug("vimid=%s, tenantid=%s, serverid=%s", vimid, tenantid, serverid)
        try:
            # prepare request resource to vim instance
            vim = VimDriverUtils.get_vim_info(vimid)
            sess = VimDriverUtils.get_session(vim, tenantid)

            # operate server now
            req_resfmt = "servers/{server_id}/os-interface/{port_id}"
            req_resouce = req_resfmt.format(server_id=serverid, port_id=portid)
            resp = sess.delete(req_resouce,
                             endpoint_filter=self.service,
                             headers={"Content-Type": "application/json",
                                      "Accept": "application/json"})
            resp_body = {}

            return Response(data=resp_body, status=resp.status_code)
        except VimDriverNewtonException as e:
            return Response(data={'error': e.content}, status=e.status_code)
        except HttpError as e:
            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:
            logger.error(traceback.format_exc())
            return Response(data={'error': str(e)},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 5
0
    def delete(self, request, vimid="", tenantid="", volumeid=""):
        logger.info("vimid, tenantid, volumeid = %s,%s,%s" % (vimid, tenantid, volumeid))
        if request.data:
            logger.debug("With data = %s" % request.data)
            pass
        try:
            # prepare request resource to vim instance
            req_resouce = "volumes"
            if volumeid:
                req_resouce += "/%s" % volumeid

            vim = VimDriverUtils.get_vim_info(vimid)
            sess = VimDriverUtils.get_session(vim, tenantid)

            self.service['region_name'] = vim['openstack_region_id'] \
                if vim.get('openstack_region_id') \
                else vim['cloud_region_id']

            logger.info("making request with URI:%s" % req_resouce)
            resp = sess.delete(req_resouce, endpoint_filter=self.service)
            logger.info("request returns with status %s" % resp.status_code)
            return Response(status=resp.status_code)
        except VimDriverNewtonException as e:
            logger.error("response with status = %s" % e.status_code)
            return Response(data={'error': e.content}, status=e.status_code)
        except HttpError as e:
            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:
            logger.error(traceback.format_exc())
            return Response(data={'error': str(e)},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 6
0
    def detach_volume(self, vimid, tenantid, serverid, *volumeids):
        logger.debug("Server--detach_volume::> %s, %s" % (serverid, volumeids))
        try:
            # prepare request resource to vim instance
            vim = VimDriverUtils.get_vim_info(vimid)
            sess = VimDriverUtils.get_session(vim, tenantid)

            self.service['region_name'] = vim['openstack_region_id'] \
                if vim.get('openstack_region_id') \
                else vim['cloud_region_id']

            # wait server to be ready to detach volume

            # assume attachment id is the same as volume id
            for volumeid in volumeids:
                req_resouce = "servers/%s/os-volume_attachments/%s" % (serverid, volumeid)

                logger.info("making request with URI:%s" % req_resouce)
                resp = sess.delete(req_resouce,
                                   endpoint_filter=self.service,
                                   headers={"Content-Type": "application/json",
                                            "Accept": "application/json"})
                logger.info("request returns with status %s" % resp.status_code)
                logger.debug("Servers--dettachVolume resp::>%s" % resp.json())

            return None
        except HttpError as e:
            logger.error("detach_volume, HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
            return None
        except Exception as e:
            logger.error(traceback.format_exc())
            logger.debug("Failed to detach_volume:%s" % str(e))
            return None
Exemplo n.º 7
0
    def delete(self, request, vimid="", tenantid="", flavorid=""):
        logger.info("vimid, tenantid, flavorid = %s,%s,%s" %
                    (vimid, tenantid, flavorid))
        if request.data:
            logger.debug("With data = %s" % request.data)
            pass

        try:
            # prepare request resource to vim instance
            vim = VimDriverUtils.get_vim_info(vimid)
            sess = VimDriverUtils.get_session(vim, tenantid)

            self.service['region_name'] = vim['openstack_region_id'] \
                if vim.get('openstack_region_id') \
                else vim['cloud_region_id']

            #delete extra specs one by one
            resp = self._delete_flavor_extra_specs(sess, flavorid)

            #delete flavor
            resp = self._delete_flavor(sess, flavorid)

            #return results
            return Response(status=resp.status_code)
        except VimDriverNewtonException as e:
            logger.error("response with status = %s" % e.status_code)
            return Response(data={'error': e.content}, status=e.status_code)
        except HttpError as e:
            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:
            logger.error(traceback.format_exc())
            return Response(data={'error': str(e)},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 8
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.º 9
0
    def attach_volume(self, vimid, tenantid, serverid, *volumeids):
        logger.info("vimid, tenantid, serverid, volumeids = %s,%s,%s,%s" % (vimid, tenantid, serverid, volumeids))
        try:
            # prepare request resource to vim instance
            vim = VimDriverUtils.get_vim_info(vimid)
            sess = VimDriverUtils.get_session(vim, tenantid)

            self.service['region_name'] = vim['openstack_region_id'] \
                if vim.get('openstack_region_id') \
                else vim['cloud_region_id']

            # check if server is ready to attach
            logger.debug("Servers--attach_volume, wait for server to be ACTIVE::>%s" % serverid)
            req_resouce = "servers/%s" % serverid
            while True:
                logger.info("making request with URI:%s" % req_resouce)
                resp = sess.get(req_resouce, endpoint_filter=self.service)
                if resp.status_code == status.HTTP_200_OK:
                    logger.debug("with content:%s" % resp.json())
                    pass

                content = resp.json()
                if content and content["server"] and content["server"]["status"] == "ACTIVE":
                    break

            for volumeid in volumeids:
                req_resouce = "servers/%s/os-volume_attachments" % serverid
                req_data = {"volumeAttachment": {
                    "volumeId": volumeid
                }}
                logger.debug("Servers--attach_volume::>%s, %s" % (req_resouce, req_data))
                req_body = json.JSONEncoder().encode(req_data)
                logger.info("making request with URI:%s" % req_resouce)
                resp = sess.post(req_resouce, data=req_body,
                                 endpoint_filter=self.service,
                                 headers={"Content-Type": "application/json",
                                          "Accept": "application/json"})
                logger.info("request returns with status %s" % resp.status_code)
                logger.debug("Servers--attach_volume resp::>%s" % resp.json())

            return None
        except HttpError as e:
            logger.error("attach_volume, HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
            return None
        except Exception as e:
            logger.error(traceback.format_exc())
            logger.debug("Failed to attach_volume:%s" % str(e))
            return None
Exemplo n.º 10
0
    def get(self, request, vimid="", tenantid="", portid=""):
        logger.info("vimid, tenantid, portid = %s,%s,%s" %
                    (vimid, tenantid, portid))
        if request.data:
            logger.debug("With data = %s" % request.data)
            pass
        try:
            # prepare request resource to vim instance
            querystr = VimDriverUtils.get_query_part(request)
            query = "project_id=%s" % (tenantid)
            if querystr:
                query += "&" + querystr

            content, status_code = self._get_ports(query, vimid, tenantid,
                                                   portid)
            logger.info("response with status = %s" % status_code)
            return Response(data=content, status=status_code)
        except VimDriverNewtonException as e:
            logger.error("response with status = %s" % e.status_code)
            return Response(data={'error': e.content}, status=e.status_code)
        except HttpError as e:
            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:
            logger.error(traceback.format_exc())
            return Response(data={'error': str(e)},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 11
0
    def get(self, request, vimid="", tenantid="", hypervisorid=""):
        logger.info("vimid, tenantid, hypervisorid = %s,%s,%s" %
                    (vimid, tenantid, hypervisorid))
        if request.data:
            logger.debug("With data = %s" % request.data)
            pass

        try:
            query = VimDriverUtils.get_query_part(request)
            content, status_code = self.get_hypervisors(
                query, vimid, tenantid, hypervisorid)

            logger.info("response with status = %s" % status_code)

            return Response(data=content, status=status_code)
        except VimDriverNewtonException as e:
            logger.error("response with status = %s" % e.status_code)
            return Response(data={'error': e.content}, status=e.status_code)
        except HttpError as e:
            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:
            logger.error(traceback.format_exc())
            return Response(data={'error': str(e)},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 12
0
    def _get_ports(self, query="", vimid="", tenantid="", portid=""):
        vim = VimDriverUtils.get_vim_info(vimid)
        sess = VimDriverUtils.get_session(vim, tenantid)

        if sess:
            # prepare request resource to vim instance
            req_resouce = "v2.0/ports"
            if portid:
                req_resouce += "/%s" % portid

            if query:
                req_resouce += "?%s" % query

            vim = VimDriverUtils.get_vim_info(vimid)
            sess = VimDriverUtils.get_session(vim, tenantid)

            self.service['region_name'] = vim['openstack_region_id'] \
                if vim.get('openstack_region_id') \
                else vim['cloud_region_id']

            logger.info("making request with URI:%s" % req_resouce)
            resp = sess.get(req_resouce, endpoint_filter=self.service)
            logger.info("request returns with status %s" % resp.status_code)
            if resp.status_code == status.HTTP_200_OK:
                logger.debug("with content:%s" % resp.json())
                pass

            content = resp.json()
            vim_dict = {
                "vimName": vim["name"],
                "vimId": vim["vimId"],
                "cloud-owner": vim["cloud_owner"],
                "cloud-region-id": vim["cloud_region_id"],
                "tenantId": tenantid,
            }
            content.update(vim_dict)

            if not portid:
                # convert the key naming in ports
                for port in content["ports"]:
                    # use only 1st entry of fixed_ips
                    tmpips = port.pop("fixed_ips", None) if port else None
                    port.update(
                        tmpips[0]) if tmpips and len(tmpips) > 0 else None
                    VimDriverUtils.replace_key_by_mapping(
                        port, self.keys_mapping)
            else:
                # convert the key naming in the port specified by id
                port = content.pop("port", None)
                #use only 1st entry of fixed_ips
                tmpips = port.pop("fixed_ips", None) if port else None
                port.update(tmpips[0]) if tmpips and len(tmpips) > 0 else None

                VimDriverUtils.replace_key_by_mapping(port, self.keys_mapping)
                content.update(port)
            return content, resp.status_code
        return {}, 500
Exemplo n.º 13
0
 def has_permission(self, request, view):
     logger.debug("HasValidToken--has_permission::META> %s" % request.META)
     token = request.META.get('HTTP_X_AUTH_TOKEN', None)
     if token:
         state, metadata = VimDriverUtils.get_token_cache(token)
         if state:
             return True
     return False
Exemplo n.º 14
0
    def head(self, request, vimid="", servicetype="", requri=""):
        self._logger.info("vimid, servicetype, requri> %s,%s,%s"
                     % (vimid, servicetype, requri))
        self._logger.debug("META, data> %s , %s" % (request.META, request.data))

        token = self._get_token(request)
        try:
            vim = VimDriverUtils.get_vim_info(vimid)
            auth_state, metadata_catalog = VimDriverUtils.get_token_cache(token)
            sess = VimDriverUtils.get_session(vim, auth_state=auth_state)

            req_resource = ''
            if requri and requri != '':
                req_resource = "/" if re.match(r'//', requri) else ''+ requri

            cloud_owner, regionid = extsys.decode_vim_id(vimid)
            interface = 'public'
            service = {'service_type': servicetype,
                       'interface': interface,
                       'region_name': vim['openstack_region_id']
                           if vim.get('openstack_region_id')
                           else vim['cloud_region_id']
                       }


            self._logger.info("service head request with uri %s, %s" % (req_resource, service))
            resp = sess.head(req_resource, endpoint_filter=service)
            self._logger.info("service head response status %s" % (resp.status_code))

            content = resp.json() if resp.content else None
            self._logger.debug("service head response: %s" % (content))

            return Response(headers={'X-Subject-Token': token}, data=content, status=resp.status_code)
        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)
Exemplo n.º 15
0
    def _dettach_volume(self, vimid, tenantid, serverId, *volumeIds):
        # assume attachment id is the same as volume id
        vim = VimDriverUtils.get_vim_info(vimid)
        sess = VimDriverUtils.get_session(vim, tenantid)

        self.service['region_name'] = vim['openstack_region_id'] \
            if vim.get('openstack_region_id') \
            else vim['cloud_region_id']

        for volumeid in volumeIds:
            req_resouce = "servers/%s/os-volume_attachments/%s" % (serverId, volumeid)
            logger.debug("Servers--dettachVolume::>%s" % req_resouce)
            logger.info("making request with URI:%s" % req_resouce)
            resp = sess.delete(req_resouce,
                               endpoint_filter=self.service,
                               headers={"Content-Type": "application/json",
                                        "Accept": "application/json"})
            logger.info("request returns with status %s" % resp.status_code)
            logger.debug("Servers--dettachVolume resp status::>%s" % resp.status_code)
Exemplo n.º 16
0
    def _create_flavor(self, sess, request):
        # prepare request resource to vim instance
        req_resouce = "/flavors"

        flavor = request.data

        VimDriverUtils.replace_key_by_mapping(flavor, self.keys_mapping, True)
        req_body = json.JSONEncoder().encode({"flavor": flavor})

        logger.info("making request with URI:%s" % req_resouce)
        logger.debug("with data:%s" % req_body)

        resp = sess.post(req_resouce,
                         data=req_body,
                         endpoint_filter=self.service)

        logger.info("request returns with status %s" % resp.status_code)

        return resp
Exemplo n.º 17
0
    def get_servers(self, query="", vimid="", tenantid="", serverid=""):

        req_resouce = "/servers"
        vim = VimDriverUtils.get_vim_info(vimid)
        vim["domain"] = "Default"
        sess = VimDriverUtils.get_session(vim, tenantid)

        logger.info("making request with URI:%s" % req_resouce)
        resp = sess.get(req_resouce, endpoint_filter=self.service)
        logger.info("request returns with status %s" % resp.status_code)
        if resp.status_code == status.HTTP_200_OK:
            logger.debug("with content:%s" % resp.json())
            pass

        content = resp.json()
        vim_dict = {
            "vimName": vim["name"],
            "vimId": vim["vimId"],
            "tenantId": tenantid,
        }

        return content, resp.status_code
Exemplo n.º 18
0
    def delete(self, request, vimid="", tenantid="", serverid=""):
        logger.info("vimid, tenantid, serverid = %s,%s,%s" % (vimid, tenantid, serverid))
        if request.data:
            logger.debug("With data = %s" % request.data)
            pass
        try:
            # prepare request resource to vim instance
            vim = VimDriverUtils.get_vim_info(vimid)
            sess = VimDriverUtils.get_session(vim, tenantid)

            self.service['region_name'] = vim['openstack_region_id'] \
                if vim.get('openstack_region_id') \
                else vim['cloud_region_id']

            # check and dettach them if volumes attached to server
            server, status_code = self._get_servers("", vimid, tenantid, serverid)
            volumearray = server.pop("volumeArray", None)
            if volumearray and len(volumearray) > 0:
                volumeIds = [extraVolume["volumeId"] for extraVolume in volumearray]
                self._dettach_volume(vimid, tenantid, serverid, *volumeIds)

            # delete server now
            req_resouce = "servers"
            if serverid:
                req_resouce += "/%s" % serverid
            logger.info("making request with URI:%s" % req_resouce)
            resp = sess.delete(req_resouce, endpoint_filter=self.service)
            logger.info("request returns with status %s" % resp.status_code)
            return Response(status=resp.status_code)
        except VimDriverNewtonException as e:
            logger.error("response with status = %s" % e.status_code)
            return Response(data={'error': e.content}, status=e.status_code)
        except HttpError as e:
            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:
            logger.error(traceback.format_exc())
            return Response(data={'error': str(e)},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 19
0
    def _get_ports(self, vimid="", tenantid="", serverid=None):
        # query attached ports
        vim = VimDriverUtils.get_vim_info(vimid)
        sess = VimDriverUtils.get_session(vim, tenantid)
        req_resouce = "servers/%s/os-interface" % serverid

        self.service['region_name'] = vim['openstack_region_id'] \
            if vim.get('openstack_region_id') \
            else vim['cloud_region_id']

        logger.info("making request with URI:%s" % req_resouce)
        resp = sess.get(req_resouce, endpoint_filter=self.service)
        logger.info("request returns with status %s" % resp.status_code)
        if resp.status_code == status.HTTP_200_OK:
            logger.debug("with content:%s" % resp.json())
            pass
        ports = resp.json()
        if ports and ports["interfaceAttachments"] \
                and len(ports["interfaceAttachments"]) > 0:
            return [{"portId":port["port_id"]}
                    for port in ports["interfaceAttachments"]]
        return None
Exemplo n.º 20
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))
Exemplo n.º 21
0
 def get(self, request, vimid="", tenantid="", alarmid=""):
     logger.debug("alarms--get::> %s" % request.data)
     try:
         # prepare request resource to vim instance
         query = VimDriverUtils.get_query_part(request)
         content, status_code = self.get_alarms(query, vimid, tenantid,
                                                alarmid)
         return Response(data=content, status=status_code)
     except VimDriverNewtonException as e:
         return Response(data={'error': e.content}, status=e.status_code)
     except HttpError as e:
         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:
         logger.error(traceback.format_exc())
         return Response(data={'error': str(e)},
                         status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 22
0
    def _get_volumes(self, query="", vimid="", tenantid="", volumeid=None):

        # prepare request resource to vim instance
        req_resouce = "volumes"
        if volumeid:
            req_resouce += "/%s" % volumeid
        else:
            req_resouce += "/detail"
            if query:
                req_resouce += "?%s" % query

        vim = VimDriverUtils.get_vim_info(vimid)
        sess = VimDriverUtils.get_session(vim, tenantid)

        self.service['region_name'] = vim['openstack_region_id'] \
            if vim.get('openstack_region_id') \
            else vim['cloud_region_id']

        logger.info("making request with URI:%s" % req_resouce)
        resp = sess.get(req_resouce, endpoint_filter=self.service)
        logger.info("request returns with status %s" % resp.status_code)
        if resp.status_code == status.HTTP_200_OK:
            logger.debug("with content:%s" % resp.json())
            pass
        content = resp.json()
        vim_dict = {
            "vimName": vim["name"],
            "vimId": vim["vimId"],
            "cloud-owner": vim["cloud_owner"],
            "cloud-region-id": vim["cloud_region_id"],
            "tenantId": tenantid,
        }
        content.update(vim_dict)

        if not volumeid:
            # convert the key naming in volumes
            for volume in content["volumes"]:
                VimDriverUtils.replace_key_by_mapping(volume,
                                                      self.keys_mapping)
        else:
            # convert the key naming in the volume specified by id
            volume = content.pop("volume", None)
            VimDriverUtils.replace_key_by_mapping(volume,
                                                  self.keys_mapping)
            content.update(volume)

        return content, resp.status_code
Exemplo n.º 23
0
    def _create_port(self, request, vimid, tenantid):
        vim = VimDriverUtils.get_vim_info(vimid)
        sess = VimDriverUtils.get_session(vim, tenantid)
        if sess:
            # prepare request resource to vim instance
            req_resouce = "v2.0/ports"

            port = request.data
            #handle ip and subnetId
            tmpip = port.pop("ip", None)
            tmpsubnet = port.pop("subnetId", None)
            if tmpip and tmpsubnet:
                port["fixed_ips"] = []
                for one_tmpip in tmpip.split(','):
                    fixed_ip = {
                        "ip_address": one_tmpip,
                        "subnet_id": tmpsubnet,
                    }
                    port["fixed_ips"].append(fixed_ip)

            VimDriverUtils.replace_key_by_mapping(port, self.keys_mapping,
                                                  True)
            req_body = json.JSONEncoder().encode({"port": port})
            logger.info("making request with URI:%s" % req_resouce)
            logger.debug("with data:%s" % req_body)
            resp = sess.post(req_resouce,
                             data=req_body,
                             endpoint_filter=self.service)
            logger.info("request returns with status %s" % resp.status_code)
            resp_body = resp.json()["port"]
            #use only 1 fixed_ip
            tmpips = resp_body.pop("fixed_ips", None)
            if tmpips and len(tmpips) > 0:
                resp_body.update(tmpips[0])

            VimDriverUtils.replace_key_by_mapping(resp_body, self.keys_mapping)
            vim_dict = {
                "vimName": vim["name"],
                "vimId": vim["vimId"],
                "cloud-owner": vim["cloud_owner"],
                "cloud-region-id": vim["cloud_region_id"],
                "tenantId": tenantid,
                "returnCode": 1,
            }
            resp_body.update(vim_dict)
            return Response(data=resp_body, status=resp.status_code)
        return {}
Exemplo n.º 24
0
    def post(self, request, vimid=""):
        self._logger.info("registration with :  %s" % vimid)

        # check if auditor is enabled
        viminfo = VimDriverUtils.get_vim_info(vimid)
        cloud_extra_info = viminfo.get("cloud_extra_info_json", {})
        if cloud_extra_info.get("capacity_auditor_enabled", False):
            # 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, settings.AAI_BASE_URL)
            backlog_item = {
                "id": vimid,
                "worker": worker_self.azcap_audit,
                "payload": (vimid, specified_project_idorname),
                "repeat": 10 * 1000000,  # repeat every 10 seconds
            }
            gAZCapAuditThread.add(backlog_item)
            if 0 == gAZCapAuditThread.state():
                gAZCapAuditThread.start()
        return super(APIv0Registry, self).post(request, vimid)
    def workload_detail(self, vimid, workloadid, request):
        '''
        get workload status
        '''
        # resp_template = {
        #     "template_type": "HEAT",
        #     "workload_id": workloadid,
        #     "workload_status": "GET_FAILED",
        #     "workload_status_reason": "Exception occurs"
        # }
        # status_code = status.HTTP_500_INTERNAL_SERVER_ERROR

        workload_query_str = VimDriverUtils.get_query_part(request)
        workload_data = request.data

        multicloudK8sUrl = "%s://%s:%s/api/multicloud-k8s/v1" % (
            settings.MSB_SERVICE_PROTOCOL, settings.MSB_SERVICE_ADDR,
            settings.MSB_SERVICE_PORT)

        # forward infra_workload API requests with queries
        cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
        infraUrl = multicloudK8sUrl + "/%s/%s/infra_workload" % (
            cloud_owner, cloud_region_id)
        if workloadid:
            infraUrl += ("/%s" % workloadid)
        if workload_query_str:
            infraUrl += ("?%s" % workload_query_str)

        # should we forward headers ? TBD
        logger.debug("request with url,content: %s,%s" %
                     (infraUrl, workload_data))
        # resp = requests.get(infraUrl, data=json.dumps(workload_data), verify=False)
        resp = requests.get(infraUrl, verify=False)
        # resp_template["workload_status_reason"] = resp.content
        logger.debug("response status,content: %s,%s" %
                     (resp.status_code, resp.content))
        return Response(data=json.loads(resp.content), status=resp.status_code)
Exemplo n.º 26
0
    def post(self, request, cloud_owner="", cloud_region_id=""):
        self._logger.info("registration with : %s, %s" %
                          (cloud_owner, cloud_region_id))

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

            vimid = extsys.encode_vim_id(cloud_owner, cloud_region_id)

            viminfo = VimDriverUtils.get_vim_info(vimid)
            cloud_extra_info = viminfo.get("cloud_extra_info_json", {})
            if cloud_extra_info.get("capacity_auditor_enabled", False):
                # vim registration will trigger the start the audit of AZ capacity
                worker_self = InfraResourceAuditor(
                    settings.MULTICLOUD_API_V1_PREFIX, settings.AAI_BASE_URL)
                backlog_item = {
                    "id": vimid,
                    "worker": worker_self.azcap_audit,
                    "payload": (vimid, specified_project_idorname),
                    "repeat": 5 * 1000000,  # repeat every 5 seconds
                }
                gAZCapAuditThread.add(backlog_item)
                if 0 == gAZCapAuditThread.state():
                    gAZCapAuditThread.start()

            return super(APIv1Registry, self).post(request, vimid)

        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)
Exemplo n.º 27
0
    def _get_flavor(self, sess, request, flavorid=""):
        if sess:
            # prepare request resource to vim instance
            req_resouce = "/flavors"
            if flavorid:
                req_resouce += "/%s" % flavorid
            else:
                req_resouce += "/detail"

            query = VimDriverUtils.get_query_part(request)
            if query:
                req_resouce += "?%s" % query

            logger.info("making request with URI:%s" % req_resouce)

            resp = sess.get(req_resouce, endpoint_filter=self.service)

            logger.info("request returns with status %s" % resp.status_code)
            if resp.status_code == status.HTTP_200_OK:
                logger.debug("with content:%s" % resp.json())
                pass

            return resp
        return {}
Exemplo n.º 28
0
    def post(self, request, vimid="", tenantid="", volumeid=""):
        logger.info("vimid, tenantid, volumeid = %s,%s,%s" % (vimid, tenantid, volumeid))
        if request.data:
            logger.debug("With data = %s" % request.data)
            pass
        try:
            #check if created already: check name
            query = "name=%s" % request.data["name"]
            content, status_code = self._get_volumes(query, vimid, tenantid)
            existed = False
            if status_code == 200:
                for volume in content["volumes"]:
                    if volume["name"] == request.data["name"]:
                        existed = True
                        break
                if existed == True:
                    vim_dict = {
                        "returnCode": 0,
                    }
                    volume.update(vim_dict)
                    return Response(data=volume, status=status_code)

            # prepare request resource to vim instance
            req_resouce = "volumes"

            vim = VimDriverUtils.get_vim_info(vimid)
            sess = VimDriverUtils.get_session(vim, tenantid)
            volume = request.data
            VimDriverUtils.replace_key_by_mapping(volume,
                                                  self.keys_mapping, True)
            req_body = json.JSONEncoder().encode({"volume": volume})

            self.service['region_name'] = vim['openstack_region_id'] \
                if vim.get('openstack_region_id') \
                else vim['cloud_region_id']

            logger.info("making request with URI:%s" % req_resouce)
            logger.debug("with data:%s" % req_body)
            resp = sess.post(req_resouce, data=req_body,
                             endpoint_filter=self.service, headers={"Content-Type": "application/json",
                             "Accept": "application/json" })
            logger.info("request returns with status %s" % resp.status_code)
            resp_body = resp.json()["volume"]
            VimDriverUtils.replace_key_by_mapping(resp_body, self.keys_mapping)
            vim_dict = {
                "vimName": vim["name"],
                "vimId": vim["vimId"],
                "cloud-owner": vim["cloud_owner"],
                "cloud-region-id": vim["cloud_region_id"],
                "tenantId": tenantid,
                "returnCode": 1,
            }
            resp_body.update(vim_dict)
            return Response(data=resp_body, status=resp.status_code)
        except VimDriverNewtonException as e:
            logger.error("response with status = %s" % e.status_code)
            return Response(data={'error': e.content}, status=e.status_code)
        except HttpError as e:
            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:
            logger.error(traceback.format_exc())
            return Response(data={'error': str(e)},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 29
0
    def get(self, request, vimid="", tenantid="", hostname=""):
        logger.info("vimid, tenantid, hostname = %s,%s,%s" % (vimid, tenantid, hostname))
        if request.data:
            logger.debug("With data = %s" % request.data)
            pass
        try:
            #prepare request resource to vim instance
            req_resouce = "/os-hosts"
            if hostname:
                req_resouce += "/%s" % hostname

            vim = VimDriverUtils.get_vim_info(vimid)
            sess = VimDriverUtils.get_session(vim, tenantid)

            self.service['region_name'] = vim['openstack_region_id'] \
                if vim.get('openstack_region_id') \
                else vim['cloud_region_id']

            logger.info("making request with URI:%s" % req_resouce)
            resp = sess.get(req_resouce, endpoint_filter=self.service)
            logger.info("request returns with status %s" % resp.status_code)
            if resp.status_code == status.HTTP_200_OK:
                logger.debug("with content:%s" % resp.json())
                pass

            content = resp.json()
            vim_dict = {
                "vimName": vim["name"],
                "vimId": vim["vimId"],
                "tenantId": tenantid,
            }
            content.update(vim_dict)


            if not hostname:
                # convert the key naming in hosts
                for host in content["hosts"]:
                    VimDriverUtils.replace_key_by_mapping(host,
                                                          self.hosts_keys_mapping)
            else:
                # restructure host data model
                old_host = content["host"]
                content["host"] = []
                # convert the key naming in resources
                for res in old_host:
                    VimDriverUtils.replace_key_by_mapping(res['resource'],
                                                          self.host_keys_mapping)
                    content["host"].append(res['resource'])

            logger.info("response with status = %s" % resp.status_code)

            return Response(data=content, status=resp.status_code)

        except VimDriverNewtonException as e:
            logger.error("response with status = %s" % e.status_code)
            return Response(data={'error': e.content}, status=e.status_code)
        except HttpError as e:
            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:
            logger.error(traceback.format_exc())
            return Response(data={'error': str(e)},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 30
0
    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

        sess = None
        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:
            # get all hypervisor detail ?
            hypervisors = self._get_list_resources("/os-hypervisors/detail",
                                                   "compute", session, viminfo,
                                                   vimid, "hypervisors")

            hypervisors_dict = {}
            # for h in hypervisors:
            #     if not h.get("service", None):
            #         continue
            #     if not h.get("host", None):
            #         continue
            #     hypervisors_dict[h["service"]["host"]] = h
            for h in hypervisors:
                if not h.get("hypervisor_hostname", None):
                    continue
                hypervisors_dict[h["hypervisor_hostname"]] = h

            vimAzCacheKey = "cap_azlist_" + vimid
            vimAzList = []
            # cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
            for az in self._get_list_resources("/os-availability-zone/detail",
                                               "compute", session, viminfo,
                                               vimid, "availabilityZoneInfo"):
                az_info = {
                    'availability-zone-name':
                    az.get('zoneName', ""),
                    'operational-status':
                    az.get('zoneState', {}).get('available', ""),
                    'hypervisor-type':
                    '',
                }
                # filter out the default az: "internal" and "nova"
                azName = az.get('zoneName', None)
                # comment it for test the registration process only
                #  if azName == 'nova':
                #    continue
                if azName == 'internal':
                    continue

                vimAzList.append(azName)

                # get list of host names
                pservers_info = [k for (k, v) in list(az['hosts'].items())]

                # Get current cap info of azName
                azCapCacheKey = "cap_" + vimid + "_" + azName
                azCapInfoCacheStr = cache.get(azCapCacheKey)
                azCapInfoCache = json.loads(
                    azCapInfoCacheStr) if azCapInfoCacheStr else {}

                for psname in pservers_info:
                    psinfo = hypervisors_dict.get(psname, None)
                    if not psinfo:
                        # warning: the pserver info not found
                        continue
                    # get current pserver cap info
                    psCapInfoCacheKey = "cap_" + vimid + "_" + psname
                    psCapInfoCacheStr = cache.get(psCapInfoCacheKey)
                    psCapInfoCache = json.loads(
                        psCapInfoCacheStr) if psCapInfoCacheStr else {}

                    # compare latest info with cached one
                    vcpu_delta = 0
                    vcpu_used_delta = 0
                    mem_delta = 0
                    mem_free_delta = 0
                    localstorage_delta = 0
                    localstorage_free_delta = 0
                    if psinfo.get("vcpus", 0) != psCapInfoCache.get(
                            "vcpus", 0):
                        vcpu_delta += psinfo.get("vcpus", 0) \
                                      - psCapInfoCache.get("vcpus", 0)
                        psCapInfoCache["vcpus"] = psinfo.get("vcpus", 0)
                    if psinfo.get("memory_mb", 0) != psCapInfoCache.get(
                            "memory_mb", 0):
                        mem_delta += psinfo.get("memory_mb", 0) \
                                     - psCapInfoCache.get("memory_mb", 0)
                        psCapInfoCache["memory_mb"] = psinfo.get(
                            "memory_mb", 0)
                    if psinfo.get("local_gb", 0) != psCapInfoCache.get(
                            "local_gb", 0):
                        localstorage_delta += psinfo.get("local_gb", 0) \
                                              - psCapInfoCache.get("local_gb", 0)
                        psCapInfoCache["local_gb"] = psinfo.get("local_gb", 0)
                    if psinfo.get("vcpus_used", 0) != psCapInfoCache.get(
                            "vcpus_used", 0):
                        vcpu_used_delta += psinfo.get("vcpus_used", 0)\
                                     - psCapInfoCache.get("vcpus_used", 0)
                        psCapInfoCache["vcpus_used"] = psinfo.get(
                            "vcpus_used", 0)
                    if psinfo.get("free_ram_mb", 0) != psCapInfoCache.get(
                            "free_ram_mb", 0):
                        mem_free_delta += psinfo.get("free_ram_mb", 0)\
                                     - psCapInfoCache.get("free_ram_mb", 0)
                        psCapInfoCache["free_ram_mb"] = psinfo.get(
                            "free_ram_mb", 0)
                    if psinfo.get("free_disk_gb", 0) != psCapInfoCache.get(
                            "free_disk_gb", 0):
                        localstorage_free_delta += psinfo.get("free_disk_gb", 0)\
                                     - psCapInfoCache.get("free_disk_gb", 0)
                        psCapInfoCache["free_disk_gb"] = psinfo.get(
                            "free_disk_gb", 0)

                    cache.set(psCapInfoCacheKey, json.dumps(psCapInfoCache),
                              3600 * 24)

                    # now apply the delta to azCapInfo
                    azCapInfoCache["vcpus"] = azCapInfoCache.get(
                        "vcpus", 0) + vcpu_delta
                    azCapInfoCache["memory_mb"] = azCapInfoCache.get(
                        "memory_mb", 0) + mem_delta
                    azCapInfoCache["local_gb"] = azCapInfoCache.get(
                        "local_gb", 0) + localstorage_delta
                    azCapInfoCache["vcpus_used"] = azCapInfoCache.get(
                        "vcpus_used", 0) + vcpu_used_delta
                    azCapInfoCache["free_ram_mb"] = azCapInfoCache.get(
                        "free_ram_mb", 0) + mem_free_delta
                    azCapInfoCache["free_disk_gb"] = azCapInfoCache.get(
                        "free_disk_gb", 0) + localstorage_free_delta
                    pass

                # update the cache
                cache.set(azCapCacheKey, json.dumps(azCapInfoCache), 3600 * 24)
                cache.set(vimAzCacheKey, json.dumps(vimAzList), 3600 * 24)
        except Exception as e:
            self._logger.error("azcap_audit raise exception: %s" % e)
            pass