diff options
author | Bin Yang <bin.yang@windriver.com> | 2018-09-15 18:25:10 +0000 |
---|---|---|
committer | Bin Yang <bin.yang@windriver.com> | 2018-09-15 18:25:10 +0000 |
commit | fb6c49e4131e44e145ab60fce7dc355d69763158 (patch) | |
tree | 334a069feaaa3cf287a04d4a6fd4c48de43b43be | |
parent | 099edc8b91480006181e4e9644c6cbbb5e162bb9 (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>
-rw-r--r-- | share/common/msapi/helper.py | 59 | ||||
-rw-r--r-- | windriver/titanium_cloud/resource/views/infra_workload.py | 174 | ||||
-rw-r--r-- | windriver/titanium_cloud/urls.py | 6 |
3 files changed, 224 insertions, 15 deletions
diff --git a/share/common/msapi/helper.py b/share/common/msapi/helper.py new file mode 100644 index 00000000..0c27990b --- /dev/null +++ b/share/common/msapi/helper.py @@ -0,0 +1,59 @@ +# Copyright (c) 2017-2018 Wind River Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +import json +import logging +import re + +from common.exceptions import VimDriverNewtonException +from common.utils import restcall + + +logger = logging.getLogger(__name__) + + +class Helper(object): + + @staticmethod + def MultiCloudIdentityHelper(multicloud_api_prefix, cloud_owner, cloud_region_id, uri, data={}, header=''): + auth_api_url_format = "/{f_cloudowner}/{f_cloudregionid}/identity{f_uri}" + auth_api_url = auth_api_url_format.format(f_cloudowner=cloud_owner, + f_cloudregionid=cloud_region_id, + 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 None + + resp = json.JSONDecoder().decode(ret[1]) + return resp + + # The consumer of this api must be attaching to the same management network of multicloud, + # The constraints comes from the returned catalog endpoint url e.g. "http://10.0.14.1:80/api/multicloud-titaniumcloud/v0/pod25_RegionOne/identity/v3" + @staticmethod + def MultiCloudServiceHelper(cloud_owner, cloud_region_id, v2_token_resp_json, service_type, uri, data=None, method="GET",): + # get endpoint from token response + token = v2_token_resp_json["access"]["token"]["id"] + catalogs = v2_token_resp_json["access"]["serviceCatalog"] + for catalog in catalogs: + if catalog['type'] == service_type: + # now we have endpoint + 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 None + + content = json.JSONDecoder().decode(ret[1]) + return content + pass 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) diff --git a/windriver/titanium_cloud/urls.py b/windriver/titanium_cloud/urls.py index bd5af421..1d54325b 100644 --- a/windriver/titanium_cloud/urls.py +++ b/windriver/titanium_cloud/urls.py @@ -90,8 +90,6 @@ urlpatterns = [ registration.APIv1Registry.as_view()), url(r'^api/multicloud-titaniumcloud/v1/(?P<cloud_owner>[0-9a-zA-Z_-]+)/(?P<cloud_region_id>[0-9a-zA-Z_-]+)/exten', include('titanium_cloud.extensions.urlsV1')), - url(r'^api/multicloud-titaniumcloud/v1/(?P<cloud_owner>[0-9a-zA-Z_-]+)/(?P<cloud_region_id>[0-9a-zA-Z_-]+)/', - include('titanium_cloud.proxy.urlsV1')), url(r'^api/multicloud-titaniumcloud/v1/(?P<cloud_owner>[0-9a-zA-Z_-]+)/(?P<cloud_region_id>[0-9a-zA-Z_-]+)/tenants/?$', tenants.APIv1Tenants.as_view()), url(r'^api/multicloud-titaniumcloud/v1/(?P<cloud_owner>[0-9a-zA-Z_-]+)/(?P<cloud_region_id>[0-9a-zA-Z_-]+)/' @@ -103,4 +101,8 @@ urlpatterns = [ vesagent_ctrl.APIv1VesAgentCtrl.as_view()), url(r'^api/multicloud-titaniumcloud/v1/(?P<cloud_owner>[0-9a-zA-Z_-]+)/(?P<cloud_region_id>[0-9a-zA-Z_-]+)/infra_workload/?$', infra_workload.APIv1InfraWorkload.as_view()), + url(r'^api/multicloud-titaniumcloud/v1/(?P<cloud_owner>[0-9a-zA-Z_-]+)/(?P<cloud_region_id>[0-9a-zA-Z_-]+)/infra_workload/(?P<requri>[0-9a-zA-Z_-]*)/?$', + infra_workload.APIv1InfraWorkload.as_view()), + url(r'^api/multicloud-titaniumcloud/v1/(?P<cloud_owner>[0-9a-zA-Z_-]+)/(?P<cloud_region_id>[0-9a-zA-Z_-]+)/', + include('titanium_cloud.proxy.urlsV1')), ] |