diff options
-rw-r--r-- | newton/newton/proxy/urls.py | 3 | ||||
-rw-r--r-- | newton/newton/proxy/views/dnsaasdelegate.py | 191 | ||||
-rw-r--r-- | newton/newton/proxy/views/identityV3.py | 13 | ||||
-rw-r--r-- | newton/newton/proxy/views/proxy_utils.py | 45 | ||||
-rw-r--r-- | newton/newton/registration/views/registration.py | 7 |
5 files changed, 253 insertions, 6 deletions
diff --git a/newton/newton/proxy/urls.py b/newton/newton/proxy/urls.py index 556cc4e6..0e0ca52a 100644 --- a/newton/newton/proxy/urls.py +++ b/newton/newton/proxy/urls.py @@ -17,6 +17,7 @@ from rest_framework.urlpatterns import format_suffix_patterns from newton.proxy.views import identityV3 from newton.proxy.views import services +from newton.proxy.views import dnsaasdelegate urlpatterns = [ # url(r'^identity/v2)$', @@ -29,6 +30,8 @@ urlpatterns = [ identityV3.TokensV2.as_view()), url(r'^identity/v2.0/tenants/?$', services.GetTenants.as_view()), + url(r'dns-delegate/(?P<requri>[0-9a-zA-Z./_-]*)$', + dnsaasdelegate.DnsaasDelegate.as_view()), url(r'^(?P<servicetype>[0-9a-zA-Z_-]{,18})/(?P<requri>[0-9a-zA-Z./_-]*)$', services.Services.as_view()), ] diff --git a/newton/newton/proxy/views/dnsaasdelegate.py b/newton/newton/proxy/views/dnsaasdelegate.py new file mode 100644 index 00000000..bce24dff --- /dev/null +++ b/newton/newton/proxy/views/dnsaasdelegate.py @@ -0,0 +1,191 @@ +# Copyright (c) 2017 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. +# See the License for the specific language governing permissions and +# limitations under the License. +import logging +import json +import traceback + +from keystoneauth1.exceptions import HttpError +import re +from rest_framework.permissions import BasePermission +from rest_framework.response import Response +from rest_framework import status +from rest_framework.views import APIView + +from newton.proxy.views.services import Services + +from newton.proxy.views.proxy_utils import ProxyUtils +from newton.pub.exceptions import VimDriverNewtonException +from newton.pub.msapi import extsys +from newton.requests.views.util import VimDriverUtils + + +logger = logging.getLogger(__name__) + +DEBUG=True + +class DnsaasDelegate(Services): + ''' + DNSaaS delegate service + ''' + + def __init__(self): + self._logger = logger + + + def _do_action(self, action, request, vim_id, servicetype, requri): + tmp_auth_token = self._get_token(request) + try: + # fetch the auth_state out of cache + auth_state_str, metadata_catalog_str = VimDriverUtils.get_token_cache(tmp_auth_token) + + if not auth_state_str: + #invalid token + return Response(data={'error': "request token %s is not valid" % (tmp_auth_token)}, + status=status.HTTP_404_NOT_FOUND) + + # get project name from auth_state + auth_state = json.loads(auth_state_str) + if not auth_state: + # invalid token + return Response(data={'error': "request token %s is broken" % (tmp_auth_token)}, + status=status.HTTP_404_NOT_FOUND) + + tenant_name = auth_state['body']['token']['project']['name'] + + #find out the delegated DNSaaS provider + viminfo = VimDriverUtils.get_vim_info(vim_id) + if not viminfo: + return Response(data={'error': "vimid %s is not found" % (vim_id)}, + status=status.HTTP_404_NOT_FOUND) + + cloud_dns_delegate_info = None + cloud_extra_info_str = viminfo.get('cloud_extra_info') + if cloud_extra_info_str: + cloud_extra_info = json.loads(cloud_extra_info_str) + cloud_dns_delegate_info = cloud_extra_info.get("dns-delegate") + + if not cloud_dns_delegate_info \ + or not cloud_dns_delegate_info.get("cloud-owner") \ + or not cloud_dns_delegate_info.get("cloud-region-id"): + return Response(data={'error': "dns-delegate for vimid %s is not configured" + % (vim_id)}, + status=status.HTTP_404_NOT_FOUND) + + vimid_delegate = cloud_dns_delegate_info.get("cloud-owner") \ + + "_" \ + + cloud_dns_delegate_info.get("cloud-region-id") + + + #now forward request to delegated DNS service endpoint + vim = VimDriverUtils.get_vim_info(vimid_delegate) + if not vim: + return Response(data={'error': "vimid %s is not found" % (vimid_delegate)}, + status=status.HTTP_404_NOT_FOUND) + + sess = VimDriverUtils.get_session(vim, tenantname=tenant_name, auth_state=None) + + cloud_owner, regionid = extsys.decode_vim_id(vimid_delegate) + interface = 'public' + service = { + 'service_type': servicetype, + 'interface': interface, + 'region_id': regionid + } + + req_resource = requri + querystr = VimDriverUtils.get_query_part(request) + if querystr: + req_resource += "?" + querystr + + self._logger.debug("service " + action + " request uri %s" % (req_resource)) + if(action == "get"): + resp = sess.get(req_resource, endpoint_filter=service, + headers={"Content-Type": "application/json", + "Accept": "application/json"}) + elif(action == "post"): + resp = sess.post(req_resource, data=json.JSONEncoder().encode(request.data), + endpoint_filter=service, + headers={"Content-Type": "application/json", + "Accept": "application/json"}) + elif(action == "put"): + resp = sess.put(req_resource, data=json.JSONEncoder().encode(request.data), + endpoint_filter=service, + headers={"Content-Type": "application/json", + "Accept": "application/json"}) + elif(action == "patch"): + resp = sess.patch(req_resource, data=json.JSONEncoder().encode(request.data), + endpoint_filter=service, + headers={"Content-Type": "application/json", + "Accept": "application/json"}) + elif (action == "delete"): + resp = sess.delete(req_resource, endpoint_filter=service, + headers={"Content-Type": "application/json", + "Accept": "application/json"}) + content = resp.json() if resp.content else None + self._logger.debug("service " + action + " response: %s, %s" % (resp.status_code, content)) + + if (action != "delete"): + return Response(headers={'X-Subject-Token': tmp_auth_token}, data=content, status=resp.status_code) + return Response(headers={'X-Subject-Token': tmp_auth_token}, status=resp.status_code) + except VimDriverNewtonException as e: + return Response(data={'error': e.content}, status=e.status_code) + except HttpError as e: + self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json())) + return Response(data=e.response.json(), status=e.http_status) + except Exception as e: + self._logger.error(traceback.format_exc()) + return Response(data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + def get(self, request, vimid="", servicetype="dns-delegate", requri=""): + self._logger.debug("DnsaasDelegate--get::META> %s" % request.META) + self._logger.debug("DnsaasDelegate--get::data> %s" % request.data) + self._logger.debug("DnsaasDelegate--get::vimid, servicetype, requri> %s,%s,%s" + % (vimid, servicetype, requri)) + return self._do_action("get", request, vimid, "dns", requri) + + def head(self, request, vimid="", servicetype="dns-delegate", requri=""): + self._logger.debug("DnsaasDelegate--get::META> %s" % request.META) + self._logger.debug("DnsaasDelegate--get::data> %s" % request.data) + self._logger.debug("DnsaasDelegate--get::vimid, servicetype, requri> %s,%s,%s" + % (vimid, servicetype, requri)) + return self._do_action("head", request, vimid, "dns", requri) + + def post(self, request, vimid="", servicetype="dns-delegate", requri=""): + self._logger.debug("DnsaasDelegate--get::META> %s" % request.META) + self._logger.debug("DnsaasDelegate--get::data> %s" % request.data) + self._logger.debug("DnsaasDelegate--get::vimid, servicetype, requri> %s,%s,%s" + % (vimid, servicetype, requri)) + return self._do_action("post", request, vimid, "dns", requri) + + def put(self, request, vimid="", servicetype="dns-delegate", requri=""): + self._logger.debug("DnsaasDelegate--get::META> %s" % request.META) + self._logger.debug("DnsaasDelegate--get::data> %s" % request.data) + self._logger.debug("DnsaasDelegate--get::vimid, servicetype, requri> %s,%s,%s" + % (vimid, servicetype, requri)) + return self._do_action("put", request, vimid, "dns", requri) + + def patch(self, request, vimid="", servicetype="dns-delegate", requri=""): + self._logger.debug("DnsaasDelegate--get::META> %s" % request.META) + self._logger.debug("DnsaasDelegate--get::data> %s" % request.data) + self._logger.debug("DnsaasDelegate--get::vimid, servicetype, requri> %s,%s,%s" + % (vimid, servicetype, requri)) + return self._do_action("patch", request, vimid, "dns", requri) + + def delete(self, request, vimid="", servicetype="dns-delegate", requri=""): + self._logger.debug("DnsaasDelegate--get::META> %s" % request.META) + self._logger.debug("DnsaasDelegate--get::data> %s" % request.data) + self._logger.debug("DnsaasDelegate--get::vimid, servicetype, requri> %s,%s,%s" + % (vimid, servicetype, requri)) + return self._do_action("delete", request, vimid, "dns", requri) diff --git a/newton/newton/proxy/views/identityV3.py b/newton/newton/proxy/views/identityV3.py index 048d5a0f..bc2b06e9 100644 --- a/newton/newton/proxy/views/identityV3.py +++ b/newton/newton/proxy/views/identityV3.py @@ -80,10 +80,17 @@ class Tokens(APIView): #set expiring in 1 hour #update the catalog - tmp_auth_data['token']['catalog'], tmp_metadata_catalog = ProxyUtils.update_catalog(vimid, tmp_auth_data['token']['catalog'], self.proxy_prefix) - tmp_auth_token = VimDriverUtils.update_token_cache(vim, sess, tmp_auth_token, tmp_auth_state, json.dumps(tmp_metadata_catalog)) + tmp_auth_data['token']['catalog'], tmp_metadata_catalog = ProxyUtils.update_catalog( + vimid, tmp_auth_data['token']['catalog'], self.proxy_prefix) - resp = Response(headers={'X-Subject-Token': tmp_auth_token}, data=tmp_auth_data, status=status.HTTP_201_CREATED) + tmp_auth_token = VimDriverUtils.update_token_cache( + vim, sess, tmp_auth_token, tmp_auth_state, json.dumps(tmp_metadata_catalog)) + + tmp_auth_data['token']['catalog'] = ProxyUtils.update_catalog_dnsaas( + vimid,tmp_auth_data['token']['catalog'], self.proxy_prefix, vim) + + resp = Response(headers={'X-Subject-Token': tmp_auth_token}, + data=tmp_auth_data, status=status.HTTP_201_CREATED) return resp except VimDriverNewtonException as e: diff --git a/newton/newton/proxy/views/proxy_utils.py b/newton/newton/proxy/views/proxy_utils.py index 89a49286..c80b9680 100644 --- a/newton/newton/proxy/views/proxy_utils.py +++ b/newton/newton/proxy/views/proxy_utils.py @@ -15,6 +15,8 @@ import logging import json import traceback import re +import uuid + from rest_framework import status from newton.pub.exceptions import VimDriverNewtonException @@ -128,4 +130,47 @@ class ProxyUtils(object): return None + @staticmethod + def update_catalog_dnsaas(vimid, catalog, multicould_namespace, viminfo): + ''' + append DNSaaS delegate endpoints to catalog + :param vimid: + :param catalog: service catalog to be updated + :param multicould_namespace: multicloud namespace prefix to replace the real one in catalog endpoints url + :param viminfo: vim information + :return:updated catalog + ''' + + try: + cloud_dns_delegate_info = None + cloud_extra_info_str = viminfo.get('cloud_extra_info') + if cloud_extra_info_str: + cloud_extra_info = json.loads(cloud_extra_info_str) + cloud_dns_delegate_info = cloud_extra_info.get("dns-delegate") + + if not cloud_dns_delegate_info\ + or not cloud_dns_delegate_info.get("cloud-owner") \ + or not cloud_dns_delegate_info.get("cloud-region-id"): + #DNSaaS deleget was not configured yet + return catalog + + dns_catalog = { + "name":"dns-delegate", + "type":"dns", + "id": str(uuid.uuid1()), + "endpoints": { + "interface": "public", + "region": cloud_dns_delegate_info.get("cloud-region-id"), + "region_id": cloud_dns_delegate_info.get("cloud-region-id"), + "id": str(uuid.uuid1()), + "url": multicould_namespace + "/%s/dns-delegate" % vimid, + } + } + catalog.append(dns_catalog) + + return catalog + + except Exception as e: + logger.error(traceback.format_exc()) + return catalog diff --git a/newton/newton/registration/views/registration.py b/newton/newton/registration/views/registration.py index 0bdc0fcd..0a9f7f8e 100644 --- a/newton/newton/registration/views/registration.py +++ b/newton/newton/registration/views/registration.py @@ -555,9 +555,10 @@ class Registry(APIView): def _discover_epa_resources(self, vimid="", viminfo=None): try: cloud_epa_caps_info = {} - cloud_extra_info = viminfo.get('cloud_extra_info') - if cloud_extra_info: - cloud_epa_caps_info.update(json.loads(cloud_extra_info)) + cloud_extra_info_str = viminfo.get('cloud_extra_info') + if cloud_extra_info_str: + cloud_extra_info = json.loads(cloud_extra_info_str) + cloud_epa_caps_info.update(cloud_extra_info.get("epa-caps")) cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid) ret = self._update_epa_caps(cloud_owner, cloud_region_id, |