summaryrefslogtreecommitdiff
path: root/keystoneclient/service_catalog.py
blob: b73f67ab4b375bf2093ba937e5ad7f19e38e1b4f (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# Copyright 2011 OpenStack LLC.
# Copyright 2011, Piston Cloud Computing, Inc.
# Copyright 2011 Nebula, Inc.
#
# All Rights Reserved.
#
# 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.


from keystoneclient import exceptions


class ServiceCatalog(object):
    """Helper methods for dealing with a Keystone Service Catalog."""

    @classmethod
    def factory(cls, resource_dict, token=None, region_name=None):
        """Create ServiceCatalog object given a auth token."""
        if ServiceCatalogV3.is_valid(resource_dict):
            return ServiceCatalogV3(token, resource_dict, region_name)
        elif ServiceCatalogV2.is_valid(resource_dict):
            return ServiceCatalogV2(resource_dict, region_name)
        else:
            raise NotImplementedError('Unrecognized auth response')

    def get_token(self):
        """Fetch token details from service catalog.

        Returns a dictionary containing the following::

        - `id`: Token's ID
        - `expires`: Token's expiration
        - `user_id`: Authenticated user's ID
        - `tenant_id`: Authorized project's ID
        - `domain_id`: Authorized domain's ID

        """
        raise NotImplementedError()

    def get_endpoints(self, service_type=None, endpoint_type=None):
        """Fetch and filter endpoints for the specified service(s).

        Returns endpoints for the specified service (or all) and
        that contain the specified type (or all).
        """
        raise NotImplementedError()

    def get_urls(self, attr=None, filter_value=None,
                 service_type='identity', endpoint_type='publicURL'):
        """Fetch endpoint urls from the service catalog.

        Fetch the endpoints from the service catalog for a particular
        endpoint attribute. If no attribute is given, return the first
        endpoint of the specified type.

        :param string attr: Endpoint attribute name.
        :param string filter_value: Endpoint attribute value.
        :param string service_type: Service type of the endpoint.
        :param string endpoint_type: Type of endpoint.
                                     Possible values: public or publicURL,
                                         internal or internalURL,
                                         admin or adminURL
        :param string region_name: Region of the endpoint.

        :returns: tuple of urls or None (if no match found)
        """
        raise NotImplementedError()

    def url_for(self, attr=None, filter_value=None,
                service_type='identity', endpoint_type='publicURL'):
        """Fetch an endpoint from the service catalog.

        Fetch the specified endpoint from the service catalog for
        a particular endpoint attribute. If no attribute is given, return
        the first endpoint of the specified type.

        Valid endpoint types: `public` or `publicURL`,
                              `internal` or `internalURL`,
                              `admin` or 'adminURL`
        """
        raise NotImplementedError()


class ServiceCatalogV2(ServiceCatalog):
    """An object for encapsulating the service catalog using raw v2 auth token
    from Keystone."""

    def __init__(self, resource_dict, region_name=None):
        self.catalog = resource_dict
        self.region_name = region_name

    @classmethod
    def is_valid(cls, resource_dict):
        # This class is also used for reading token info of an unscoped token.
        # Unscoped token does not have 'serviceCatalog' in V2, checking this
        # will not work. Use 'token' attribute instead.
        return 'token' in resource_dict

    def get_token(self):
        token = {'id': self.catalog['token']['id'],
                 'expires': self.catalog['token']['expires']}
        try:
            token['user_id'] = self.catalog['user']['id']
            token['tenant_id'] = self.catalog['token']['tenant']['id']
        except Exception:
            # just leave the tenant and user out if it doesn't exist
            pass
        return token

    def get_endpoints(self, service_type=None, endpoint_type=None):
        if endpoint_type and 'URL' not in endpoint_type:
            endpoint_type = endpoint_type + 'URL'

        sc = {}
        for service in self.catalog.get('serviceCatalog', []):
            if service_type and service_type != service['type']:
                continue
            sc[service['type']] = []
            for endpoint in service['endpoints']:
                if endpoint_type and endpoint_type not in endpoint.keys():
                    continue
                sc[service['type']].append(endpoint)
        return sc

    def get_urls(self, attr=None, filter_value=None,
                 service_type='identity', endpoint_type='publicURL'):
        sc_endpoints = self.get_endpoints(service_type=service_type,
                                          endpoint_type=endpoint_type)
        endpoints = sc_endpoints.get(service_type)
        if not endpoints:
            return

        if endpoint_type and 'URL' not in endpoint_type:
            endpoint_type = endpoint_type + 'URL'

        return tuple(endpoint[endpoint_type]
                     for endpoint in endpoints
                     if (endpoint_type in endpoint
                         and (not self.region_name
                              or endpoint.get('region') == self.region_name)
                         and (not filter_value
                              or endpoint.get(attr) == filter_value)))

    def url_for(self, attr=None, filter_value=None,
                service_type='identity', endpoint_type='publicURL'):
        catalog = self.catalog.get('serviceCatalog', [])

        if not catalog:
            raise exceptions.EmptyCatalog('The service catalog is empty.')

        if 'URL' not in endpoint_type:
            endpoint_type = endpoint_type + 'URL'

        for service in catalog:
            if service['type'] != service_type:
                continue

            endpoints = service['endpoints']
            for endpoint in endpoints:
                if (self.region_name and
                        endpoint.get('region') != self.region_name):
                    continue
                if not filter_value or endpoint.get(attr) == filter_value:
                    return endpoint[endpoint_type]

        raise exceptions.EndpointNotFound('%s endpoint for %s not found.' %
                                          (endpoint_type, service_type))


class ServiceCatalogV3(ServiceCatalog):
    """An object for encapsulating the service catalog using raw v3 auth token
    from Keystone."""

    def __init__(self, token, resource_dict, region_name=None):
        self._auth_token = token
        self.catalog = resource_dict
        self.region_name = region_name

    @classmethod
    def is_valid(cls, resource_dict):
        # This class is also used for reading token info of an unscoped token.
        # Unscoped token does not have 'catalog', checking this
        # will not work. Use 'methods' attribute instead.
        return 'methods' in resource_dict

    def get_token(self):
        token = {'id': self._auth_token,
                 'expires': self.catalog['expires_at']}
        try:
            token['user_id'] = self.catalog['user']['id']
            domain = self.catalog.get('domain')
            if domain:
                token['domain_id'] = domain['id']
            project = self.catalog.get('project')
            if project:
                token['tenant_id'] = project['id']
        except Exception:
            # just leave the domain, project and user out if it doesn't exist
            pass
        return token

    def get_endpoints(self, service_type=None, endpoint_type=None):
        if endpoint_type:
            endpoint_type = endpoint_type.rstrip('URL')
        sc = {}
        for service in self.catalog.get('catalog', []):
            if service_type and service_type != service['type']:
                continue
            sc[service['type']] = []
            for endpoint in service['endpoints']:
                if endpoint_type and endpoint_type != endpoint['interface']:
                    continue
                sc[service['type']].append(endpoint)
        return sc

    def get_urls(self, attr=None, filter_value=None,
                 service_type='identity', endpoint_type='public'):
        if endpoint_type:
            endpoint_type = endpoint_type.rstrip('URL')
        sc_endpoints = self.get_endpoints(service_type=service_type,
                                          endpoint_type=endpoint_type)
        endpoints = sc_endpoints.get(service_type)
        if not endpoints:
            return None

        urls = list()
        for endpoint in endpoints:
            if (endpoint['interface'] == endpoint_type
                    and (not self.region_name
                         or endpoint.get('region') == self.region_name)
                    and (not filter_value
                         or endpoint.get(attr) == filter_value)):
                urls.append(endpoint['url'])
        return tuple(urls)

    def url_for(self, attr=None, filter_value=None,
                service_type='identity', endpoint_type='public'):
        catalog = self.catalog.get('catalog', [])

        if not catalog:
            raise exceptions.EmptyCatalog('The service catalog is empty.')

        if endpoint_type:
            endpoint_type = endpoint_type.rstrip('URL')

        for service in catalog:
            if service['type'] != service_type:
                continue

            endpoints = service['endpoints']
            for endpoint in endpoints:
                if endpoint.get('interface') != endpoint_type:
                    continue
                if (self.region_name and
                        endpoint.get('region') != self.region_name):
                    continue
                if not filter_value or endpoint.get(attr) == filter_value:
                    return endpoint['url']

        raise exceptions.EndpointNotFound('%s endpoint for %s not found.' %
                                          (endpoint_type, service_type))