Add post handler of flavor handler for infra 97/66097/4
authorHuang Haibin <haibin.huang@intel.com>
Wed, 12 Sep 2018 12:05:21 +0000 (20:05 +0800)
committerHuang Haibin <haibin.huang@intel.com>
Sat, 15 Sep 2018 00:42:15 +0000 (08:42 +0800)
Change-Id: Ic140ba963c30a86c9755b0b1d0d0f813bb0ab2f0
Issue-ID: MULTICLOUD-250
Signed-off-by: Huang Haibin <haibin.huang@intel.com>
pike/pike/resource/views/infra_workload.py

index 9ef249d..20ad67d 100644 (file)
@@ -39,24 +39,60 @@ class InfraWorkload(APIView):
         self._logger.debug("META: %s" % request.META)
 
         try :
-
-            # stub response
-            resp_template = {
-                "template_type": "heat",
-                "workload_id": "3095aefc-09fb-4bc7-b1f0-f21a304e864c",
-                "template_response":
-                {
-                    "stack": {
-                        "id": "3095aefc-09fb-4bc7-b1f0-f21a304e864c",
-                        "links": [
-                            {
-                                "href": "http://192.168.123.200:8004/v1/eb1c63a4f77141548385f113a28f0f52/stacks/teststack/3095aefc-09fb-4bc7-b1f0-f21a304e864c",
-                                "rel": "self"
-                            }
-                        ]
-                    }
+            vim = VimDriverUtils.get_vim_info(vimid)
+            cloud_owner, regionid = extsys.decode_vim_id(vimid)
+
+            data = request.data
+            oof_directive = data["oof_directive"]
+            template_type = data["template_type"]
+            template_data = data["template_data"]
+
+            resp_template = None
+            if "heat" == template_type:
+                tenant_name = None
+                interface = 'public'
+                service = {'service_type': 'orchestration',
+                           'interface': interface,
+                           'region_id': vim['openstack_region_id']
+                               if vim.get('openstack_region_id')
+                                else vim['cloud_region_id']}
+
+                if template_data.has_key("parameters"):
+                    paramters = template_data["parameters"]
+                else:
+                    self._logger.error("we can't find parameters in heat template")
+                    return Response(data=None, status=HTTP_400_BADREQUEST)
+
+                for directive in template_data["directives"]:
+                    if directive["type"] == "vnfc":
+                        for directive2 in directive["directives"]:
+                            if directive2["type"] == flavor_directive:
+                                flavor_label = directive2[0]["attribute_name"]
+                                flavor_value = directive2[0]["attribute_value"]
+                                if parameters.has_key(flavor_label):
+                                    template_data["parameters"][flavor_label] = flavor_value
+                                else:
+                                    self._logger.warn("we can't find the flavor_label: %s" % 
+                                                        flavor_label)
+
+                req_body = template_data
+                sess = VimDriverUtils.get_session(vim, tenant_name)
+                resp = sess.post(req_resource,
+                                 data = req_body,
+                                 endpoint_filter = service)
+
+                resp_template = {
+                    "template_type": template_type,
+                    "workload_id": resp["stack"]["id"],
+                    "template_response": resp
                 }
-            }
+
+            elif "tosca" == template_type:
+                #TODO
+                self._logger.info("TBD")
+            else:
+                self._logger.warn("This template type is not supported")
+
 
             self._logger.info("RESP with data> result:%s" % resp_template)
             return Response(data=resp_template, status=status.HTTP_201_CREATED)