Implement infra_workload delete API 47/67547/1
authorXiaohua Zhang <xiaohua.zhang@windriver.com>
Wed, 19 Sep 2018 03:12:51 +0000 (03:12 +0000)
committerXiaohua Zhang <xiaohua.zhang@windriver.com>
Wed, 19 Sep 2018 03:12:51 +0000 (03:12 +0000)
Change-Id: I16f198e44810278eafb5be08ed88268911554f57
Issue-ID: MULTICLOUD-358
Signed-off-by: Xiaohua Zhang <xiaohua.zhang@windriver.com>
share/common/msapi/helper.py
share/common/utils/restcall.py
windriver/titanium_cloud/resource/tests/tests_infra_workload.py
windriver/titanium_cloud/resource/views/infra_workload.py

index 3ec404e..3e10c0f 100644 (file)
@@ -16,6 +16,7 @@ import re
 from common.exceptions import VimDriverNewtonException
 from common.utils import restcall
 
+from rest_framework import status
 
 logger = logging.getLogger(__name__)
 
@@ -30,12 +31,9 @@ class Helper(object):
                                                   f_uri=uri)
         extra_headers = header
         ret = restcall._call_req(multicloud_api_prefix, "", "", 0, auth_api_url, "POST", extra_headers, json.dumps(data))
-        if ret[0] > 0 or ret[1] is None:
-            logger.critical("call url %s failed with status %s" % (multicloud_api_prefix+auth_api_url, ret[0]))
-            return ret
-
-        resp = json.JSONDecoder().decode(ret[1])
-        ret[1] = resp
+        if ret[0] == 0 and ret[1]:
+            content = json.JSONDecoder().decode(ret[1])
+            ret[1] = content
         return ret
 
     # The consumer of this api must be attaching to the same management network of multicloud,
@@ -51,11 +49,8 @@ class Helper(object):
                 endpoint_url = catalog['endpoints'][0]['publicURL']
                 extra_headers = {'X-Auth-Token': token}
                 ret = restcall._call_req(endpoint_url, "", "", 0, uri, method, extra_headers, json.dumps(data) if data else "")
-                if ret[0] > 0 or ret[1] is None:
-                    logger.critical("call url %s failed with status %s" % (endpoint_url+uri, ret[0]))
-                    return ret
-
-                content = json.JSONDecoder().decode(ret[1])
-                ret[1] = content
+                if ret[0] == 0 and ret[1]:
+                    content = json.JSONDecoder().decode(ret[1])
+                    ret[1] = content
                 return ret
-            pass
+        return [1, None, status.HTTP_404_NOT_FOUND] # return resource not found in case no type found
\ No newline at end of file
index 05788bc..ec3abb3 100644 (file)
@@ -80,7 +80,7 @@ def _call_req(base_url, user, passwd, auth_type,
                                                   headers=headers)
                 resp_status, resp_body = \
                     resp['status'], codecs.decode(
-                        resp_content, 'UTF-8')
+                        resp_content, 'UTF-8') if resp_content else None
                 if resp_status in status_ok_list:
                     ret = [0, resp_body, resp_status]
                 else:
index 05a9d8d..8914728 100644 (file)
@@ -102,7 +102,7 @@ MOCK_HEAT_CREATE_RESPONSE1 = {
 MOCK_HEAT_LIST_RESPONSE1 = {
     'stacks': [
         {
-            'resource_status':"CREATE_IN_PROCESS"
+            'stack_status':"CREATE_IN_PROCESS"
         }
     ]
 }
index 5ce3e23..100496f 100644 (file)
@@ -141,21 +141,21 @@ class InfraWorkload(APIView):
             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")
-            resources = content.get('stacks', []) if retcode > 0 and content else []
-
-            resource_status = resources[0]["resource_status"] if len(resources)>0 else ""
+            stacks = content.get('stacks', []) if retcode == 0 and content else []
+            stack_status = stacks[0]["stack_status"] if len(stacks) > 0 else ""
 
             # stub response
             resp_template = {
                 "template_type": "HEAT",
                 "workload_id": stack_id,
-                "workload_status": resource_status
+                "workload_status": stack_status
             }
 
             if retcode > 0:
+                # return error messsages
                 resp_template['workload_response'] = content
 
-            if ('CREATE_COMPLETE' == resource_status):
+            if ('CREATE_COMPLETE' == stack_status):
                 self.heatbridge_update(request, vimid, stack_id)
 
             self._logger.info("RESP with data> result:%s" % resp_template)
@@ -177,11 +177,53 @@ class InfraWorkload(APIView):
         self._logger.debug("META: %s" % request.META)
 
         try :
+            # 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
-            self._logger.info("RESP with data> result:%s" % "")
-            return Response(status=status.HTTP_204_NO_CONTENT)
+            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))
@@ -223,7 +265,7 @@ class InfraWorkload(APIView):
         resource_uri = "/stacks/%s/resources"%(stack_id)
         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")
-        resources = content.get('resources', []) if retcode==0 and content else []
+        resources = content.get('resources', []) if retcode == 0 and content else []
 
         #find and update resources
         transactions = []
@@ -328,7 +370,7 @@ class InfraWorkload(APIView):
 
         return  aai_transactions
 
-    def heatbridge_delete(self, request, stack_id, vimid, tenant_id):
+    def heatbridge_delete(self, request, stack_id, vimid):
         '''
         remove heat resource from AAI for the specified cloud region and tenant
         The resources includes: vserver, vserver/l-interface,