Skip to content

Commit 00ef277

Browse files
TerryHoweTerry Howe
authored andcommitted
Add some factories
Add some factories for transport, authenticate, and session. Change-Id: I355b07b2e644358295ba4543cfce3418235ba4ff
1 parent 850c375 commit 00ef277

File tree

9 files changed

+244
-44
lines changed

9 files changed

+244
-44
lines changed

examples/authenticate.py

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
from examples import common
3131
from examples import transport
3232
from openstack.auth import base
33-
from openstack.auth.identity import v2
34-
from openstack.auth.identity import v3
33+
from openstack.auth.identity import authenticator
3534

3635

3736
class TestAuthenticator(base.BaseAuthenticator):
@@ -48,35 +47,14 @@ def get_endpoint(self, transport, service, **kwargs):
4847

4948

5049
def make_authenticate(opts):
51-
"""Create authenticator of some sort."""
52-
token = opts.os_token
53-
username = opts.os_username
54-
password = opts.os_password
55-
auth_url = opts.os_auth_url
56-
project_name = opts.os_project_name
57-
version = opts.os_identity_api_version
58-
if version is None:
59-
version = '3'
60-
else:
61-
version = version.lower().replace('v', '')
62-
version = version.split('.')[0]
63-
if version == '3':
64-
if not token:
65-
args = {'username': username, 'password': password}
66-
if project_name:
67-
args['project_name'] = project_name
68-
return v3.Password(auth_url, **args)
69-
else:
70-
return v3.Token(auth_url, token=token)
71-
elif version == '2':
72-
if not token:
73-
args = {}
74-
if project_name:
75-
args['tenant_name'] = project_name
76-
return v2.Password(auth_url, username, password, **args)
77-
else:
78-
return v2.Token(auth_url, token)
79-
raise Exception("No support for version: %s" % version)
50+
return authenticator.Authenticator.create(
51+
username=opts.os_username,
52+
password=opts.os_password,
53+
token=opts.os_token,
54+
auth_url=opts.os_auth_url,
55+
version=opts.os_identity_api_version,
56+
project_name=opts.os_project_name,
57+
)
8058

8159

8260
def run_authenticate(opts):

examples/session.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,24 @@
2222

2323
import sys
2424

25-
from examples import authenticate
2625
from examples import common
27-
from examples import transport
2826
from openstack.auth import service_filter
2927
from openstack import session
3028

3129

3230
def make_session(opts):
33-
region = opts.os_region
34-
preference = service_filter.ServiceFilter(region=region)
35-
xport = transport.make_transport(opts)
36-
auth = authenticate.make_authenticate(opts)
37-
return session.Session(xport, auth, preference=preference)
31+
return session.Session.create(
32+
username=opts.os_username,
33+
password=opts.os_password,
34+
token=opts.os_token,
35+
auth_url=opts.os_auth_url,
36+
version=opts.os_identity_api_version,
37+
project_name=opts.os_project_name,
38+
cacert=opts.os_cacert,
39+
insecure=opts.insecure,
40+
user_agent='SDKExample',
41+
region=opts.os_region,
42+
)
3843

3944

4045
def run_session(opts):

examples/transport.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@
3030

3131

3232
def make_transport(opts):
33-
# Certificate verification - defaults to True
34-
if opts.os_cacert:
35-
verify = opts.os_cacert
36-
else:
37-
verify = not opts.insecure
38-
return transport.Transport(verify=verify, user_agent=USER_AGENT)
33+
return transport.Transport.create(
34+
cacert=opts.os_cacert,
35+
insecure=opts.insecure,
36+
user_agent=USER_AGENT
37+
)
3938

4039

4140
def run_transport(opts):
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 openstack.auth.identity import v2
14+
from openstack.auth.identity import v3
15+
from openstack import exceptions
16+
17+
18+
def create(username=None, password=None, token=None, auth_url=None,
19+
version='3', project_name=None):
20+
"""Temporary code for creating an authenticator
21+
22+
This is temporary code to create an authenticator. This code will be
23+
removed in the future.
24+
25+
:param string username: User name for authentication.
26+
:param string password: Password associated with the user.
27+
:param string token: Authentication token to use if available.
28+
:param string auth_url: The URL to use for authentication.
29+
:param string version: Version of authentication to use.
30+
:param string project_name: Project name to athenticate.
31+
32+
:returns string: An authenticator.
33+
"""
34+
version = version.lower().replace('v', '')
35+
version = version.split('.')[0]
36+
if version == '3':
37+
if not token:
38+
args = {'username': username, 'password': password}
39+
if project_name:
40+
args['project_name'] = project_name
41+
return v3.Password(auth_url, **args)
42+
else:
43+
return v3.Token(auth_url, token=token)
44+
elif version == '2':
45+
if not token:
46+
args = {}
47+
if project_name:
48+
args['tenant_name'] = project_name
49+
return v2.Password(auth_url, username, password, **args)
50+
else:
51+
return v2.Token(auth_url, token)
52+
msg = ("No support for identity version: %s" % version)
53+
raise exceptions.NoMatchingPlugin(msg)

openstack/session.py

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

1313
import logging
1414

15+
from openstack.auth.identity import authenticator
16+
from openstack.auth import service_filter
17+
from openstack import transport
1518
from openstack import utils
1619

1720

@@ -35,6 +38,28 @@ def __init__(self, transport, authenticator, preference=None):
3538
self.authenticator = authenticator
3639
self.preference = preference
3740

