def get_subnets(self, query="", vimid="", tenantid="", subnetid=""):
        logger.debug("Subnets--get_subnets::> %s" % subnetid)

        # prepare request resource to vim instance
        req_resouce = "v2.0/subnets"
        if subnetid:
            req_resouce += "/%s" % subnetid

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

        vim = VimDriverUtils.get_vim_info(vimid)
        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,
        }
        content.update(vim_dict)

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

        return content, resp.status_code
    def get_volumes(self, query="", vimid="", tenantid="", volumeid=None):
        logger.debug("Volumes--get_volumes::> %s,%s" % (tenantid, volumeid))

        # 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)
        resp = sess.get(req_resouce, endpoint_filter=self.service)
        content = resp.json()
        vim_dict = {
            "vimName": vim["name"],
            "vimId": vim["vimId"],
            "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
Beispiel #3
0
    def get_networks(self, query, vimid="", tenantid="", networkid=""):
        logger.debug("Networks--get_networks::> %s" % networkid)

        # prepare request resource to vim instance
        req_resouce = "v2.0/networks"
        if networkid:
            req_resouce += "/%s" % networkid

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

        vim = VimDriverUtils.get_vim_info(vimid)
        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,
        }
        content.update(vim_dict)

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

        return content, resp.status_code
    def get_images(self, query="", vimid="", tenantid="", imageid=""):
        logger.debug("Images--get_images::> %s" % imageid)

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

        vim = VimDriverUtils.get_vim_info(vimid)
        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,
        }
        content.update(vim_dict)

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

        return content, resp.status_code
    def post(self, request, vimid="", tenantid="", volumeid=""):
        logger.debug("Volumes--post::> %s" % request.data)
        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
                    pass
                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})
            resp = sess.post(req_resouce,
                             data=req_body,
                             endpoint_filter=self.service,
                             headers={
                                 "Content-Type": "application/json",
                                 "Accept": "application/json"
                             })
            resp_body = resp.json()["volume"]
            VimDriverUtils.replace_key_by_mapping(resp_body, self.keys_mapping)
            vim_dict = {
                "vimName": vim["name"],
                "vimId": vim["vimId"],
                "tenantId": tenantid,
                "returnCode": 1,
            }
            resp_body.update(vim_dict)
            return Response(data=resp_body, status=resp.status_code)
        except VimDriverKiloException 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)
        pass
Beispiel #6
0
    def create_flavor(self, sess, request):
        logger.debug("Flavors--create::> %s" % request.data)
        # 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})
        return sess.post(req_resouce,
                         data=req_body,
                         endpoint_filter=self.service)
        pass
    def get_servers(self, query="", vimid="", tenantid="", serverid=None):
        logger.debug("Servers--get_servers::> %s,%s" % (tenantid, serverid))

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

        vim = VimDriverUtils.get_vim_info(vimid)
        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,
        }
        content.update(vim_dict)

        if not serverid:
            # convert the key naming in servers
            for server in content["servers"]:
                metadata = server.pop("metadata", None)
                if metadata:
                    meta_data = []
                    self.convertMetadata(metadata, meta_data, True)
                    server["metadata"] = meta_data
                VimDriverUtils.replace_key_by_mapping(server,
                                                      self.keys_mapping)
                self.convert_resp(server)
                server["nicArray"] = self.get_ports(vimid, tenantid,
                                                    server["id"])

        else:
            # convert the key naming in the server specified by id
            server = content.pop("server", None)
            metadata = server.pop("metadata", None)
            if metadata:
                meta_data = []
                self.convertMetadata(metadata, meta_data, True)
                server["metadata"] = meta_data
            VimDriverUtils.replace_key_by_mapping(server, self.keys_mapping)
            self.convert_resp(server)
            server["nicArray"] = self.get_ports(vimid, tenantid, serverid)
            content.update(server)

        return content, resp.status_code
