Skip to content

Commit c925e58

Browse files
Merge pull request #1084 from allmightyspiff/1074
#1074 - fixed issue with config setup
2 parents 42cd438 + 5dc451f commit c925e58

File tree

5 files changed

+46
-33
lines changed

5 files changed

+46
-33
lines changed

SoftLayer/CLI/config/setup.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import click
66

77
import SoftLayer
8-
from SoftLayer import auth
98
from SoftLayer.CLI import config
109
from SoftLayer.CLI import environment
1110
from SoftLayer.CLI import exceptions
@@ -22,7 +21,6 @@ def get_api_key(client, username, secret):
2221
# Try to use a client with username/api key
2322
if len(secret) == 64:
2423
try:
25-
client.auth = auth.BasicAuthentication(username, secret)
2624
client['Account'].getCurrentUser()
2725
return secret
2826
except SoftLayer.SoftLayerAPIError as ex:
@@ -32,12 +30,10 @@ def get_api_key(client, username, secret):
3230
# Try to use a client with username/password
3331
client.authenticate_with_password(username, secret)
3432

35-
user_record = client['Account'].getCurrentUser(
36-
mask='id, apiAuthenticationKeys')
33+
user_record = client['Account'].getCurrentUser(mask='id, apiAuthenticationKeys')
3734
api_keys = user_record['apiAuthenticationKeys']
3835
if len(api_keys) == 0:
39-
return client['User_Customer'].addApiAuthenticationKey(
40-
id=user_record['id'])
36+
return client['User_Customer'].addApiAuthenticationKey(id=user_record['id'])
4137
return api_keys[0]['authenticationKey']
4238

4339

@@ -47,9 +43,8 @@ def cli(env):
4743
"""Edit configuration."""
4844

4945
username, secret, endpoint_url, timeout = get_user_input(env)
50-
51-
env.client.transport.transport.endpoint_url = endpoint_url
52-
api_key = get_api_key(env.client, username, secret)
46+
new_client = SoftLayer.Client(username=username, api_key=secret, endpoint_url=endpoint_url, timeout=timeout)
47+
api_key = get_api_key(new_client, username, secret)
5348

5449
path = '~/.softlayer'
5550
if env.config_file:
@@ -103,17 +98,20 @@ def get_user_input(env):
10398
secret = env.getpass('API Key or Password', default=defaults['api_key'])
10499

105100
# Ask for which endpoint they want to use
101+
endpoint = defaults.get('endpoint_url', 'public')
106102
endpoint_type = env.input(
107-
'Endpoint (public|private|custom)', default='public')
103+
'Endpoint (public|private|custom)', default=endpoint)
108104
endpoint_type = endpoint_type.lower()
109105

110-
if endpoint_type == 'custom':
111-
endpoint_url = env.input('Endpoint URL',
112-
default=defaults['endpoint_url'])
106+
if endpoint_type == 'public':
107+
endpoint_url = SoftLayer.API_PUBLIC_ENDPOINT
113108
elif endpoint_type == 'private':
114109
endpoint_url = SoftLayer.API_PRIVATE_ENDPOINT
115110
else:
116-
endpoint_url = SoftLayer.API_PUBLIC_ENDPOINT
111+
if endpoint_type == 'custom':
112+
endpoint_url = env.input('Endpoint URL', default=endpoint)
113+
else:
114+
endpoint_url = endpoint_type
117115

118116
# Ask for timeout
119117
timeout = env.input('Timeout', default=defaults['timeout'] or 0)

SoftLayer/CLI/order/item_list.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from SoftLayer.managers import ordering
88
from SoftLayer.utils import lookup
99

10-
COLUMNS = ['category', 'keyName', 'description']
10+
COLUMNS = ['category', 'keyName', 'description', 'priceId']
1111

1212

1313
@click.command()
@@ -60,7 +60,7 @@ def cli(env, package_keyname, keyword, category):
6060
categories = sorted_items.keys()
6161
for catname in sorted(categories):
6262
for item in sorted_items[catname]:
63-
table.add_row([catname, item['keyName'], item['description']])
63+
table.add_row([catname, item['keyName'], item['description'], get_price(item)])
6464
env.fout(table)
6565

6666

@@ -75,3 +75,12 @@ def sort_items(items):
7575
sorted_items[category].append(item)
7676

7777
return sorted_items
78+
79+
80+
def get_price(item):
81+
"""Given an SoftLayer_Product_Item, returns its default price id"""
82+
83+
for price in item.get('prices', []):
84+
if not price.get('locationGroupId'):
85+
return price.get('id')
86+
return 0

