Debian Codegraph @

neutronclient/common/clientmanager.py

raw
1 # Copyright 2012 OpenStack Foundation.
2 # All Rights Reserved
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16
17 """Manage access to the clients, including authenticating when needed.
18 """
19
20 import debtcollector.renames
21
22 from neutronclient import client
23 from neutronclient.neutron import client as neutron_client
24
25
26 class ClientCache(object):
27 """Descriptor class for caching created client handles."""
28
29 def __init__(self, factory):
30 self.factory = factory
31 self._handle = None
32
33 def __get__(self, instance, owner):
34 # Tell the ClientManager to login to keystone
35 if self._handle is None:
36 self._handle = self.factory(instance)
37 return self._handle
38
39
40 class ClientManager(object):
41 """Manages access to API clients, including authentication."""
42 neutron = ClientCache(neutron_client.make_client)
43 # Provide support for old quantum commands (for example
44 # in stable versions)
45 quantum = neutron
46
47 @debtcollector.renames.renamed_kwarg(
48 'tenant_id', 'project_id', replace=True)
49 @debtcollector.renames.renamed_kwarg(
50 'tenant_name', 'project_name', replace=True)
51 def __init__(self, token=None, url=None,
52 auth_url=None,
53 endpoint_type=None,
54 project_name=None,
55 project_id=None,
56 username=None,
57 user_id=None,
58 password=None,
59 region_name=None,
60 api_version=None,
61 auth_strategy=None,
62 insecure=False,
63 ca_cert=None,
64 log_credentials=False,
65 service_type=None,
66 service_name=None,
67 timeout=None,
68 retries=0,
69 raise_errors=True,
70 session=None,
71 auth=None,
72 ):
73 self._token = token
74 self._url = url
75 self._auth_url = auth_url
76 self._service_type = service_type
77 self._service_name = service_name
78 self._endpoint_type = endpoint_type
79 self._project_name = project_name
80 self._project_id = project_id
81 self._username = username
82 self._user_id = user_id
83 self._password = password
84 self._region_name = region_name
85 self._api_version = api_version
86 self._service_catalog = None
87 self._auth_strategy = auth_strategy
88 self._insecure = insecure
89 self._ca_cert = ca_cert
90 self._log_credentials = log_credentials
91 self._timeout = timeout
92 self._retries = retries
93 self._raise_errors = raise_errors
94 self._session = session
95 self._auth = auth
96 return
97
98 def initialize(self):
99 if not self._url:
100 httpclient = client.construct_http_client(
101 username=self._username,
102 user_id=self._user_id,
103 project_name=self._project_name,
104 project_id=self._project_id,
105 password=self._password,
106 region_name=self._region_name,
107 auth_url=self._auth_url,
108 service_type=self._service_type,
109 service_name=self._service_name,
110 endpoint_type=self._endpoint_type,
111 insecure=self._insecure,
112 ca_cert=self._ca_cert,
113 timeout=self._timeout,
114 session=self._session,
115 auth=self._auth,
116 log_credentials=self._log_credentials)
117 httpclient.authenticate()
118 # Populate other password flow attributes
119 self._token = httpclient.auth_token
120 self._url = httpclient.endpoint_url