keystone  18.0.0
About: OpenStack Keystone (Core Service: Identity) provides an authentication and authorization service for other OpenStack services. Provides a catalog of endpoints for all OpenStack services.
The "Victoria" series (maintained release).
  Fossies Dox: keystone-18.0.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

provider_api.py
Go to the documentation of this file.
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 
13 
14 class ProviderAPIRegistry(object):
15  __shared_object_state = {}
16  __registry = {}
17  __iter__ = __registry.__iter__
18  __getitem__ = __registry.__getitem__
19  locked = False
20 
21  def __init__(self):
22  # NOTE(morgan): This rebinds __dict__ and allows all instances of
23  # the provider API to share a common state. Any changes except
24  # rebinding __dict__ will maintain the same state stored on the class
25  # not the instance. This design pattern is preferable to
26  # full singletons where state sharing is the important "feature"
27  # derived from the "singleton"
28  #
29  # Use "super" to bypass the __setattr__ preventing changes to the
30  # object itself.
31  super(ProviderAPIRegistry, self).__setattr__(
32  '__dict__', self.__shared_object_state)
33 
34  def __getattr__(self, item):
35  """Do attr lookup."""
36  try:
37  return self.__registry[item]
38  except KeyError:
39  raise AttributeError(
40  "'ProviderAPIs' has no attribute %s" % item)
41 
42  def __setattr__(self, key, value):
43  """Do not allow setting values on the registry object."""
44  raise RuntimeError('Programming Error: You may not set values on the '
45  'ProviderAPIRegistry objects.')
46 
47  def _register_provider_api(self, name, obj):
48  """Register an instance of a class as a provider api."""
49  if name == 'driver':
50  raise ValueError('A provider may not be named "driver".')
51 
52  if self.locked:
53  raise RuntimeError(
54  'Programming Error: The provider api registry has been '
55  'locked (post configuration). Ensure all provider api '
56  'managers are instantiated before locking.')
57 
58  if name in self.__registry:
60  '`%(name)s` has already been registered as an api '
61  'provider by `%(prov)r`' % {'name': name,
62  'prov': self.__registry[name]})
63  self.__registry[name] = obj
64 
66  """ONLY USED FOR TESTING."""
67  self.__registry.clear()
68  # Use super to allow setting around class implementation of __setattr__
69  super(ProviderAPIRegistry, self).__setattr__('locked', False)
70 
72  # Use super to allow setting around class implementation of __setattr__
73  super(ProviderAPIRegistry, self).__setattr__('locked', True)
74 
75  def deferred_provider_lookup(self, api, method):
76  """Create descriptor that performs lookup of api and method on demand.
77 
78  For specialized cases, such as the enforcer "get_member_from_driver"
79  which needs to be effectively a "classmethod", this method returns
80  a smart descriptor object that does the lookup at runtime instead of
81  at import time.
82 
83  :param api: The api to use, e.g. "identity_api"
84  :type api: str
85  :param method: the method on the api to return
86  :type method: str
87  """
88  class DeferredProviderLookup(object):
89  def __init__(self, api, method):
90  self.__api = api
91  self.__method = method
92 
93  def __get__(self, instance, owner):
94  api = getattr(ProviderAPIs, self.__api)
95  return getattr(api, self.__method)
96 
97  return DeferredProviderLookup(api, method)
98 
99 
100 class DuplicateProviderError(Exception):
101  """Attempting to register a duplicate API provider."""
102 
103 
104 class ProviderAPIMixin(object):
105  """Allow referencing provider apis on self via __getattr__.
106 
107  Be sure this class is first in the class definition for inheritance.
108  """
109 
110  def __getattr__(self, item):
111  """Magic getattr method."""
112  try:
113  return getattr(ProviderAPIs, item)
114  except AttributeError:
115  return self.__getattribute__(item)
116 
117 
118 ProviderAPIs = ProviderAPIRegistry()
keystone.common.provider_api.ProviderAPIRegistry._register_provider_api
def _register_provider_api(self, name, obj)
Definition: provider_api.py:47
keystone.common.provider_api.ProviderAPIRegistry.__method
__method
Definition: provider_api.py:91
keystone.common.provider_api.ProviderAPIRegistry
Definition: provider_api.py:14
keystone.common.provider_api.DuplicateProviderError
Definition: provider_api.py:100
keystone.common.provider_api.ProviderAPIRegistry.locked
bool locked
Definition: provider_api.py:19
keystone.common.provider_api.ProviderAPIRegistry.__api
__api
Definition: provider_api.py:90
keystone.common.provider_api.ProviderAPIRegistry.__init__
def __init__(self)
Definition: provider_api.py:21
keystone.common.provider_api.ProviderAPIRegistry.__registry
dictionary __registry
Definition: provider_api.py:16
keystone.common.provider_api.ProviderAPIRegistry.lock_provider_registry
def lock_provider_registry(self)
Definition: provider_api.py:71
keystone.common.provider_api.ProviderAPIMixin.__getattr__
def __getattr__(self, item)
Definition: provider_api.py:110
keystone.common.provider_api.ProviderAPIRegistry.__getattr__
def __getattr__(self, item)
Definition: provider_api.py:34
keystone.common.provider_api.ProviderAPIRegistry.__setattr__
def __setattr__(self, key, value)
Definition: provider_api.py:42
keystone.common.provider_api.ProviderAPIRegistry._clear_registry_instances
def _clear_registry_instances(self)
Definition: provider_api.py:65
keystone.common.provider_api.ProviderAPIRegistry.__shared_object_state
dictionary __shared_object_state
Definition: provider_api.py:15
keystone.common.provider_api.ProviderAPIRegistry.deferred_provider_lookup
def deferred_provider_lookup(self, api, method)
Definition: provider_api.py:75