Skip to content

Commit 89a7f51

Browse files
committed
Moves some assumptions that were being made in tests.
Converts usages of SoftLayer.Client to SoftLayer.create_client_from_env
1 parent 200787d commit 89a7f51

10 files changed

Lines changed: 40 additions & 35 deletions

File tree

SoftLayer/API.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def __getitem__(self, name):
156156
157157
Usage:
158158
>>> import SoftLayer
159-
>>> client = SoftLayer.Client()
159+
>>> client = SoftLayer.create_client_from_env()
160160
>>> client['Account']
161161
<Service: Account>
162162
@@ -176,7 +176,7 @@ def call(self, service, method, *args, **kwargs):
176176
177177
Usage:
178178
>>> import SoftLayer
179-
>>> client = SoftLayer.Client()
179+
>>> client = SoftLayer.create_client_from_env()
180180
>>> client['Account'].getVirtualGuests(mask="id", limit=10)
181181
[...]
182182
@@ -321,7 +321,7 @@ def call(self, name, *args, **kwargs):
321321
322322
Usage:
323323
>>> import SoftLayer
324-
>>> client = SoftLayer.Client()
324+
>>> client = SoftLayer.create_client_from_env()
325325
>>> client['Account'].getVirtualGuests(mask="id", limit=10)
326326
[...]
327327
@@ -341,7 +341,7 @@ def iter_call(self, name, *args, **kwargs):
341341
342342
Usage:
343343
>>> import SoftLayer
344-
>>> client = SoftLayer.Client()
344+
>>> client = SoftLayer.create_client_from_env()
345345
>>> gen = client['Account'].getVirtualGuests(iter=True)
346346
>>> for virtual_guest in gen:
347347
... virtual_guest['id']

SoftLayer/CLI/config/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66

