summaryrefslogtreecommitdiffstats
path: root/share/newton_base/proxy/proxy_utils.py
blob: 98ef0a2092665ee1107372f6b160eab7b4b4760e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# 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.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import json
import traceback
import re
import uuid

from rest_framework import status

from common.exceptions import VimDriverNewtonException

logger = logging.getLogger(__name__)

#DEBUG=True
#MULTICLOUD_PREFIX = "http://%s:%s/api/multicloud-newton/v0" %(config.MSB_SERVICE_IP, config.MSB_SERVICE_PORT)

class ProxyUtils(object):

    @staticmethod
    def update_prefix(metadata_catalog, content):
        '''match the longgest prefix and replace it'''

        if not content:
            return content

        for (servicetype, service_metadata) in metadata_catalog.items():
            real_prefix = service_metadata['prefix']
            proxy_prefix = service_metadata['proxy_prefix']

            if content:
                # filter the resp content and replace all endpoint prefix
                tmp_content = json.dumps(content)
                tmp_pattern = re.compile(real_prefix+r'([^:])')
                tmp_content = tmp_pattern.sub(proxy_prefix+r'\1', tmp_content)
                content = json.loads(tmp_content)

        return content

    @staticmethod
    def update_catalog(vimid, catalog, multicould_namespace):
        '''
        replace the orignal endpoints with multicloud's
        return the catalog with updated endpoints, and also another catalog with prefix and suffix of each endpoint
        :param vimid:
        :param catalog: service catalog to be updated
        :param multicould_namespace: multicloud namespace prefix to replace the real one in catalog endpoints url
        :return:updated catalog, and metadata_catalog looks like:
        {
            'compute': {
                'prefix': 'http://ip:port',
                'proxy_prefix': 'http://another_ip: another_port',
                'suffix': 'v2.1/53a4ab9015c84ee892e46d294f3b8b2d',
            },
            'network': {
                'prefix': 'http://ip:port',
                'proxy_prefix': 'http://another_ip: another_port',
                'suffix': '',
            },
        }
        '''

        metadata_catalog = {}
        if catalog:
            # filter and replace endpoints of catalogs
            for item in catalog:
                one_catalog = {}
                metadata_catalog[item['type']] = one_catalog

                endpoints = item['endpoints']
                item['endpoints']=[]
                for endpoint in endpoints:
                    interface = endpoint.get('interface', None)
                    if interface != 'public':
                        continue
    #                elif item["type"] == "identity":
    #                    endpoint["url"] = multicould_namespace + "/%s/identity/v3" % vimid
                    else:
                        # replace the endpoint with MultiCloud's proxy
                        import re
                        endpoint_url = endpoint["url"]
                        real_prefix = None
                        real_suffix = None
                        m = re.search(r'^(http[s]?://[0-9.]+:[0-9]+)(/([0-9a-zA-Z/._-]+)$)?', endpoint_url)
                        if not m:
                            m = re.search(r'^(http[s]?://[0-9.]+)(/([0-9a-zA-Z/._-]+)$)?', endpoint_url)
                        if m:
                            real_prefix = m.group(1)
                            real_suffix = m.group(3)

                        if real_prefix:
                            # populate metadata_catalog
                            one_catalog['prefix'] = real_prefix
                            one_catalog['suffix'] = real_suffix if real_suffix else ''
                            one_catalog['proxy_prefix'] = multicould_namespace + "/%s" % vimid

                            endpoint_url = multicould_namespace + "/%s" % vimid

                            tmp_pattern = re.compile(item["type"])
                            if not real_suffix or not re.match(tmp_pattern, real_suffix):
                                one_catalog['proxy_prefix'] += "/" + item["type"]
                                endpoint_url += '/' + item["type"]

                            if real_suffix:
                                endpoint_url += "/" + real_suffix

                            if item["type"] == "identity":
                                endpoint_url = multicould_namespace + "/%s/identity/v3" % vimid

                        else:
                            #something wrong
                            pass

                        endpoint["url"] = endpoint_url
                    item['endpoints'].append( endpoint )

            return catalog, metadata_catalog
        else:
            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


#    @staticmethod
#    def update_dnsaas_project_id(content, new_project_id):
#        '''
#        update project id in DNSaaS delegate content
#        '''
#        try:
#            if content:
#                # filter the resp content and replace all endpoint prefix
#                tmp_content = json.dumps(content)
#                tmp_pattern = re.compile(r'(^.*)"project_id"\s*:\s*"' + r'[\w-]+'+r'"(.*$)')
#                part1 = tmp_pattern.sub(r'\1', tmp_content)
#                part2 = tmp_pattern.sub(r'"project_id":"'+new_project_id +r'"\2', tmp_content)
#                #logger.debug("jsonstr:%s,part1:%s,part2:%s"%(tmp_content,part1,part2))
#                content = json.loads(part1+part2)
#            return content
#        except Exception as e:
#            logger.error(traceback.format_exc())
#            return content