File: _adapter.py

package info (click to toggle)
python-shade 1.30.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,196 kB
  • sloc: python: 33,354; sh: 111; makefile: 15
file content (164 lines) | stat: -rw-r--r-- 5,949 bytes parent folder | download | duplicates (2)
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
# Copyright (c) 2016 Red Hat, 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.

''' Wrapper around keystoneauth Session to wrap calls in TaskManager '''

import functools
from keystoneauth1 import adapter
from six.moves import urllib

from shade import _log
from shade import exc
from shade import task_manager


def extract_name(url):
    '''Produce a key name to use in logging/metrics from the URL path.

    We want to be able to logic/metric sane general things, so we pull
    the url apart to generate names. The function returns a list because
    there are two different ways in which the elements want to be combined
    below (one for logging, one for statsd)

    Some examples are likely useful:

    /servers -> ['servers']
    /servers/{id} -> ['servers']
    /servers/{id}/os-security-groups -> ['servers', 'os-security-groups']
    /v2.0/networks.json -> ['networks']
    '''

    url_path = urllib.parse.urlparse(url).path.strip()
    # Remove / from the beginning to keep the list indexes of interesting
    # things consistent
    if url_path.startswith('/'):
        url_path = url_path[1:]

    # Special case for neutron, which puts .json on the end of urls
    if url_path.endswith('.json'):
        url_path = url_path[:-len('.json')]

    url_parts = url_path.split('/')
    if url_parts[-1] == 'detail':
        # Special case detail calls
        # GET /servers/detail
        # returns ['servers', 'detail']
        name_parts = url_parts[-2:]
    else:
        # Strip leading version piece so that
        # GET /v2.0/networks
        # returns ['networks']
        if url_parts[0] in ('v1', 'v2', 'v2.0'):
            url_parts = url_parts[1:]
        name_parts = []
        # Pull out every other URL portion - so that
        # GET /servers/{id}/os-security-groups
        # returns ['servers', 'os-security-groups']
        for idx in range(0, len(url_parts)):
            if not idx % 2 and url_parts[idx]:
                name_parts.append(url_parts[idx])

    # Keystone Token fetching is a special case, so we name it "tokens"
    if url_path.endswith('tokens'):
        name_parts = ['tokens']

    # Getting the root of an endpoint is doing version discovery
    if not name_parts:
        name_parts = ['discovery']

    # Strip out anything that's empty or None
    return [part for part in name_parts if part]


class ShadeAdapter(adapter.Adapter):

    def __init__(self, shade_logger, manager, *args, **kwargs):
        super(ShadeAdapter, self).__init__(*args, **kwargs)
        self.shade_logger = shade_logger
        self.manager = manager
        self.request_log = _log.setup_logging('shade.request_ids')

    def _log_request_id(self, response, obj=None):
        # Log the request id and object id in a specific logger. This way
        # someone can turn it on if they're interested in this kind of tracing.
        request_id = response.headers.get('x-openstack-request-id')
        if not request_id:
            return response
        tmpl = "{meth} call to {service} for {url} used request id {req}"
        kwargs = dict(
            meth=response.request.method,
            service=self.service_type,
            url=response.request.url,
            req=request_id)

        if isinstance(obj, dict):
            obj_id = obj.get('id', obj.get('uuid'))
            if obj_id:
                kwargs['obj_id'] = obj_id
                tmpl += " returning object {obj_id}"
        self.request_log.debug(tmpl.format(**kwargs))
        return response

    def _munch_response(self, response, result_key=None, error_message=None):
        exc.raise_from_response(response, error_message=error_message)

        if not response.content:
            # This doens't have any content
            return self._log_request_id(response)

        # Some REST calls do not return json content. Don't decode it.
        if 'application/json' not in response.headers.get('Content-Type'):
            return self._log_request_id(response)

        try:
            result_json = response.json()
            self._log_request_id(response, result_json)
        except Exception:
            return self._log_request_id(response)
        return result_json

    def request(
            self, url, method, run_async=False, error_message=None,
            *args, **kwargs):
        name_parts = extract_name(url)
        name = '.'.join([self.service_type, method] + name_parts)
        class_name = "".join([
            part.lower().capitalize() for part in name.split('.')])

        request_method = functools.partial(
            super(ShadeAdapter, self).request, url, method)

        class RequestTask(task_manager.BaseTask):

            def __init__(self, **kw):
                super(RequestTask, self).__init__(**kw)
                self.name = name
                self.__class__.__name__ = str(class_name)
                self.run_async = run_async

            def main(self, client):
                self.args.setdefault('raise_exc', False)
                return request_method(**self.args)

        response = self.manager.submit_task(RequestTask(**kwargs))
        if run_async:
            return response
        else:
            return self._munch_response(response, error_message=error_message)

    def _version_matches(self, version):
        api_version = self.get_api_major_version()
        if api_version:
            return api_version[0] == version
        return False