OpenStack Identity (Keystone) Client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

adapter.py 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. import warnings
  13. from oslo_serialization import jsonutils
  14. class Adapter(object):
  15. """An instance of a session with local variables.
  16. A session is a global object that is shared around amongst many clients. It
  17. therefore contains state that is relevant to everyone. There is a lot of
  18. state such as the service type and region_name that are only relevant to a
  19. particular client that is using the session. An adapter provides a wrapper
  20. of client local data around the global session object.
  21. :param session: The session object to wrap.
  22. :type session: keystoneclient.session.Session
  23. :param str service_type: The default service_type for URL discovery.
  24. :param str service_name: The default service_name for URL discovery.
  25. :param str interface: The default interface for URL discovery.
  26. :param str region_name: The default region_name for URL discovery.
  27. :param str endpoint_override: Always use this endpoint URL for requests
  28. for this client.
  29. :param tuple version: The version that this API targets.
  30. :param auth: An auth plugin to use instead of the session one.
  31. :type auth: keystoneclient.auth.base.BaseAuthPlugin
  32. :param str user_agent: The User-Agent string to set.
  33. :param int connect_retries: the maximum number of retries that should
  34. be attempted for connection errors.
  35. Default None - use session default which
  36. is don't retry.
  37. :param logger: A logging object to use for requests that pass through this
  38. adapter.
  39. :type logger: logging.Logger
  40. """
  41. def __init__(self, session, service_type=None, service_name=None,
  42. interface=None, region_name=None, endpoint_override=None,
  43. version=None, auth=None, user_agent=None,
  44. connect_retries=None, logger=None):
  45. warnings.warn(
  46. 'keystoneclient.adapter.Adapter is deprecated as of the 2.1.0 '
  47. 'release in favor of keystoneauth1.adapter.Adapter. It will be '
  48. 'removed in future releases.', DeprecationWarning)
  49. # NOTE(jamielennox): when adding new parameters to adapter please also
  50. # add them to the adapter call in httpclient.HTTPClient.__init__
  51. self.session = session
  52. self.service_type = service_type
  53. self.service_name = service_name
  54. self.interface = interface
  55. self.region_name = region_name
  56. self.endpoint_override = endpoint_override
  57. self.version = version
  58. self.user_agent = user_agent
  59. self.auth = auth
  60. self.connect_retries = connect_retries
  61. self.logger = logger
  62. def _set_endpoint_filter_kwargs(self, kwargs):
  63. if self.service_type:
  64. kwargs.setdefault('service_type', self.service_type)
  65. if self.service_name:
  66. kwargs.setdefault('service_name', self.service_name)
  67. if self.interface:
  68. kwargs.setdefault('interface', self.interface)
  69. if self.region_name:
  70. kwargs.setdefault('region_name', self.region_name)
  71. if self.version:
  72. kwargs.setdefault('version', self.version)
  73. def request(self, url, method, **kwargs):
  74. endpoint_filter = kwargs.setdefault('endpoint_filter', {})
  75. self._set_endpoint_filter_kwargs(endpoint_filter)
  76. if self.endpoint_override:
  77. kwargs.setdefault('endpoint_override', self.endpoint_override)
  78. if self.auth:
  79. kwargs.setdefault('auth', self.auth)
  80. if self.user_agent:
  81. kwargs.setdefault('user_agent', self.user_agent)
  82. if self.connect_retries is not None:
  83. kwargs.setdefault('connect_retries', self.connect_retries)
  84. if self.logger:
  85. kwargs.setdefault('logger', self.logger)
  86. return self.session.request(url, method, **kwargs)
  87. def get_token(self, auth=None):
  88. """Return a token as provided by the auth plugin.
  89. :param auth: The auth plugin to use for token. Overrides the plugin
  90. on the session. (optional)
  91. :type auth: :class:`keystoneclient.auth.base.BaseAuthPlugin`
  92. :raises keystoneclient.exceptions.AuthorizationFailure: if a new token
  93. fetch fails.
  94. :returns: A valid token.
  95. :rtype: string
  96. """
  97. return self.session.get_token(auth or self.auth)
  98. def get_endpoint(self, auth=None, **kwargs):
  99. """Get an endpoint as provided by the auth plugin.
  100. :param auth: The auth plugin to use for token. Overrides the plugin on
  101. the session. (optional)
  102. :type auth: :class:`keystoneclient.auth.base.BaseAuthPlugin`
  103. :raises keystoneclient.exceptions.MissingAuthPlugin: if a plugin is not
  104. available.
  105. :returns: An endpoint if available or None.
  106. :rtype: string
  107. """
  108. if self.endpoint_override:
  109. return self.endpoint_override
  110. self._set_endpoint_filter_kwargs(kwargs)
  111. return self.session.get_endpoint(auth or self.auth, **kwargs)
  112. def invalidate(self, auth=None):
  113. """Invalidate an authentication plugin."""
  114. return self.session.invalidate(auth or self.auth)
  115. def get_user_id(self, auth=None):
  116. """Return the authenticated user_id as provided by the auth plugin.
  117. :param auth: The auth plugin to use for token. Overrides the plugin
  118. on the session. (optional)
  119. :type auth: keystoneclient.auth.base.BaseAuthPlugin
  120. :raises keystoneclient.exceptions.AuthorizationFailure:
  121. if a new token fetch fails.
  122. :raises keystoneclient.exceptions.MissingAuthPlugin:
  123. if a plugin is not available.
  124. :returns: Current `user_id` or None if not supported by plugin.
  125. :rtype: string
  126. """
  127. return self.session.get_user_id(auth or self.auth)
  128. def get_project_id(self, auth=None):
  129. """Return the authenticated project_id as provided by the auth plugin.
  130. :param auth: The auth plugin to use for token. Overrides the plugin
  131. on the session. (optional)
  132. :type auth: keystoneclient.auth.base.BaseAuthPlugin
  133. :raises keystoneclient.exceptions.AuthorizationFailure:
  134. if a new token fetch fails.
  135. :raises keystoneclient.exceptions.MissingAuthPlugin:
  136. if a plugin is not available.
  137. :returns: Current `project_id` or None if not supported by plugin.
  138. :rtype: string
  139. """
  140. return self.session.get_project_id(auth or self.auth)
  141. def get(self, url, **kwargs):
  142. return self.request(url, 'GET', **kwargs)
  143. def head(self, url, **kwargs):
  144. return self.request(url, 'HEAD', **kwargs)
  145. def post(self, url, **kwargs):
  146. return self.request(url, 'POST', **kwargs)
  147. def put(self, url, **kwargs):
  148. return self.request(url, 'PUT', **kwargs)
  149. def patch(self, url, **kwargs):
  150. return self.request(url, 'PATCH', **kwargs)
  151. def delete(self, url, **kwargs):
  152. return self.request(url, 'DELETE', **kwargs)
  153. class LegacyJsonAdapter(Adapter):
  154. """Make something that looks like an old HTTPClient.
  155. A common case when using an adapter is that we want an interface similar to
  156. the HTTPClients of old which returned the body as JSON as well.
  157. You probably don't want this if you are starting from scratch.
  158. """
  159. def request(self, *args, **kwargs):
  160. headers = kwargs.setdefault('headers', {})
  161. headers.setdefault('Accept', 'application/json')
  162. try:
  163. kwargs['json'] = kwargs.pop('body')
  164. except KeyError: # nosec(cjschaef): kwargs doesn't contain a 'body'
  165. # key, while 'json' is an optional argument for Session.request
  166. pass
  167. resp = super(LegacyJsonAdapter, self).request(*args, **kwargs)
  168. body = None
  169. if resp.text:
  170. try:
  171. body = jsonutils.loads(resp.text)
  172. except ValueError: # nosec(cjschaef): return None for body as
  173. # expected
  174. pass
  175. return resp, body