summaryrefslogtreecommitdiffstats
path: root/newton
diff options
context:
space:
mode:
authorBin Yang <bin.yang@windriver.com>2017-03-03 16:16:04 +0800
committerBin Yang <bin.yang@windriver.com>2017-03-03 16:16:04 +0800
commite0010886c07924786c30fa5a55675dba4165b58d (patch)
tree5a7cd2459eaf901987378fecfa1092fddf594d69 /newton
parent55486a802cf6c8eabdc2086dccaa32b0342d8786 (diff)
Refactor virtual ports API for newton
Change-Id: I314f09c3aa4570d1df6df14e456ba4132af63320 Issue-Id: MULTIVIM-22 Signed-off-by: Bin Yang <bin.yang@windriver.com>
Diffstat (limited to 'newton')
-rw-r--r--newton/newton/requests/views/vport.py104
1 files changed, 70 insertions, 34 deletions
diff --git a/newton/newton/requests/views/vport.py b/newton/newton/requests/views/vport.py
index e4492c61..750726ea 100644
--- a/newton/newton/requests/views/vport.py
+++ b/newton/newton/requests/views/vport.py
@@ -43,6 +43,23 @@ class Vports(APIView):
logger.debug("Ports--get::> %s" % request.data)
try:
# prepare request resource to vim instance
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+
+ content, status_code = self.get_ports(sess, request, vim, tenantid, portid)
+
+ return Response(data=content, status=status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except Exception as e:
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+
+ def get_ports(self, sess, request, vim, tenantid, portid=""):
+ logger.debug("Ports--get_ports::> %s" % portid)
+ if sess:
+ # prepare request resource to vim instance
req_resouce = "v2.0/ports"
if portid:
req_resouce += "/%s" % portid
@@ -50,9 +67,6 @@ class Vports(APIView):
query = VimDriverUtils.get_query_part(request)
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 = {
@@ -65,50 +79,75 @@ class Vports(APIView):
if not portid:
# convert the key naming in ports
for port in content["ports"]:
- #use only 1 fixed_ip
- port.update(port["fixed_ips"][0])
- port.pop("fixed_ips", None)
+ # use only 1st entry of fixed_ips
+ if port:
+ tmpips = port.pop("fixed_ips", None)
+ port.update(tmpips[0])
VimDriverUtils.replace_key_by_mapping(port,
self.keys_mapping)
else:
# convert the key naming in the port specified by id
- tmp = content["port"]
- content.pop("port", None)
- #use only 1 fixed_ip
- tmp.update(tmp["fixed_ips"][0])
- tmp.pop("fixed_ips", None)
- VimDriverUtils.replace_key_by_mapping(tmp,
+ port = content.pop("port", None)
+ #use only 1st entry of fixed_ips
+ if port:
+ tmpips = port.pop("fixed_ips", None)
+ port.update(tmpips[0])
+
+ VimDriverUtils.replace_key_by_mapping(port,
self.keys_mapping)
- content.update(tmp)
+ content.update(port)
+ return content, resp.status_code
+ return {}, 500
- return Response(data=content, status=resp.status_code)
+ def post(self, request, vimid="", tenantid="", portid=""):
+ logger.debug("Ports--post::> %s" % request.data)
+ try:
+ # prepare request resource to vim instance
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+
+ #check if already created: name
+ content, status_code = self.get_ports(sess, request, vim, tenantid)
+ existed = False
+ if status_code == 200:
+ for port in content["ports"]:
+ if port["name"] == request.data["name"]:
+ existed = True
+ break
+ pass
+ if existed == True:
+ vim_dict = {
+ "returnCode": 0,
+ }
+ port.update(vim_dict)
+ return Response(data=port, status=status_code)
+
+ #otherwise create a new one
+ return self.create_port(sess, request, vim, tenantid)
except VimDriverNewtonException as e:
return Response(data={'error': e.content}, status=e.status_code)
except Exception as e:
return Response(data={'error': str(e)},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
- def post(self, request, vimid="", tenantid="", portid=""):
- logger.debug("Ports--post::> %s" % request.data)
- try:
+ def create_port(self, sess, request, vim, tenantid):
+ logger.debug("Ports--create::> %s" % request.data)
+ if sess:
# prepare request resource to vim instance
req_resouce = "v2.0/ports"
- vim = VimDriverUtils.get_vim_info(vimid)
- sess = VimDriverUtils.get_session(vim, tenantid)
port = request.data
#handle ip and subnetId
- if port["ip"] and port["subnetId"]:
+ tmpip = port.pop("ip", None)
+ tmpsubnet = port.pop("subnetId", None)
+ if tmpip and tmpsubnet:
fixed_ip = {
- "ip_address": port["ip"],
- "subnet_id": port["subnetId"],
+ "ip_address": tmpip,
+ "subnet_id": tmpsubnet,
}
port["fixed_ips"] = []
port["fixed_ips"].append(fixed_ip)
- port.pop("ip", None)
- port.pop("subnetId", None)
-
VimDriverUtils.replace_key_by_mapping(port,
self.keys_mapping, True)
req_body = json.JSONEncoder().encode({"port": port})
@@ -116,23 +155,20 @@ class Vports(APIView):
endpoint_filter=self.service)
resp_body = resp.json()["port"]
#use only 1 fixed_ip
- tmp = resp_body
- tmp.update(tmp["fixed_ips"][0])
- tmp.pop("fixed_ips", None)
+ tmpips = resp_body.pop("fixed_ips", None)
+ if tmpips:
+ 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)
- except VimDriverNewtonException as e:
- return Response(data={'error': e.content}, status=e.status_code)
- except Exception as e:
- return Response(data={'error': str(e)},
- status=status.HTTP_500_INTERNAL_SERVER_ERROR)
- pass
+ return {}
def delete(self, request, vimid="", tenantid="", portid=""):
logger.debug("Ports--delete::> %s" % request.data)