41+
@classmethod
42+
def create(cls, username=None, password=None, token=None, auth_url=None,
43+
version=None, project_name=None, cacert=None, insecure=False,
44+
user_agent=None, region=None):
45+
xport = transport.Transport.create(
46+
cacert=cacert,
47+
insecure=insecure,
48+
user_agent=user_agent,
49+
)
50+
args = {
51+
'username': username,
52+
'password': password,
53+
'token': token,
54+
'auth_url': auth_url,
55+
'project_name': project_name
56+
}
57+
if version:
58+
args['version'] = version
59+
auth = authenticator.create(**args)
60+
preference = service_filter.ServiceFilter(region=region)
61+
return cls(xport, auth, preference=preference)
62+
3863
def _request(self, path, method, service=None, authenticate=True,
3964
**kwargs):
4065
"""Send an HTTP request with the specified characteristics.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 openstack.auth.identity import authenticator
14+
from openstack import exceptions
15+
from openstack.tests import base
16+
17+
18+
class TestAuthenticatorCreate(base.TestCase):
19+
def test_create_3_password(self):
20+
auth = authenticator.create(
21+
username='1',
22+
password='2',
23+
token=None,
24+
auth_url='4',
25+
version='3',
26+
project_name='6',
27+
)
28+
self.assertEqual('1', auth.auth_methods[0].username)
29+
self.assertEqual('2', auth.auth_methods[0].password)
30+
self.assertEqual('4', auth.auth_url)
31+
self.assertEqual('6', auth.project_name)
32+
33+
def test_create_3_token(self):
34+
auth = authenticator.create(
35+
username='1',
36+
password='2',
37+
token='3',
38+
auth_url='4',
39+
version='3',
40+
project_name='6',
41+
)
42+
self.assertEqual('3', auth.auth_methods[0].token)
43+
self.assertEqual('4', auth.auth_url)
44+
45+
def test_create_2_password(self):
46+
auth = authenticator.create(
47+
username='1',
48+
password='2',
49+
token=None,
50+
auth_url='4',
51+
version='2',
52+
project_name='6',
53+
)
54+
self.assertEqual('1', auth.username)
55+
self.assertEqual('2', auth.password)
56+
self.assertEqual('4', auth.auth_url)
57+
self.assertEqual('6', auth.tenant_name)
58+
59+
def test_create_2_token(self):
60+
auth = authenticator.create(
61+
username='1',
62+
password='2',
63+
token='3',
64+
auth_url='4',
65+
version='2',
66+
project_name='6',
67+
)
68+
self.assertEqual('3', auth.token)
69+
self.assertEqual('4', auth.auth_url)
70+
71+
def test_create_bogus(self):
72+
self.assertRaises(
73+
exceptions.NoMatchingPlugin,
74+
authenticator.create,
75+
username='1',
76+
password='2',
77+
token='3',
78+
auth_url='4',
79+
version='99',
80+
project_name='6',
81+
)

openstack/tests/test_session.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,24 @@ def test_patch(self):
8181
self.auth.get_endpoint.assert_called_with(self.xport, self.serv)
8282
url = self.auth.ENDPOINT + self.TEST_PATH
8383
self.xport.request.assert_called_with('PATCH', url, **self.expected)
84+
85+
86+
class TestSessionCreate(base.TestCase):
87+
def test_create(self):
88+
sess = session.Session.create(
89+
username='1',
90+
password='2',
91+
token=None,
92+
auth_url='4',
93+
version='3',
94+
project_name='6',
95+
cacert='7',
96+
insecure='8',
97+
user_agent='9',
98+
region='10',
99+
)
100+
self.assertEqual('1', sess.authenticator.auth_methods[0].username)
101+
self.assertEqual('2', sess.authenticator.auth_methods[0].password)
102+
self.assertEqual('7', sess.transport.verify)
103+
self.assertEqual('9', sess.transport._user_agent)
104+
self.assertEqual('10', sess.preference.region)

openstack/tests/test_transport.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,32 @@ def test_history_matches_requests_json(self):
638638
for r, s in zip(req_resp.history, resp.history):
639639
self.assertEqual(s.url, r.url)
640640
self.assertEqual(s.status_code, r.status_code)
641+
642+
643+
class TestTransporCreate(base.TestCase):
644+
def test_create(self):
645+
xport = transport.Transport.create(
646+
cacert='1',
647+
insecure=False,
648+
user_agent='2',
649+
)
650+
self.assertEqual('1', xport.verify)
651+
self.assertEqual('2', xport._user_agent)
652+
653+
def test_create_no_cert(self):
654+
xport = transport.Transport.create(
655+
cacert=None,
656+
insecure=False,
657+
user_agent='3',
658+
)
659+
self.assertEqual(True, xport.verify)
660+
self.assertEqual('3', xport._user_agent)
661+
662+
def test_create_verify(self):
663+
xport = transport.Transport.create(
664+
cacert=None,
665+
insecure=True,
666+
user_agent='4',
667+
)
668+
self.assertEqual(False, xport.verify)
669+
self.assertEqual('4', xport._user_agent)

openstack/transport.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ def __init__(
8282
self._redirect = redirect
8383
self._accept = accept
8484

85+
@classmethod
86+
def create(cls, cacert=None, insecure=False, user_agent=None):
87+
# Certificate verification - defaults to True
88+
if cacert:
89+
verify = cacert
90+
else:
91+
verify = not insecure
92+
return cls(verify=verify, user_agent=user_agent)
93+
8594
def request(self, method, url, redirect=None, **kwargs):
8695
"""Send a request
8796

0 commit comments

Comments
 (0)