Skip to content

Commit 46f329a

Browse files
committed
Introduce the connection class
The connection class can be made with a transport and/or authenticator that was already constructed or it can be made with nothing. The create method will create a transport, authenticator and session if they are needed. The normal use case would look something like: conn = connection.Connection(**auth_args) Change-Id: I27573bce799d9992748967b5ea1b575139a75853
1 parent 65416f3 commit 46f329a

File tree

9 files changed

+166
-200
lines changed

9 files changed

+166
-200
lines changed

examples/authenticate.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from examples import common
3131
from examples import transport
3232
from openstack.auth import base
33-
from openstack.auth.identity import authenticator
33+
from openstack import connection
3434

3535

3636
class TestAuthenticator(base.BaseAuthPlugin):
@@ -60,7 +60,8 @@ def make_authenticate(opts):
6060
'verify': opts.verify,
6161
'token': opts.token,
6262
}
63-
return authenticator.create(**args)
63+
conn = connection.Connection(**args)
64+
return conn.authenticator
6465

6566

6667
def run_authenticate(opts):

examples/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def option_parser():
120120
'--os-auth-plugin',
121121
dest='auth_plugin',
122122
metavar='<auth-plugin>',
123-
default=env('OS_AUTH_PLUGIN', default='identity_v3'),
123+
default=env('OS_AUTH_PLUGIN', default=None),
124124
help='Authentication plugin (Env: OS_AUTH_PLUGIN)',
125125
)
126126
parser.add_argument(
@@ -205,7 +205,7 @@ def option_parser():
205205
'--os-token',
206206
dest='token',
207207
metavar='<token>',
208-
default=env('OS_TOKEN'),
208+
default=env('OS_TOKEN', default=None),
209209
help='Defaults to env[OS_TOKEN]',
210210
)
211211
parser.add_argument(

examples/session.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
from examples import common
2626
from openstack.auth import service_filter
27-
from openstack import session
27+
from openstack import connection
2828

2929

3030
def make_session(opts):
@@ -41,11 +41,9 @@ def make_session(opts):
4141
'verify': opts.verify,
4242
'token': opts.token,
4343
}
44-
return session.Session.create(
45-
user_agent='SDKExample',
46-
region=opts.region_name,
47-
**args
48-
)
44+
preference = service_filter.ServiceFilter(region=opts.region_name)
45+
conn = connection.Connection(preference=preference, **args)
46+
return conn.session
4947

5048

5149
def run_session(opts):

openstack/auth/identity/authenticator.py

Lines changed: 0 additions & 52 deletions
This file was deleted.

openstack/connection.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
from stevedore import driver
14+
15+
from openstack import exceptions
16+
from openstack import session
17+
from openstack import transport as xport
18+
19+
USER_AGENT = 'OSPythonSDK'
20+
21+
22+
class Connection(object):
23+
AUTH_PLUGIN_NAMESPACE = "openstack.auth.plugin"
24+
25+
def __init__(self, transport=None, authenticator=None, preference=None,
26+
verify=True, user_agent=USER_AGENT,
27+
auth_plugin=None, **auth_args):
28+
"""Connection to a cloud provider.
29+
30+
Context for a connection to a cloud provider. The context generally
31+
contains a transport, authenticator, and sesssion. You may pass in
32+
a previously created transport and authenticator or you may pass in
33+
the parameters to create a transport and authenticator.
34+
35+
:param transport: A transport to make HTTP requests.
36+
:param authenticator: Authenticator to authenticate session.
37+
:param preference: User service preferences.
38+
:param boolean/string verify: ``True`` to verify SSL or CA_BUNDLE path.
39+
:param string user_agent: Value for ``User-Agent`` header.
40+
:param string auth_plugin: Name of the authorization plugin.
41+
:param auth_args: Arguments to create authenticator.
42+
"""
43+
self.transport = self._create_transport(transport, verify, user_agent)
44+
self.authenticator = self._create_authenticator(authenticator,
45+
auth_plugin,
46+
**auth_args)
47+
self.session = session.Session(self.transport, self.authenticator,
48+
preference)
49+
50+
def _create_transport(self, transport, verify, user_agent):
51+
if transport:
52+
return transport
53+
return xport.Transport(verify=verify, user_agent=user_agent)
54+
55+
def _create_authenticator(self, authenticator, auth_plugin, **auth_args):
56+
if authenticator:
57+
return authenticator
58+
if auth_plugin is None:
59+
if 'auth_url' not in auth_args:
60+
msg = ("auth_url was not provided.")
61+
raise exceptions.AuthorizationFailure(msg)
62+
auth_url = auth_args['auth_url']
63+
endpoint_version = auth_url.split('v')[-1][0]
64+
if endpoint_version == '2':
65+
auth_plugin = 'identity_v2'
66+
else:
67+
auth_plugin = 'identity_v3'
68+
69+
mgr = driver.DriverManager(
70+
namespace=self.AUTH_PLUGIN_NAMESPACE,
71+
name=auth_plugin,
72+
invoke_on_load=False,
73+
)
74+
plugin = mgr.driver
75+
valid_list = plugin.valid_options
76+
args = dict((n, auth_args[n]) for n in valid_list if n in auth_args)
77+
return plugin(**args)

openstack/session.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212

1313
import logging
1414

15-
from openstack.auth.identity import authenticator
16-
from openstack.auth import service_filter
17-
from openstack import transport
1815
from openstack import utils
1916

2017

@@ -38,13 +35,6 @@ def __init__(self, transport, authenticator, preference=None):
3835
self.authenticator = authenticator
3936
self.preference = preference
4037

41-
@classmethod
42-
def create(cls, verify=True, region=None, **auth_args):
43-
xport = transport.Transport(verify=verify)
44-
auth = authenticator.create(**auth_args)
45-
preference = service_filter.ServiceFilter(region=region)
46-
return cls(xport, auth, preference=preference)
47-
4838
def _request(self, path, method, service=None, authenticate=True,
4939
**kwargs):
5040
"""Send an HTTP request with the specified characteristics.

openstack/tests/auth/identity/test_authenticator.py

Lines changed: 0 additions & 110 deletions
This file was deleted.

0 commit comments

Comments
 (0)