This file is indexed.

/usr/lib/python2.7/dist-packages/novaclient/client.py is in python-novaclient 2:9.1.1-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# Copyright 2010 Jacob Kaplan-Moss
# Copyright 2011 OpenStack Foundation
# Copyright 2011 Piston Cloud Computing, 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.

"""
OpenStack Client interface. Handles the REST calls and responses.
"""

import itertools
import pkgutil
import warnings

from keystoneauth1 import adapter
from keystoneauth1 import identity
from keystoneauth1 import session as ksession
from oslo_utils import importutils
import pkg_resources

osprofiler_profiler = importutils.try_import("osprofiler.profiler")
osprofiler_web = importutils.try_import("osprofiler.web")

import novaclient
from novaclient import api_versions
from novaclient import exceptions
from novaclient import extension as ext
from novaclient.i18n import _
from novaclient import utils

# TODO(jichenjc): when an extension in contrib is moved to core extension,
# Add the name into the following list, then after last patch merged,
# remove the whole function
extensions_ignored_name = ["__init__"]


class SessionClient(adapter.LegacyJsonAdapter):

    client_name = 'python-novaclient'
    client_version = novaclient.__version__

    def __init__(self, *args, **kwargs):
        self.times = []
        self.timings = kwargs.pop('timings', False)
        self.api_version = kwargs.pop('api_version', None)
        self.api_version = self.api_version or api_versions.APIVersion()
        super(SessionClient, self).__init__(*args, **kwargs)

    def request(self, url, method, **kwargs):
        kwargs.setdefault('headers', kwargs.get('headers', {}))
        api_versions.update_headers(kwargs["headers"], self.api_version)

        # NOTE(dbelova): osprofiler_web.get_trace_id_headers does not add any
        # headers in case if osprofiler is not initialized.
        if osprofiler_web:
            kwargs['headers'].update(osprofiler_web.get_trace_id_headers())

        # NOTE(jamielennox): The standard call raises errors from
        # keystoneauth1, where we need to raise the novaclient errors.
        raise_exc = kwargs.pop('raise_exc', True)
        with utils.record_time(self.times, self.timings, method, url):
            resp, body = super(SessionClient, self).request(url,
                                                            method,
                                                            raise_exc=False,
                                                            **kwargs)

        # TODO(andreykurilin): uncomment this line, when we will be able to
        #   check only nova-related calls
        # api_versions.check_headers(resp, self.api_version)
        if raise_exc and resp.status_code >= 400:
            raise exceptions.from_response(resp, body, url, method)

        return resp, body

    def get_timings(self):
        return self.times

    def reset_timings(self):
        self.times = []

    @property
    def management_url(self):
        self.logger.warning(
            _("Property `management_url` is deprecated for SessionClient. "
              "Use `endpoint_override` instead."))
        return self.endpoint_override

    @management_url.setter
    def management_url(self, value):
        self.logger.warning(
            _("Property `management_url` is deprecated for SessionClient. "
              "It should be set via `endpoint_override` variable while class"
              " initialization."))
        self.endpoint_override = value