SoftLayer/managers/ordering.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
itemCategory[id, name, categoryCode]
1717
'''
1818

19-
ITEM_MASK = '''id, keyName, description, itemCategory, categories'''
19+
ITEM_MASK = '''id, keyName, description, itemCategory, categories, prices'''
2020

2121
PACKAGE_MASK = '''id, name, keyName, isActive, type'''
2222

tests/CLI/modules/config_tests.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,29 @@ def set_up(self):
5151
transport = testing.MockableTransport(SoftLayer.FixtureTransport())
5252
self.env.client = SoftLayer.BaseClient(transport=transport)
5353

54+
@mock.patch('SoftLayer.Client')
5455
@mock.patch('SoftLayer.CLI.formatting.confirm')
5556
@mock.patch('SoftLayer.CLI.environment.Environment.getpass')
5657
@mock.patch('SoftLayer.CLI.environment.Environment.input')
57-
def test_setup(self, mocked_input, getpass, confirm_mock):
58+
def test_setup(self, mocked_input, getpass, confirm_mock, client):
59+
client.return_value = self.env.client
5860
if(sys.platform.startswith("win")):
5961
self.skipTest("Test doesn't work in Windows")
6062
with tempfile.NamedTemporaryFile() as config_file:
6163
confirm_mock.return_value = True
6264
getpass.return_value = 'A' * 64
6365
mocked_input.side_effect = ['user', 'public', 0]
6466

65-
result = self.run_command(['--config=%s' % config_file.name,
66-
'config', 'setup'])
67+
result = self.run_command(['--config=%s' % config_file.name, 'config', 'setup'])
6768

6869
self.assert_no_fail(result)
69-
self.assertTrue('Configuration Updated Successfully'
70-
in result.output)
70+
self.assertTrue('Configuration Updated Successfully' in result.output)
7171
contents = config_file.read().decode("utf-8")
72+
7273
self.assertTrue('[softlayer]' in contents)
7374
self.assertTrue('username = user' in contents)
74-
self.assertTrue('api_key = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
75-
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAA' in contents)
76-
self.assertTrue('endpoint_url = %s' % consts.API_PUBLIC_ENDPOINT
77-
in contents)
75+
self.assertTrue('api_key = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' in contents)
76+
self.assertTrue('endpoint_url = %s' % consts.API_PUBLIC_ENDPOINT in contents)
7877

7978
@mock.patch('SoftLayer.CLI.formatting.confirm')
8079
@mock.patch('SoftLayer.CLI.environment.Environment.getpass')
@@ -115,6 +114,17 @@ def test_get_user_input_custom(self, mocked_input, getpass):
115114

116115
self.assertEqual(endpoint_url, 'custom-endpoint')
117116

117+
@mock.patch('SoftLayer.CLI.environment.Environment.getpass')
118+
@mock.patch('SoftLayer.CLI.environment.Environment.input')
119+
def test_github_1074(self, mocked_input, getpass):
120+
"""Tests to make sure directly using an endpoint works"""
121+
getpass.return_value = 'A' * 64
122+
mocked_input.side_effect = ['user', 'test-endpoint', 0]
123+
124+
_, _, endpoint_url, _ = config.get_user_input(self.env)
125+
126+
self.assertEqual(endpoint_url, 'test-endpoint')
127+
118128
@mock.patch('SoftLayer.CLI.environment.Environment.getpass')
119129
@mock.patch('SoftLayer.CLI.environment.Environment.input')
120130
def test_get_user_input_default(self, mocked_input, getpass):

tests/CLI/modules/order_tests.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,9 @@ def test_item_list(self):
3838

3939
self.assert_no_fail(result)
4040
self.assert_called_with('SoftLayer_Product_Package', 'getItems')
41-
expected_results = [{'category': 'testing',
42-
'keyName': 'item1',
43-
'description': 'description1'},
44-
{'category': 'testing',
45-
'keyName': 'item2',
46-
'description': 'description2'}]
47-
self.assertEqual(expected_results, json.loads(result.output))
41+
self.assertIn('description2', result.output)
42+
self.assertIn('testing', result.output)
43+
self.assertIn('item2', result.output)
4844

4945
def test_package_list(self):
5046
p_mock = self.set_mock('SoftLayer_Product_Package', 'getAllObjects')

0 commit comments

Comments
 (0)