diff options
author | Bin Yang <bin.yang@windriver.com> | 2017-03-03 17:24:37 +0800 |
---|---|---|
committer | Bin Yang <bin.yang@windriver.com> | 2017-03-03 17:24:37 +0800 |
commit | b41ab02979b62317b187a278c26ef387345ed50d (patch) | |
tree | 423c522261adb2486409c055d06055e5116e1405 /newton | |
parent | 1bcc306e61c5eef31f9f222e85c4b414331a5bd0 (diff) |
Refactor Networks API for newton
Change-Id: I8bfee93e796dc14311ea38c97a0cc6ba7dd89515
Issue-Id: MULTIVIM-22
Signed-off-by: Bin Yang <bin.yang@windriver.com>
Diffstat (limited to 'newton')
-rw-r--r-- | newton/newton/requests/views/network.py | 84 |
1 files changed, 55 insertions, 29 deletions
diff --git a/newton/newton/requests/views/network.py b/newton/newton/requests/views/network.py index 9d91afc3..ad17d398 100644 --- a/newton/newton/requests/views/network.py +++ b/newton/newton/requests/views/network.py @@ -41,45 +41,70 @@ class Networks(APIView): def get(self, request, vimid="", tenantid="", networkid=""): logger.debug("Networks--get::> %s" % request.data) try: - # prepare request resource to vim instance - req_resouce = "v2.0/networks" - if networkid: - req_resouce += "/%s" % networkid - 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 = { - "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 - VimDriverUtils.replace_key_by_mapping(content["network"], - self.keys_mapping) + content, status_code = self.get_networks(request, vimid, tenantid, networkid) + return Response(data=content, status=status_code) - return Response(data=content, 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) + def get_networks(self, request, 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 + 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 = { + "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 post(self, request, vimid="", tenantid="", networkid=""): logger.debug("Networks--post::> %s" % request.data) try: + #check if created already: check name + content, status_code = self.get_networks(request, vimid, tenantid) + existed = False + if status_code == 200: + for network in content["networks"]: + if network["name"] == request.data["name"]: + existed = True + break + pass + if existed == True: + vim_dict = { + "returnCode": 0, + } + network.update(vim_dict) + return Response(data=network, status=status_code) + # prepare request resource to vim instance req_resouce = "v2.0/networks" if networkid: @@ -102,6 +127,7 @@ class Networks(APIView): "vimName": vim["name"], "vimId": vim["vimId"], "tenantId": tenantid, + "returnCode": 1, } resp_body.update(vim_dict) return Response(data=resp_body, status=resp.status_code) |