def _construct_http_client(api_version=None,
                           auth=None,
                           auth_token=None,
                           auth_url=None,
                           cacert=None,
                           cert=None,
                           endpoint_override=None,
                           endpoint_type='publicURL',
                           http_log_debug=False,
                           insecure=False,
                           logger=None,
                           os_cache=False,
                           password=None,
                           project_domain_id=None,
                           project_domain_name=None,
                           project_id=None,
                           project_name=None,
                           region_name=None,
                           service_name=None,
                           service_type='compute',
                           session=None,
                           timeout=None,
                           timings=False,
                           user_agent='python-novaclient',
                           user_domain_id=None,
                           user_domain_name=None,
                           user_id=None,
                           username=None,
                           **kwargs):
    if not session:
        if not auth and auth_token:
            auth = identity.Token(auth_url=auth_url,
                                  token=auth_token,
                                  project_id=project_id,
                                  project_name=project_name,
                                  project_domain_id=project_domain_id,
                                  project_domain_name=project_domain_name)
        elif not auth:
            auth = identity.Password(username=username,
                                     user_id=user_id,
                                     password=password,
                                     project_id=project_id,
                                     project_name=project_name,
                                     auth_url=auth_url,
                                     project_domain_id=project_domain_id,
                                     project_domain_name=project_domain_name,
                                     user_domain_id=user_domain_id,
                                     user_domain_name=user_domain_name)
        session = ksession.Session(auth=auth,
                                   verify=(cacert or not insecure),
                                   timeout=timeout,
                                   cert=cert,
                                   user_agent=user_agent)

    return SessionClient(api_version=api_version,
                         auth=auth,
                         endpoint_override=endpoint_override,
                         interface=endpoint_type,
                         logger=logger,
                         region_name=region_name,
                         service_name=service_name,
                         service_type=service_type,
                         session=session,
                         timings=timings,
                         user_agent=user_agent,
                         **kwargs)


def discover_extensions(*args, **kwargs):
    """Returns the list of extensions, which can be discovered by python path
    and by entry-point 'novaclient.extension'.
    """
    # TODO(mriedem): Remove support for 'only_contrib' in Queens.
    if 'only_contrib' in kwargs and kwargs['only_contrib']:
        warnings.warn(_('Discovering extensions only by contrib path is no '
                        'longer supported since all contrib extensions '
                        'have either been made required or removed. The '
                        'only_contrib argument is deprecated and will be '
                        'removed in a future release.'))
        return []
    chain = itertools.chain(_discover_via_python_path(),
                            _discover_via_entry_points())
    return [ext.Extension(name, module) for name, module in chain]


def _discover_via_python_path():
    for (module_loader, name, _ispkg) in pkgutil.iter_modules():
        if name.endswith('_python_novaclient_ext'):
            # NOTE(sdague): needed for python 2.x compatibility.
            if not hasattr(module_loader, 'load_module'):
                module_loader = module_loader.find_module(name)
            module = module_loader.load_module(name)
            if hasattr(module, 'extension_name'):
                name = module.extension_name

            yield name, module


def _discover_via_entry_points():
    for ep in pkg_resources.iter_entry_points('novaclient.extension'):
        name = ep.name
        module = ep.load()

        yield name, module


def _get_client_class_and_version(version):
    if not isinstance(version, api_versions.APIVersion):
        version = api_versions.get_api_version(version)
    else:
        api_versions.check_major_version(version)
    if version.is_latest():
        raise exceptions.UnsupportedVersion(
            _("The version should be explicit, not latest."))
    return version, importutils.import_class(
        "novaclient.v%s.client.Client" % version.ver_major)


def get_client_class(version):
    """Returns Client class based on given version."""
    warnings.warn(_("'get_client_class' is deprecated. "
                    "Please use `novaclient.client.Client` instead."))
    _api_version, client_class = _get_client_class_and_version(version)
    return client_class


def _check_arguments(kwargs, release, deprecated_name, right_name=None):
    """Process deprecation of arguments.

    Checks presence of deprecated argument in kwargs, prints proper warning
    message, renames key to right one it needed.
    """
    if deprecated_name in kwargs:
        if right_name:
            if right_name in kwargs:
                msg = _("The '%(old)s' argument is deprecated in "
                        "%(release)s and its use may result in errors "
                        "in future releases. As '%(new)s' is provided, "
                        "the '%(old)s' argument will be ignored.") % {
                    "old": deprecated_name, "release": release,
                    "new": right_name}
                kwargs.pop(deprecated_name)
            else:
                msg = _("The '%(old)s' argument is deprecated in "
                        "%(release)s and its use may result in errors in "
                        "future releases. Use '%(right)s' instead.") % {
                    "old": deprecated_name, "release": release,
                    "right": right_name}
                kwargs[right_name] = kwargs.pop(deprecated_name)

        else:
            msg = _("The '%(old)s' argument is deprecated in %(release)s "
                    "and its use may result in errors in future "
                    "releases.") % {
                "old": deprecated_name, "release": release}
            # just ignore it
            kwargs.pop(deprecated_name)

        warnings.warn(msg)