Beispiel #8
0
    def get(self, request, vimid=""):
        logger.debug("Tenants--get::> %s" % request.data)
        try:
            #prepare request resource to vim instance
            query = VimDriverUtils.get_query_part(request)

            vim = VimDriverUtils.get_vim_info(vimid)
            if '/v2' in vim["url"]:
                req_resouce = "/v2.0/tenants"
            elif '/v3' in vim["url"]:
                req_resouce = "/projects"
            else:
                req_resouce = "/projects"

            sess = VimDriverUtils.get_session(vim)
            resp = sess.get(req_resouce, endpoint_filter=self.service)
            content = resp.json()
            vim_dict = {
                "vimName": vim["name"],
                "vimId": vim["vimId"],
            }
            content.update(vim_dict)

            VimDriverUtils.replace_key_by_mapping(content, self.keys_mapping)

            if query:
                _, tenantname = query.split('=')
                if tenantname:
                    tmp = content["tenants"]
                    content["tenants"] = []
                    # convert the key naming in hosts
                    for tenant in tmp:
                        if tenantname == tenant['name']:
                            content["tenants"].append(tenant)

            return Response(data=content, 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)
    def get_ports(self, query="", vimid="", tenantid="", portid=""):
        logger.debug("Ports--get_ports::> %s" % 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)
            resp = sess.get(req_resouce, endpoint_filter=self.service)
            content = resp.json()
            vim_dict = {
                "vimName": vim["name"],
                "vimId": vim["vimId"],
                "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
Beispiel #10
0
    def get(self, request, vimid="", tenantid="", hostname=""):
        logger.debug("Hosts--get::> %s" % request.data)
        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)
            resp = sess.get(req_resouce, endpoint_filter=self.service)
            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'])

            return Response(data=content, 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)
    def create_flavor(self, sess, request):
        logger.debug("Flavors--create::> %s" % request.data)
        # 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.debug("SBI:%s" % req_body)
        return sess.post(req_resouce,
                         data=req_body,
                         endpoint_filter=self.service,
                         headers={
                             "Content-Type": "application/json",
                             "Accept": "application/json"
                         })
        pass
    def create_port(self, request, vimid, tenantid):
        logger.debug("Ports--create::> %s" % request.data)
        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:
                fixed_ip = {
                    "ip_address": tmpip,
                    "subnet_id": tmpsubnet,
                }
                port["fixed_ips"] = []
                port["fixed_ips"].append(fixed_ip)

            VimDriverUtils.replace_key_by_mapping(port, self.keys_mapping,
                                                  True)
            req_body = json.JSONEncoder().encode({"port": port})
            resp = sess.post(req_resouce,
                             data=req_body,
                             endpoint_filter=self.service)
            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"],
                "tenantId": tenantid,
                "returnCode": 1,
            }
            resp_body.update(vim_dict)
            return Response(data=resp_body, status=resp.status_code)
        return {}
    def post(self, request, vimid="", tenantid="", serverid=""):
        logger.debug("Servers--post::> %s" % request.data)
        try:
            # check if created already: check name
            servername = request.data["name"]
            query = "name=%s" % servername
            content, status_code = self.get_servers(query, vimid, tenantid)
            existed = False
            if status_code == 200:
                for server in content["servers"]:
                    if server["name"] == request.data["name"]:
                        existed = True
                        break
                    pass
                if existed == True and server:
                    vim_dict = {
                        "returnCode": 0,
                    }
                    server.update(vim_dict)
                    return Response(data=server, status=status_code)

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

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

            # convert parameters
            boot = server.pop("boot", None)
            if not boot:
                return Response(data={'error': "missing boot paramters"},
                                status=status.HTTP_500_INTERNAL_SERVER_ERROR)
            if boot["type"] == 1:
                # boot from volume
                server["block_device_mapping_v2"] = [{
                    "uuid": boot["volumeId"],
                    "source_type": "volume",
                    "destination_type": "volume",
                    "delete_on_termination": "false",
                    "boot_index": "0"
                }]
            else:
                # boot from image
                server["imageRef"] = boot["imageId"]

            nicarray = server.pop("nicArray", None)
            if not nicarray:
                return Response(data={'error': "missing nicArray paramters"},
                                status=status.HTTP_500_INTERNAL_SERVER_ERROR)
            else:
                networks = []
                for nic in nicarray:
                    networks.append({"port": nic["portId"]})
                if len(networks) > 0:
                    server["networks"] = networks

            meta_data = server.pop("metadata", None)
            if meta_data:
                metadata = {}
                self.convertMetadata(metadata, meta_data, False)
                server["metadata"] = metadata

            contextarray = server.pop("contextArray", None)
            if contextarray:
                # now set "contextArray" array
                personalities = []
                for context in contextarray:
                    personalities.append({
                        "path": context["fileName"],
                        "contents": context["fileData"]
                    })
                if len(personalities) > 0:
                    server["personality"] = personalities
                pass

            volumearray = server.pop("volumeArray", None)

            VimDriverUtils.replace_key_by_mapping(server, self.keys_mapping,
                                                  True)
            req_body = json.JSONEncoder().encode({"server": server})
            resp = sess.post(req_resouce,
                             data=req_body,
                             endpoint_filter=self.service,
                             headers={
                                 "Content-Type": "application/json",
                                 "Accept": "application/json"
                             })

            resp_body = resp.json().pop("server", None)

            logger.debug("Servers--post status::>%s, %s" %
                         (resp_body["id"], resp.status_code))
            if resp.status_code == 200 or resp.status_code == 201 or resp.status_code == 202:
                if volumearray and len(volumearray) > 0:
                    # server is created, now attach volumes
                    volumeIds = [
                        extraVolume["volumeId"] for extraVolume in volumearray
                    ]
                    self.attachVolume(vimid, tenantid, resp_body["id"],
                                      *volumeIds)
                pass
            pass

            metadata = resp_body.pop("metadata", None)
            if metadata:
                meta_data = []
                self.convertMetadata(metadata, meta_data, True)
                resp_body["metadata"] = meta_data

            VimDriverUtils.replace_key_by_mapping(resp_body, self.keys_mapping)
            vim_dict = {
                "vimName": vim["name"],
                "vimId": vim["vimId"],
                "tenantId": tenantid,
                "returnCode": 1,
            }
            resp_body.update(vim_dict)
            resp_body["boot"] = boot
            resp_body["volumeArray"] = volumearray
            resp_body["nicArray"] = nicarray
            resp_body["contextArray"] = contextarray
            resp_body["name"] = servername
            return Response(data=resp_body, status=resp.status_code)
        except VimDriverKiloException 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)
        pass
    def get(self, request, vimid="", tenantid="", flavorid=""):
        logger.debug("Flavors--get::> %s" % request.data)
        try:
            # prepare request resource to vim instance
            query = VimDriverUtils.get_query_part(request)

            vim = VimDriverUtils.get_vim_info(vimid)
            sess = VimDriverUtils.get_session(vim, tenantid)
            resp = self.get_flavor(sess, request, flavorid)
            content = resp.json()

            if flavorid:
                flavor = content.pop("flavor", None)
                extraResp = self.get_flavor_extra_specs(sess, flavor["id"])
                extraContent = extraResp.json()
                if extraContent["extra_specs"]:
                    extraSpecs = []
                    self.convertExtraSpecs(extraSpecs,
                                           extraContent["extra_specs"], True)
                    flavor["extraSpecs"] = extraSpecs
                VimDriverUtils.replace_key_by_mapping(flavor,
                                                      self.keys_mapping)
                content.update(flavor)

            else:
                wanted = None
                #check if query contains name="flavorname"
                if query:
                    for queryone in query.split('&'):
                        k, v = queryone.split('=')
                        if k == "name":
                            wanted = v
                            break
                        pass

                if wanted:
                    oldFlavors = content.pop("flavors", None)
                    content["flavors"] = []
                    for flavor in oldFlavors:
                        if wanted == flavor["name"]:
                            content["flavors"].append(flavor)
                        pass

                #iterate each flavor to get extra_specs
                for flavor in content["flavors"]:
                    extraResp = self.get_flavor_extra_specs(sess, flavor["id"])
                    extraContent = extraResp.json()
                    if extraContent["extra_specs"]:
                        extraSpecs = []
                        self.convertExtraSpecs(extraSpecs,
                                               extraContent["extra_specs"],
                                               True)
                        flavor["extraSpecs"] = extraSpecs
                    VimDriverUtils.replace_key_by_mapping(
                        flavor, self.keys_mapping)

            #add extra keys
            vim_dict = {
                "vimName": vim["name"],
                "vimId": vim["vimId"],
                "tenantId": tenantid,
            }
            content.update(vim_dict)

            return Response(data=content, status=resp.status_code)
        except VimDriverKiloException 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)
        pass
    def post(self, request, vimid="", tenantid="", flavorid=""):
        logger.debug("Flavors--post::> %s" % request.data)
        sess = None
        resp = None
        resp_body = None
        try:
            # prepare request resource to vim instance
            vim = VimDriverUtils.get_vim_info(vimid)
            sess = VimDriverUtils.get_session(vim, tenantid)

            #check if the flavor is already created: name or id
            tmpresp = self.get_flavor(sess, request)
            content = tmpresp.json()
            #iterate each flavor to get extra_specs
            existed = False
            for flavor in content["flavors"]:
                if flavor["name"] == request.data["name"]:
                    existed = True
                    break
                elif hasattr(request.data,
                             "id") and flavor["id"] == request.data["id"]:
                    existed = True
                    break
                pass

            if existed == True:
                extraResp = self.get_flavor_extra_specs(sess, flavor["id"])
                extraContent = extraResp.json()
                if extraContent["extra_specs"]:
                    extraSpecs = []
                    self.convertExtraSpecs(extraSpecs,
                                           extraContent["extra_specs"], True)
                    flavor["extraSpecs"] = extraSpecs
                VimDriverUtils.replace_key_by_mapping(flavor,
                                                      self.keys_mapping)
                vim_dict = {
                    "vimName": vim["name"],
                    "vimId": vim["vimId"],
                    "tenantId": tenantid,
                    "returnCode": 0,
                }
                flavor.update(vim_dict)
                return Response(data=flavor, status=tmpresp.status_code)

            extraSpecs = request.data.pop("extraSpecs", None)
            #create flavor first
            resp = self.create_flavor(sess, request)
            if resp.status_code == 200:
                resp_body = resp.json()["flavor"]
            else:
                return resp

            flavorid = resp_body['id']
            if extraSpecs:
                extra_specs = {}
                self.convertExtraSpecs(extraSpecs, extra_specs, False)
                #                logger.debug("extraSpecs:%s" % extraSpecs)
                #                logger.debug("extra_specs:%s" % extra_specs)
                extraResp = self.create_flavor_extra_specs(
                    sess, extra_specs, flavorid)
                if extraResp.status_code == 200:
                    #combine the response body and return
                    tmpSpecs = []
                    tmpRespBody = extraResp.json()
                    self.convertExtraSpecs(tmpSpecs,
                                           tmpRespBody['extra_specs'], True)

                    resp_body.update({"extraSpecs": tmpSpecs})
                else:
                    #rollback
                    delete_flavor(self, request, vimid, tenantid, flavorid)
                    return extraResp

            VimDriverUtils.replace_key_by_mapping(resp_body, self.keys_mapping)
            vim_dict = {
                "vimName": vim["name"],
                "vimId": vim["vimId"],
                "tenantId": tenantid,
                "returnCode": 1,
            }
            resp_body.update(vim_dict)
            return Response(data=resp_body, status=resp.status_code)
        except VimDriverKiloException as e:
            if sess and resp and resp.status_code == 200:
                self.delete_flavor(sess, flavorid)

            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())

            if sess and resp and resp.status_code == 200:
                self.delete_flavor(sess, flavorid)

            return Response(data={'error': str(e)},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
        pass
    def post(self, request, vimid="", tenantid="", imageid=""):
        logger.debug("Images--post::> %s" % request.data)
        try:
            #check if created already: check name
            query = "name=%s" % request.data["name"]
            content, status_code = self.get_images(query, vimid, tenantid)
            existed = False
            if status_code == 200:
                for image in content["images"]:
                    if image["name"] == request.data["name"]:
                        existed = True
                        break
                    pass
                if existed == True:
                    vim_dict = {
                        "returnCode": 0,
                    }
                    image.update(vim_dict)
                    return Response(data=image, status=status_code)

            imageurl = request.data.pop("imagePath", None)
            imagefd = None
            if not imageurl:
                return Response(data={'error': 'imagePath is not specified'},
                                status=500)

            #valid image url
            imagefd = urllib2.urlopen(imageurl)
            if not imagefd:
                logger.debug("image is not available at %s" % imageurl)
                return Response(
                    data={'error': 'cannot access to specified imagePath'},
                    status=500)

                # prepare request resource to vim instance
            req_resouce = "v2/images"

            vim = VimDriverUtils.get_vim_info(vimid)
            sess = VimDriverUtils.get_session(vim, tenantid)
            image = request.data
            VimDriverUtils.replace_key_by_mapping(image, self.keys_mapping,
                                                  True)
            #req_body = json.JSONEncoder().encode({"image": image})
            req_body = json.JSONEncoder().encode(image)
            resp = sess.post(req_resouce,
                             data=req_body,
                             endpoint_filter=self.service)
            #resp_body = resp.json()["image"]
            resp_body = resp.json()
            VimDriverUtils.replace_key_by_mapping(resp_body, self.keys_mapping)
            vim_dict = {
                "vimName": vim["name"],
                "vimId": vim["vimId"],
                "tenantId": tenantid,
                "returnCode": 1,
            }
            resp_body.update(vim_dict)

            #launch a thread to download image and upload to VIM
            if resp.status_code == 201:
                imageid = resp_body["id"]
                logger.debug("launch thread to upload image: %s" % imageid)
                tmp_thread = imageThread(vimid, tenantid, imageid, imagefd)
                running_thread_lock.acquire()
                running_threads[imageid] = tmp_thread
                running_thread_lock.release()
                tmp_thread.start()
            else:
                logger.debug("resp.status_code: %s" % resp.status_code)
                pass

            return Response(data=resp_body, status=resp.status_code)
        except VimDriverKiloException as e:
            return Response(data={'error': e.content}, status=e.status_code)
        except urllib2.URLError as e:
            return Response(
                data={'error': 'image is not accessible:%s' % str(e)},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR)
        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)
        pass