Esempio n. 1
0
def get_vim_by_id(vim_id):
    cloud_owner,cloud_region_id = decode_vim_id(vim_id)

    if cloud_owner and cloud_region_id:
        # get cloud region without depth
        retcode, content, status_code = \
            restcall.req_to_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s"
                       % (cloud_owner,cloud_region_id),"GET")
        if retcode != 0:
            logger.error("Status code is %s, detail is %s.", status_code, content)
            raise VimDriverNewtonException(
                "Failed to query VIM with id (%s:%s,%s)." % (vim_id,cloud_owner,cloud_region_id),
                status_code, content)
        tmp_viminfo = json.JSONDecoder().decode(content)

        # get esr-system-info under this cloud region
        retcode2, content2, status_code2 = \
            restcall.req_to_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s/esr-system-info-list"
                       % (cloud_owner,cloud_region_id),"GET")
        if retcode2 != 0:
            logger.error("Status code is %s, detail is %s.", status_code2, content2)
            raise VimDriverNewtonException(
                "Failed to query esr info for VIM with id (%s:%s,%s)." % (vim_id,cloud_owner,cloud_region_id),
                status_code2, content2)
        tmp_authinfo = json.JSONDecoder().decode(content2)

        #convert vim information
        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
            viminfo['cloud_region_id'] = cloud_region_id
            viminfo['type'] = tmp_viminfo.get('cloud-type')
            viminfo['name'] = tmp_viminfo.get('complex-name')
            viminfo['version'] = tmp_viminfo.get('cloud-region-version')
            viminfo['cloud_extra_info'] = tmp_viminfo.get('cloud-extra-info')

            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")
            # 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
Esempio n. 2
0
def get_vim_by_id(vim_id):

    cloud_owner, cloud_region_id = decode_vim_id(vim_id)

    if cloud_owner and cloud_region_id:
        retcode, content, status_code = \
            restcall.req_to_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s"
                                % (cloud_owner, cloud_region_id), "GET")
        if retcode != 0:
            logger.error("Status code is %s, detail is %s.", status_code,
                         content)
            raise VimDriverNewtonException(
                "Failed to query VIM with id (%s:%s,%s)." %
                (vim_id, cloud_owner, cloud_region_id), status_code, content)
        tmp_viminfo = json.JSONDecoder().decode(content)

        # assume esr-system-info-id is composed by {cloud-owner} _ {cloud-region-id}
        vimid = cloud_owner + "_" + cloud_region_id
        base_url = "/cloud-infrastructure/cloud-regions/cloud-region"
        esr_url = "/%s/%s/esr-system-info-list/esr-system-info/%s" % (
            cloud_owner, cloud_region_id, vimid)
        retcode2, content2, status_code2 = restcall.req_to_aai(
            base_url + esr_url, "GET")
        if retcode2 != 0:
            logger.error("Status code is %s, detail is %s.", status_code,
                         content)
            raise VimDriverNewtonException(
                "Failed to query ESR system with id (%s:%s,%s)." %
                (vim_id, cloud_owner, cloud_region_id), status_code, content)
        tmp_authinfo = json.JSONDecoder().decode(content2)

        # convert vim information
        if tmp_viminfo:
            viminfo = {}
            viminfo['vimId'] = vim_id
            viminfo['cloud_owner'] = cloud_owner
            viminfo['cloud_region_id'] = cloud_region_id
            viminfo['type'] = tmp_viminfo['cloud-type']
            viminfo['name'] = tmp_viminfo['complex-name']
            viminfo['version'] = tmp_viminfo['cloud-region-version']
            viminfo['cloud_extra_info'] = tmp_viminfo['cloud-extra-info']

            if tmp_authinfo:
                viminfo['userName'] = tmp_authinfo['user-name']
                viminfo['password'] = tmp_authinfo['password']
                viminfo['domain'] = tmp_authinfo['cloud-domain']
                viminfo['url'] = tmp_authinfo['service-url']
                viminfo['tenant'] = tmp_authinfo['default-tenant']
                viminfo['cacert'] = tmp_authinfo['ssl-cacert']
                viminfo['insecure'] = tmp_authinfo['ssl-insecure']
            else:
                return None

            return viminfo
        else:
            return None
    else:
        return None
Esempio n. 3
0
    def _delete_flavor_one_extra_spec(self, sess, flavorid, extra_spec_key):
        # prepare request resource to vim instance
        try:
            req_resouce = "/flavors"
            if flavorid and extra_spec_key:
                req_resouce += "/%s" % flavorid
                req_resouce += "/os-extra_specs/%s" % extra_spec_key
            else:
                raise VimDriverNewtonException(
                    message="VIM newton exception",
                    content="internal bug in deleting flavor extra specs: %s" %
                    extra_spec_key,
                    status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)

            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 resp

        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)
Esempio n. 4
0
    def _get_resource_and_metadata(self, servicetype, metadata_catalog, requri):
        real_prefix = None
        proxy_prefix = None
        suffix = None
        if servicetype and metadata_catalog:
            metadata_catalog = json.loads(metadata_catalog)
            service_metadata = metadata_catalog.get(servicetype, None)
            if service_metadata:
                real_prefix = service_metadata['prefix']
                proxy_prefix = service_metadata['proxy_prefix']
                suffix = service_metadata['suffix']

        if not real_prefix or not proxy_prefix:
            raise VimDriverNewtonException(message="internal state error",
                                           content="invalid cached metadata",
                                           status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)

        if requri == suffix:
            requri = None

        if suffix and requri:
            # remove the suffix from the requri to avoid duplicated suffix in real request uri later
            tmp_pattern = re.compile(suffix)
            requri = tmp_pattern.sub('', requri)

        req_resource = ''
        if requri and requri != '':
            req_resource = "/" if re.match(r'//', requri) else '' + requri
        return req_resource, metadata_catalog