def Client(version, username=None, password=None, project_id=None,
           auth_url=None, **kwargs):
    """Initialize client object based on given version.

    HOW-TO:
    The simplest way to create a client instance is initialization with your
    credentials::

        >>> from novaclient import client
        >>> nova = client.Client(VERSION, USERNAME, PASSWORD,
        ...                      PROJECT_ID, AUTH_URL)

    Here ``VERSION`` can be a string or
    ``novaclient.api_versions.APIVersion`` obj. If you prefer string value,
    you can use ``1.1`` (deprecated now), ``2`` or ``2.X``
    (where X is a microversion).


    Alternatively, you can create a client instance using the keystoneauth
    session API. See "The novaclient Python API" page at
    python-novaclient's doc.
    """
    if password:
        kwargs["password"] = password
    if project_id:
        kwargs["project_id"] = project_id

    _check_arguments(kwargs, "Ocata", "auth_plugin")
    _check_arguments(kwargs, "Ocata", "auth_system")
    if "no_cache" in kwargs:
        _check_arguments(kwargs, "Ocata", "no_cache", right_name="os_cache")
        # os_cache is not a fully compatible with no_cache, so we need to
        # apply this custom processing
        kwargs["os_cache"] = not kwargs["os_cache"]
    _check_arguments(kwargs, "Ocata", "bypass_url",
                     right_name="endpoint_override")
    _check_arguments(kwargs, "Ocata", "api_key", right_name="password")
    # NOTE(andreykurilin): OpenStack projects use two variables with one
    #   meaning: 'endpoint_type' and 'interface'. 'endpoint_type' is an old
    #   name which was used by most OpenStack clients. Later it was replaced by
    #  'interface' in keystone and later some other clients switched to new
    #   variable name too. In case of novaclient, there is no need to switch to
    #  'interface' variable name due too several reasons:
    #     - novaclient uses 'endpoint_type' variable name long time ago and
    #       there is no real reasons to switch to new name;
    #     - 'interface' argument is used in several shell subcommands
    #       (for example in `nova floating-ip-bulk-create`), so we will need to
    #       modify these subcommands to not conflict with global flag
    #       'interface'
    #   Actually, novaclient did not accept 'interface' before, but since we
    #   allow additional arguments(kwargs), someone can use this variable name
    #   and face issue about unexpected behavior.
    _check_arguments(kwargs, "Ocata", "interface", right_name="endpoint_type")
    _check_arguments(kwargs, "Ocata", "tenant_name", right_name="project_name")
    _check_arguments(kwargs, "Ocata", "tenant_id", right_name="project_id")
    _check_arguments(kwargs, "Ocata", "proxy_tenant_id")
    _check_arguments(kwargs, "Ocata", "proxy_token")
    _check_arguments(kwargs, "Ocata", "connection_pool")
    _check_arguments(kwargs, "Ocata", "volume_service_name")

    api_version, client_class = _get_client_class_and_version(version)
    kwargs.pop("direct_use", None)

    profile = kwargs.pop("profile", None)
    if osprofiler_profiler and profile:
        # Initialize the root of the future trace: the created trace ID will
        # be used as the very first parent to which all related traces will be
        # bound to. The given HMAC key must correspond to the one set in
        # nova-api nova.conf, otherwise the latter will fail to check the
        # request signature and will skip initialization of osprofiler on
        # the server side.
        osprofiler_profiler.init(profile)

    return client_class(api_version=api_version,
                        auth_url=auth_url,
                        direct_use=False,
                        username=username,
                        **kwargs)