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
6 changes: 5 additions & 1 deletion SoftLayer/CLI/hardware/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting
from SoftLayer.CLI import helpers
from SoftLayer import exceptions


@click.command()
Expand All @@ -22,6 +23,9 @@ def cli(env, identifier):
instance = manager.get_hardware(hardware_id)

table = formatting.Table(['username', 'password'])
if 'passwords' not in instance['operatingSystem']:
raise exceptions.SoftLayerError("No passwords found in operatingSystem")

for item in instance['operatingSystem']['passwords']:
table.add_row([item['username'], item['password']])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of raising an exception, I would prefer this to just print out "None"

table.add_row([item.get('username', 'None'), item.get('password', 'None')])

.get() allows you to specify a default value to return if the item isn't found. This article explain it in a bit more detail if you are interested.
https://docs.quantifiedcode.com/python-anti-patterns/correctness/not_using_get_to_return_a_default_value_from_a_dictionary.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

table.add_row([item.get('username', 'None'), item.get('password', 'None')])
env.fout(table)
56 changes: 54 additions & 2 deletions tests/CLI/modules/server_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,58 @@ def test_server_cancel_reasons(self):
self.assert_no_fail(result)
self.assertEqual(len(output), 10)

def test_server_credentials(self):
result = self.run_command(['hardware', 'credentials', '12345'])

self.assert_no_fail(result)
self.assertEqual(json.loads(result.output),
[{
'username': 'root',
'password': 'abc123'
}])

def test_server_credentials_exception_passwords_not_found(self):
mock = self.set_mock('SoftLayer_Hardware_Server', 'getObject')
mock.return_value = {
"accountId": 11111,
"domain": "chechu.com",
"fullyQualifiedDomainName": "host3.vmware.chechu.com",
"hardwareStatusId": 5,
"hostname": "host3.vmware",
"id": 12345,
"operatingSystem": {}
}

result = self.run_command(['hardware', 'credentials', '12345'])

self.assertEqual(
'No passwords found in operatingSystem',
str(result.exception)
)

def test_server_credentials_exception_password_not_found(self):
mock = self.set_mock('SoftLayer_Hardware_Server', 'getObject')
mock.return_value = {
"accountId": 11111,
"domain": "chechu.com",
"fullyQualifiedDomainName": "host3.vmware.chechu.com",
"hardwareStatusId": 5,
"hostname": "host3.vmware",
"id": 12345,
"operatingSystem": {
"hardwareId": 22222,
"id": 333333,
"passwords": [{}]
}
}

result = self.run_command(['hardware', 'credentials', '12345'])

self.assertEqual(
'None',
str(result.exception)
)

def test_server_details(self):
result = self.run_command(['server', 'detail', '1234',
'--passwords', '--price'])
Expand Down Expand Up @@ -313,7 +365,7 @@ def test_create_server_missing_required(self):

@mock.patch('SoftLayer.CLI.template.export_to_template')
def test_create_server_with_export(self, export_mock):
if(sys.platform.startswith("win")):
if (sys.platform.startswith("win")):
self.skipTest("Test doesn't work in Windows")
result = self.run_command(['--really', 'server', 'create',
'--size=S1270_8GB_2X1TBSATA_NORAID',
Expand Down Expand Up @@ -387,7 +439,7 @@ def test_edit_server_failed(self, edit_mock):
hostname='hardware-test1')

def test_edit_server_userfile(self):
if(sys.platform.startswith("win")):
if (sys.platform.startswith("win")):
self.skipTest("Test doesn't work in Windows")
with tempfile.NamedTemporaryFile() as userfile:
userfile.write(b"some data")
Expand Down