77
def get_settings_from_client(client):
8-
"""Pull out settings from a SoftLayer.Client instance.
8+
"""Pull out settings from a SoftLayer.BaseClient instance.
99
10-
:param client: SoftLayer.Client instance
10+
:param client: SoftLayer.BaseClient instance
1111
"""
1212
settings = {
1313
'username': '',

SoftLayer/CLI/core.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,11 @@ def cli(ctx,
144144
transport = SoftLayer.XmlRpcTransport()
145145

146146
wrapped_transport = SoftLayer.TimingTransport(transport)
147-
env.client = SoftLayer.Client(proxy=proxy,
148-
config_file=config,
149-
transport=wrapped_transport)
147+
env.client = SoftLayer.create_client_from_env(
148+
proxy=proxy,
149+
config_file=config,
150+
transport=wrapped_transport,
151+
)
150152

151153

152154
@cli.resultcallback()

SoftLayer/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
Usage:
77
88
>>> import SoftLayer
9-
>>> client = SoftLayer.Client(username="username", api_key="api_key")
9+
>>> client = SoftLayer.create_client_from_env(username="username",
10+
api_key="api_key")
1011
>>> resp = client['Account'].getObject()
1112
>>> resp['companyName']
1213
'Your Company'

SoftLayer/managers/metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class MetadataManager(object):
4343
Usage:
4444
4545
>>> import SoftLayer
46-
>>> client = SoftLayer.Client()
46+
>>> client = SoftLayer.create_client_from_env()
4747
>>> from SoftLayer import MetadataManager
4848
>>> meta = MetadataManager(client)
4949
>>> meta.get('datacenter')

SoftLayer/managers/vs.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def list_instances(self, hourly=True, monthly=True, tags=None, cpus=None,
6464
# SL_USERNAME = YOUR_USERNAME
6565
# SL_API_KEY = YOUR_API_KEY
6666
import SoftLayer
67-
client = SoftLayer.Client()
67+
client = SoftLayer.create_client_from_env()
6868
6969
mgr = SoftLayer.VSManager(client)
7070
for vsi in mgr.list_instances(hourly=True, datacenter='dal05'):
@@ -154,7 +154,7 @@ def get_instance(self, instance_id, **kwargs):
154154
# SL_USERNAME = YOUR_USERNAME
155155
# SL_API_KEY = YOUR_API_KEY
156156
import SoftLayer
157-
client = SoftLayer.Client()
157+
client = SoftLayer.create_client_from_env()
158158
159159
mgr = SoftLayer.VSManager(client)
160160
vsi = mgr.get_instance(12345)
@@ -226,7 +226,7 @@ def cancel_instance(self, instance_id):
226226
# SL_USERNAME = YOUR_USERNAME
227227
# SL_API_KEY = YOUR_API_KEY
228228
import SoftLayer
229-
client = SoftLayer.Client()
229+
client = SoftLayer.create_client_from_env()
230230
231231
mgr = SoftLayer.VSManager(client)
232232
mgr.cancel_instance(12345)
@@ -249,7 +249,7 @@ def reload_instance(self, instance_id, post_uri=None, ssh_keys=None):
249249
# SL_USERNAME = YOUR_USERNAME
250250
# SL_API_KEY = YOUR_API_KEY
251251
import SoftLayer
252-
client = SoftLayer.Client()
252+
client = SoftLayer.create_client_from_env()
253253
254254
post_uri = 'https://somehost.com/bootstrap.sh'
255255
mgr = SoftLayer.VSManager(client)
@@ -606,7 +606,7 @@ def upgrade(self, instance_id, cpus=None, memory=None,
606606
607607
# Upgrade instance 12345 to 4 CPUs and 4 GB of memory
608608
import SoftLayer
609-
client = SoftLayer.Client(config="~/.softlayer")
609+
client = SoftLayer.create_client_from_env()
610610
611611
mgr = SoftLayer.VSManager(client)
612612
mgr.upgrade(12345, cpus=4, memory=4)

SoftLayer/testing/__init__.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,13 @@ def tear_down(self):
8484

8585
def setUp(self): # NOQA
8686
testtools.TestCase.setUp(self)
87-
self.env = environment.Environment()
8887

8988
# Create a crazy mockable, fixture client
90-
auth = SoftLayer.BasicAuthentication('default-user', 'default-key')
9189
self.mocks = MockableTransport(SoftLayer.FixtureTransport())
9290
self.transport = SoftLayer.TimingTransport(self.mocks)
93-
self.client = SoftLayer.Client(
94-
transport=self.transport,
95-
auth=auth,
96-
timeout=10,
97-
endpoint_url='default-endpoint-url')
91+
self.client = SoftLayer.BaseClient(transport=self.transport)
9892

93+
self.env = environment.Environment()
9994
self.env.client = self.client
10095
return self.set_up()
10196

SoftLayer/tests/CLI/modules/config_tests.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,32 @@
99

1010
import mock
1111

12+
import SoftLayer
13+
from SoftLayer import auth
1214
from SoftLayer.CLI.config import setup as config
1315
from SoftLayer.CLI import exceptions
1416
from SoftLayer import consts
1517
from SoftLayer import testing
18+
from SoftLayer import transports
1619

1720

1821
class TestHelpShow(testing.TestCase):
1922

23+
def set_up(self):
24+
transport = transports.XmlRpcTransport(
25+
endpoint_url='http://endpoint-url',
26+
)
27+
self.env.client = SoftLayer.BaseClient(
28+
transport=transport,
29+
auth=auth.BasicAuthentication('username', 'api-key'))
30+
2031
def test_show(self):
2132
result = self.run_command(['config', 'show'])
2233

2334
self.assertEqual(result.exit_code, 0)
2435
self.assertEqual(json.loads(result.output),
25-
{'Username': 'default-user',
26-
'API Key': 'default-key',
36+
{'Username': 'username',
37+
'API Key': 'api-key',
2738
'Endpoint URL': 'not set',
2839
'Timeout': 'not set'})
2940

SoftLayer/tests/api_tests.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,20 @@
99
import SoftLayer
1010
import SoftLayer.API
1111
from SoftLayer import testing
12-
13-
TEST_AUTH_HEADERS = {
14-
'authenticate': {'username': 'default-user', 'apiKey': 'default-key'}
15-
}
12+
from SoftLayer import transports
1613

1714

1815
class Inititialization(testing.TestCase):
1916
def test_init(self):
2017
client = SoftLayer.Client(username='doesnotexist',
21-
api_key='issurelywrong', timeout=10)
18+
api_key='issurelywrong',
19+
timeout=10)
2220

2321
self.assertIsInstance(client.auth, SoftLayer.BasicAuthentication)
2422
self.assertEqual(client.auth.username, 'doesnotexist')
2523
self.assertEqual(client.auth.api_key, 'issurelywrong')
26-
self.assertEqual(client.transport.endpoint_url,
27-
SoftLayer.API_PUBLIC_ENDPOINT.rstrip('/'))
24+
self.assertIsNotNone(client.transport)
25+
self.assertIsInstance(client.transport, transports.XmlRpcTransport)
2826
self.assertEqual(client.transport.timeout, 10)
2927

3028
@mock.patch('SoftLayer.config.get_client_settings')
@@ -80,7 +78,6 @@ def test_simple_call(self):
8078
args=tuple(),
8179
limit=None,
8280
offset=None,
83-
headers=TEST_AUTH_HEADERS,
8481
)
8582

8683
def test_complex(self):
@@ -104,7 +101,6 @@ def test_complex(self):
104101
args=(1234,),
105102
limit=9,
106103
offset=10,
107-
headers=TEST_AUTH_HEADERS,
108104
)
109105

110106
@mock.patch('SoftLayer.API.BaseClient.iter_call')

docs/config_file.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Configuration File
66
The SoftLayer API bindings load your settings from a number of different
77
locations.
88

9-
* Input directly into SoftLayer.Client(...)
9+
* Input directly into SoftLayer.create_client_from_env(...)
1010
* Enviorment variables (`SL_USERNAME`, `SL_API_KEY`)
1111
* Config file locations (`~/.softlayer`, `/etc/softlayer.conf`)
1212
* Or argument (`-C/path/to/config` or `--config=/path/to/config`)

0 commit comments

Comments
 (0)