Esempio n. 5
0
def delete_vim_by_id(vim_id):
    cloud_owner, cloud_region_id = decode_vim_id(vim_id)
    if cloud_owner and cloud_region_id:
        del_url = "/cloud-infrastructure/cloud-regions/cloud-region/%s/%s"
        retcode, content, status_code = restcall.req_to_aai(del_url % (cloud_owner, cloud_region_id),
                                                            "DELETE")
        if retcode != 0:
            logger.error("Status code is %s, detail is %s.", status_code, content)
            raise VimDriverNewtonException(
                "Failed to delete VIM in AAI with id (%s:%s,%s)." % (vim_id, cloud_owner, cloud_region_id),
                status_code, content)
        return 0
    # return non zero if failed to decode cloud owner and region id
    return 1
Esempio n. 6
0
    def _delete_flavor(self, sess, flavorid):
        # prepare request resource to vim instance
        req_resouce = "/flavors"
        if flavorid:
            req_resouce += "/%s" % flavorid
        else:
            raise VimDriverNewtonException(
                message="VIM newton exception",
                content="internal bug in deleting flavor",
                status_code=500)

        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 resp
Esempio n. 7
0
def delete_vim_by_id(vim_id):
    cloud_owner, cloud_region_id = decode_vim_id(vim_id)
    if cloud_owner and cloud_region_id:
        #get the vim info
        viminfo = get_vim_by_id(vim_id)
        if not viminfo or not viminfo['resource-version']:
            return 0

        retcode, content, status_code = \
            restcall.req_to_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s?resource-version=%s"
                       % ( cloud_owner, cloud_region_id, viminfo['resource-version']), "DELETE")
        if retcode != 0:
            logger.error("Status code is %s, detail is %s.", status_code, content)
            raise VimDriverNewtonException(
                "Failed to delete VIM in AAI with id (%s:%s,%s)." % (vim_id,cloud_owner,cloud_region_id),
                status_code, content)
        return 0
    # return non zero if failed to decode cloud owner and region id
    return 1
Esempio n. 8
0
    def _create_flavor_extra_specs(self, sess, extraspecs, flavorid):
        # prepare request resource to vim instance
        req_resouce = "/flavors"
        if flavorid:
            req_resouce += "/%s/os-extra_specs" % flavorid
        else:
            raise VimDriverNewtonException(
                message="VIM newton exception",
                content="internal bug in creating flavor extra specs",
                status_code=500)

        req_body = json.JSONEncoder().encode({"extra_specs": extraspecs})

        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
Esempio n. 9
0
    def delete(self, request, vimid="", requri=""):
        self._logger.info("vimid,requri: %s, %s" % (vimid, requri))
        self._logger.debug("META: %s" % request.META)

        try:
            if requri == "":
                raise VimDriverNewtonException(
                    message="workload_id is not specified",
                    content="workload_id must be specified to delete the workload",
                    status_code=400)

            # assume the workload_type is heat
            stack_id = requri
            cloud_owner, regionid = extsys.decode_vim_id(vimid)
            # should go via multicloud proxy so that
            #  the selflink is updated by multicloud
            retcode, v2_token_resp_json, os_status = \
                helper.MultiCloudIdentityHelper(
                    settings.MULTICLOUD_API_V1_PREFIX,
                    cloud_owner, regionid, "/v2.0/tokens")

            if retcode > 0 or not v2_token_resp_json:
                logger.error("authenticate fails:%s, %s, %s" %
                             (cloud_owner, regionid, v2_token_resp_json))
                return
            # tenant_id = v2_token_resp_json["access"]["token"]["tenant"]["id"]
            # tenant_name = v2_token_resp_json["access"]["token"]["tenant"]["name"]

            # get stack status
            service_type = "orchestration"
            resource_uri = "/stacks?id=%s" % stack_id if stack_id else "/stacks"
            self._logger.info("retrieve stack resources, URI:%s" % resource_uri)
            retcode, content, os_status = \
                helper.MultiCloudServiceHelper(cloud_owner, regionid,
                                               v2_token_resp_json,
                                               service_type, resource_uri,
                                               None, "GET")

            stacks = content.get('stacks', []) \
                if retcode == 0 and content else []
            # assume there is at most 1 stack returned
            #  since it was filtered by id
            stack1 = stacks[0] if stacks else None
            stack_status = ""

            if stack1 and 'CREATE_COMPLETE' == stack1['stack_status']:
                # delete the stack
                resource_uri = "/stacks/%s/%s" % \
                               (stack1['stack_name'], stack1['id'])
                self._logger.info("delete stack, URI:%s" % resource_uri)
                retcode, content, os_status = \
                    helper.MultiCloudServiceHelper(cloud_owner, regionid,
                                                   v2_token_resp_json,
                                                   service_type, resource_uri,
                                                   None, "DELETE")
                # if retcode == 0:
                #    stack_status = "DELETE_IN_PROCESS"
                #    # and update AAI inventory by heatbridge-delete
                #    self.heatbridge_delete(request, vimid, stack1['id'])

            # stub response
            resp_template = {
                "template_type": "HEAT",
                "workload_id": stack_id,
                "workload_status": stack_status
            }

            if retcode > 0:
                resp_template["workload_response"] = content

            self._logger.info("RESP with data> result:%s" % resp_template)
            return Response(status=os_status)
        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)