summaryrefslogtreecommitdiffstats
path: root/windriver/titanium_cloud/resource
diff options
context:
space:
mode:
authorBin Yang <bin.yang@windriver.com>2018-09-15 18:25:10 +0000
committerBin Yang <bin.yang@windriver.com>2018-09-15 18:25:10 +0000
commitfb6c49e4131e44e145ab60fce7dc355d69763158 (patch)
tree334a069feaaa3cf287a04d4a6fd4c48de43b43be /windriver/titanium_cloud/resource
parent099edc8b91480006181e4e9644c6cbbb5e162bb9 (diff)
Add heatbridge handler
Embedded the heatbridge handler into GET API Add multicloud api helper Change-Id: I96bd5e555dbcaa08d5ac5ac132c004a690f2e41d Issue-ID: MULTICLOUD-264 Signed-off-by: Bin Yang <bin.yang@windriver.com>
Diffstat (limited to 'windriver/titanium_cloud/resource')
-rw-r--r--windriver/titanium_cloud/resource/views/infra_workload.py174
1 files changed, 161 insertions, 13 deletions
diff --git a/windriver/titanium_cloud/resource/views/infra_workload.py b/windriver/titanium_cloud/resource/views/infra_workload.py
index 08efcfd1..dce66dec 100644
--- a/windriver/titanium_cloud/resource/views/infra_workload.py
+++ b/windriver/titanium_cloud/resource/views/infra_workload.py
@@ -27,7 +27,9 @@ from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from common.msapi import extsys
+from common.msapi.helper import Helper as helper
+from common.utils import restcall
logger = logging.getLogger(__name__)
@@ -37,8 +39,9 @@ class InfraWorkload(APIView):
def __init__(self):
self._logger = logger
- def post(self, request, vimid=""):
- self._logger.info("vimid, data: %s, %s" % (vimid, request.data))
+ def post(self, request, vimid="", requri=""):
+ self._logger.info("vimid,requri: %s, %s" % (vimid, requri))
+ self._logger.info("data: %s, %s" % (request.data))
self._logger.debug("META: %s" % request.META)
try :
@@ -76,17 +79,19 @@ class InfraWorkload(APIView):
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
- def get(self, request, vimid=""):
- self._logger.info("vimid: %s" % (vimid))
+ def get(self, request, vimid="", requri=""):
+ self._logger.info("vimid,requri: %s, %s" % (vimid, requri))
self._logger.debug("META: %s" % request.META)
try :
-
+ stack_id = requri
+ workload_data = self.heatbridge_update(request, vimid, stack_id)
# stub response
resp_template = {
"template_type": "heat",
"workload_id": "3095aefc-09fb-4bc7-b1f0-f21a304e864c",
"workload_status": "CREATE_IN_PROCESS",
+ 'workload_data': workload_data
}
self._logger.info("RESP with data> result:%s" % resp_template)
@@ -103,11 +108,12 @@ class InfraWorkload(APIView):
return Response(data={'error': str(e)},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
- def delete(self, request, vimid=""):
- self._logger.info("vimid: %s" % (vimid))
+ def delete(self, request, vimid="", requri=""):
+ self._logger.info("vimid,requri: %s, %s" % (vimid,requri))
self._logger.debug("META: %s" % request.META)
try :
+ stack_id = requri
# stub response
self._logger.info("RESP with data> result:%s" % "")
@@ -124,6 +130,148 @@ class InfraWorkload(APIView):
return Response(data={'error': str(e)},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ def heatbridge_update(self, request, vimid, stack_id):
+ '''
+ update heat resource to AAI for the specified cloud region and tenant
+ The resources includes: vserver, vserver/l-interface,
+ :param request:
+ :param vimid:
+ :param stack_id:
+ :return:
+ '''
+
+ cloud_owner, regionid = extsys.decode_vim_id(vimid)
+ # should go via multicloud proxy so that the selflink is updated by multicloud
+ v2_token_resp_json = helper.MultiCloudIdentityHelper(settings.MULTICLOUD_API_V1_PREFIX,
+ cloud_owner, regionid, "/v2.0/tokens")
+ if not v2_token_resp_json:
+ logger.error("authenticate fails:%s,%s" % (cloud_owner, regionid))
+ return
+ tenant_id = v2_token_resp_json["access"]["token"]["tenant"]["id"]
+ # tenant_name = v2_token_resp_json["access"]["token"]["tenant"]["name"]
+
+ # common prefix
+ aai_cloud_region = "/cloud-infrastructure/cloud-regions/cloud-region/%s/%s/tenants/tenant/%s" \
+ % (cloud_owner, regionid, tenant_id)
+
+ # get stack resource
+ service_type = "orchestration"
+ resource_uri = "/stacks/%s/resources"%(stack_id)
+ self._logger.info("retrieve stack resources, URI:%s" % resource_uri)
+ content = helper.MultiCloudServiceHelper(cloud_owner, regionid, v2_token_resp_json, service_type, resource_uri, None, "GET")
+ resources = content.get('resources', []) if content else []
+
+ #find and update resources
+ transactions = []
+ for resource in resources:
+ if resource.get('resource_type', None) == 'OS::Nova::Server':
+ # retrieve vserver details
+ service_type = "compute"
+ resource_uri = "/servers/%s" % (resource['physical_resource_id'])
+ self._logger.info("retrieve vserver detail, URI:%s" % resource_uri)
+ content = helper.MultiCloudServiceHelper(cloud_owner, regionid, v2_token_resp_json, service_type,
+ resource_uri, None, "GET")
+ self._logger.debug(" resp data:%s" % content)
+ vserver_detail = content.get('server', None) if content else None
+ if vserver_detail:
+ # compose inventory entry for vserver
+ vserver_link = ""
+ for link in vserver_detail['links']:
+ if link['rel'] == 'self':
+ vserver_link = link['href']
+ break
+ pass
+
+ # note: relationship-list to flavor/image is not be update yet
+ # note: volumes is not updated yet
+ # note: relationship-list to vnf will be handled somewhere else
+ aai_resource = {
+ 'body': {
+ 'vserver-name': vserver_detail['name'],
+ 'vserver-name2': vserver_detail['name'],
+ "vserver-id": vserver_detail['id'],
+ "vserver-selflink": vserver_link,
+ "prov-status": vserver_detail['status']
+ },
+ "uri": aai_cloud_region + "/vservers/vserver/%s"\
+ % ( vserver_detail['id'])}
+
+ try:
+ # then update the resource
+ retcode, content, status_code = \
+ restcall.req_to_aai(aai_resource['uri'], "PUT", content=aai_resource['body'])
+
+ if retcode == 0 and content:
+ content = json.JSONDecoder().decode(content)
+ self._logger.debug("AAI update %s response: %s" % (aai_resource['uri'], content))
+ except Exception as e:
+ self._logger.error(traceback.format_exc())
+ pass
+
+ aai_resource_transactions = {"put": [aai_resource]}
+ transactions.append(aai_resource_transactions)
+ #self._logger.debug("aai_resource :%s" % aai_resource_transactions)
+ pass
+
+ for resource in resources:
+ if resource.get('resource_type', None) == 'OS::Neutron::Port':
+ # retrieve vserver details
+ service_type = "network"
+ resource_uri = "/v2.0/ports/%s" % (resource['physical_resource_id'])
+ self._logger.info("retrieve vserver detail, URI:%s" % resource_uri)
+ content = helper.MultiCloudServiceHelper(cloud_owner, regionid, v2_token_resp_json, service_type,
+ resource_uri, None, "GET")
+ self._logger.debug(" resp data:%s" % content)
+
+ vport_detail = content.get('port', None)
+ if vport_detail:
+ # compose inventory entry for vport
+ # note: l3-interface-ipv4-address-list, l3-interface-ipv6-address-list are not updated yet
+ # note: network-name is not update yet since the detail coming with network-id
+ aai_resource = {
+ "body": {
+ "interface-name": vport_detail['name'],
+ "interface-id": vport_detail['id'],
+ "macaddr": vport_detail['mac_address']
+ },
+ 'uri': aai_cloud_region + "/vservers/vserver/%s/l-interfaces/l-interface/%s" \
+ % (vport_detail['device_id'], vport_detail['name'])
+ }
+ try:
+ # then update the resource
+ retcode, content, status_code = \
+ restcall.req_to_aai(aai_resource['uri'], "PUT", content=aai_resource['body'])
+
+ if retcode == 0 and content:
+ content = json.JSONDecoder().decode(content)
+ self._logger.debug("AAI update %s response: %s" % (aai_resource['uri'], content))
+ except Exception as e:
+ self._logger.error(traceback.format_exc())
+ pass
+
+ aai_resource_transactions = {"put": [aai_resource]}
+ transactions.append(aai_resource_transactions)
+ # self._logger.debug("aai_resource :%s" % aai_resource_transactions)
+
+ pass
+
+ aai_transactions = {"transactions": transactions}
+ self._logger.debug("aai_transactions :%s" % aai_transactions)
+
+ return aai_transactions
+
+ def heatbridge_delete(self, request, stack_id, vimid, tenant_id):
+ '''
+ remove heat resource from AAI for the specified cloud region and tenant
+ The resources includes: vserver, vserver/l-interface,
+ :param request:
+ :param stack_id:
+ :param vimid:
+ :param tenant_id:
+ :return:
+ '''
+ pass
+
class APIv1InfraWorkload(InfraWorkload):
@@ -131,23 +279,23 @@ class APIv1InfraWorkload(InfraWorkload):
super(APIv1InfraWorkload, self).__init__()
# self._logger = logger
- def post(self, request, cloud_owner="", cloud_region_id=""):
+ def post(self, request, cloud_owner="", cloud_region_id="", requri=""):
#self._logger.info("cloud owner, cloud region id, data: %s,%s, %s" % (cloud_owner, cloud_region_id, request.data))
#self._logger.debug("META: %s" % request.META)
vimid = extsys.encode_vim_id(cloud_owner, cloud_region_id)
- return super(APIv1InfraWorkload, self).post(request, vimid)
+ return super(APIv1InfraWorkload, self).post(request, vimid, requri)
- def get(self, request, cloud_owner="", cloud_region_id=""):
+ def get(self, request, cloud_owner="", cloud_region_id="", requri=""):
#self._logger.info("cloud owner, cloud region id, data: %s,%s, %s" % (cloud_owner, cloud_region_id, request.data))
#self._logger.debug("META: %s" % request.META)
vimid = extsys.encode_vim_id(cloud_owner, cloud_region_id)
- return super(APIv1InfraWorkload, self).get(request, vimid)
+ return super(APIv1InfraWorkload, self).get(request, vimid, requri)
- def delete(self, request, cloud_owner="", cloud_region_id=""):
+ def delete(self, request, cloud_owner="", cloud_region_id="", requri=""):
#self._logger.info("cloud owner, cloud region id, data: %s,%s, %s" % (cloud_owner, cloud_region_id, request.data))
#self._logger.debug("META: %s" % request.META)
vimid = extsys.encode_vim_id(cloud_owner, cloud_region_id)
- return super(APIv1InfraWorkload, self).delete(request, vimid)
+ return super(APIv1InfraWorkload, self).delete(request, vimid, requri)