Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions SoftLayer/CLI/config/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import click

import SoftLayer
from SoftLayer import auth
from SoftLayer.CLI import config
from SoftLayer.CLI import environment
from SoftLayer.CLI import exceptions
Expand All @@ -22,7 +21,6 @@ def get_api_key(client, username, secret):
# Try to use a client with username/api key
if len(secret) == 64:
try:
client.auth = auth.BasicAuthentication(username, secret)
client['Account'].getCurrentUser()
return secret
except SoftLayer.SoftLayerAPIError as ex:
Expand All @@ -32,12 +30,10 @@ def get_api_key(client, username, secret):
# Try to use a client with username/password
client.authenticate_with_password(username, secret)

user_record = client['Account'].getCurrentUser(
mask='id, apiAuthenticationKeys')
user_record = client['Account'].getCurrentUser(mask='id, apiAuthenticationKeys')
api_keys = user_record['apiAuthenticationKeys']
if len(api_keys) == 0:
return client['User_Customer'].addApiAuthenticationKey(
id=user_record['id'])
return client['User_Customer'].addApiAuthenticationKey(id=user_record['id'])
return api_keys[0]['authenticationKey']


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

username, secret, endpoint_url, timeout = get_user_input(env)

env.client.transport.transport.endpoint_url = endpoint_url
api_key = get_api_key(env.client, username, secret)
new_client = SoftLayer.Client(username=username, api_key=secret, endpoint_url=endpoint_url, timeout=timeout)
api_key = get_api_key(new_client, username, secret)

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

# Ask for which endpoint they want to use
endpoint = defaults.get('endpoint_url', 'public')
endpoint_type = env.input(
'Endpoint (public|private|custom)', default='public')
'Endpoint (public|private|custom)', default=endpoint)
endpoint_type = endpoint_type.lower()

if endpoint_type == 'custom':
endpoint_url = env.input('Endpoint URL',
default=defaults['endpoint_url'])
if endpoint_type == 'public':
endpoint_url = SoftLayer.API_PUBLIC_ENDPOINT
elif endpoint_type == 'private':
endpoint_url = SoftLayer.API_PRIVATE_ENDPOINT
else:
endpoint_url = SoftLayer.API_PUBLIC_ENDPOINT
if endpoint_type == 'custom':
endpoint_url = env.input('Endpoint URL', default=endpoint)
else:
endpoint_url = endpoint_type

# Ask for timeout
timeout = env.input('Timeout', default=defaults['timeout'] or 0)
Expand Down
13 changes: 11 additions & 2 deletions SoftLayer/CLI/order/item_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from SoftLayer.managers import ordering
from SoftLayer.utils import lookup

COLUMNS = ['category', 'keyName', 'description']
COLUMNS = ['category', 'keyName', 'description', 'priceId']


@click.command()
Expand Down Expand Up @@ -60,7 +60,7 @@ def cli(env, package_keyname, keyword, category):
categories = sorted_items.keys()
for catname in sorted(categories):
for item in sorted_items[catname]:
table.add_row([catname, item['keyName'], item['description']])
table.add_row([catname, item['keyName'], item['description'], get_price(item)])
env.fout(table)


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

return sorted_items


def get_price(item):
"""Given an SoftLayer_Product_Item, returns its default price id"""

for price in item.get('prices', []):
if not price.get('locationGroupId'):
return price.get('id')
return 0
2 changes: 1 addition & 1 deletion SoftLayer/managers/ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
itemCategory[id, name, categoryCode]
'''

ITEM_MASK = '''id, keyName, description, itemCategory, categories'''
ITEM_MASK = '''id, keyName, description, itemCategory, categories, prices'''

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

Expand Down
28 changes: 19 additions & 9 deletions tests/CLI/modules/config_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,29 @@ def set_up(self):
transport = testing.MockableTransport(SoftLayer.FixtureTransport())
self.env.client = SoftLayer.BaseClient(transport=transport)

@mock.patch('SoftLayer.Client')
@mock.patch('SoftLayer.CLI.formatting.confirm')
@mock.patch('SoftLayer.CLI.environment.Environment.getpass')
@mock.patch('SoftLayer.CLI.environment.Environment.input')
def test_setup(self, mocked_input, getpass, confirm_mock):
def test_setup(self, mocked_input, getpass, confirm_mock, client):
client.return_value = self.env.client
if(sys.platform.startswith("win")):
self.skipTest("Test doesn't work in Windows")
with tempfile.NamedTemporaryFile() as config_file:
confirm_mock.return_value = True
getpass.return_value = 'A' * 64
mocked_input.side_effect = ['user', 'public', 0]

result = self.run_command(['--config=%s' % config_file.name,
'config', 'setup'])
result = self.run_command(['--config=%s' % config_file.name, 'config', 'setup'])

self.assert_no_fail(result)
self.assertTrue('Configuration Updated Successfully'
in result.output)
self.assertTrue('Configuration Updated Successfully' in result.output)
contents = config_file.read().decode("utf-8")

self.assertTrue('[softlayer]' in contents)
self.assertTrue('username = user' in contents)
self.assertTrue('api_key = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAA' in contents)
self.assertTrue('endpoint_url = %s' % consts.API_PUBLIC_ENDPOINT
in contents)
self.assertTrue('api_key = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' in contents)
self.assertTrue('endpoint_url = %s' % consts.API_PUBLIC_ENDPOINT in contents)

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

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

@mock.patch('SoftLayer.CLI.environment.Environment.getpass')
@mock.patch('SoftLayer.CLI.environment.Environment.input')
def test_github_1074(self, mocked_input, getpass):
"""Tests to make sure directly using an endpoint works"""
getpass.return_value = 'A' * 64
mocked_input.side_effect = ['user', 'test-endpoint', 0]

_, _, endpoint_url, _ = config.get_user_input(self.env)

self.assertEqual(endpoint_url, 'test-endpoint')

@mock.patch('SoftLayer.CLI.environment.Environment.getpass')
@mock.patch('SoftLayer.CLI.environment.Environment.input')
def test_get_user_input_default(self, mocked_input, getpass):
Expand Down
10 changes: 3 additions & 7 deletions tests/CLI/modules/order_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,9 @@ def test_item_list(self):

self.assert_no_fail(result)
self.assert_called_with('SoftLayer_Product_Package', 'getItems')
expected_results = [{'category': 'testing',
'keyName': 'item1',
'description': 'description1'},
{'category': 'testing',
'keyName': 'item2',
'description': 'description2'}]
self.assertEqual(expected_results, json.loads(result.output))
self.assertIn('description2', result.output)
self.assertIn('testing', result.output)
self.assertIn('item2', result.output)

def test_package_list(self):
p_mock = self.set_mock('SoftLayer_Product_Package', 'getAllObjects')
Expand Down