Skip to content

Commit de7c255

Browse files
author
Flavio Fernandes
committed
scli vlan detail: gracefully handle hardware that has no name/domain
Avoid exceptions in cases when IMS returns hardware or VSI instances that have no 'hostname' and 'domain' attributes. An example of such: $ slcli vlan detail 1499927 An unexpected error has occured: Traceback (most recent call last): File "/home/vagrant/.venv/local/lib/python2.7/site-packages/SoftLayer/CLI/core.py", line 176, in main cli.main(**kwargs) File "/home/vagrant/.venv/local/lib/python2.7/site-packages/click/core.py", line 697, in main rv = self.invoke(ctx) File "/home/vagrant/.venv/local/lib/python2.7/site-packages/click/core.py", line 1066, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/home/vagrant/.venv/local/lib/python2.7/site-packages/click/core.py", line 1066, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/home/vagrant/.venv/local/lib/python2.7/site-packages/click/core.py", line 895, in invoke return ctx.invoke(self.callback, **ctx.params) File "/home/vagrant/.venv/local/lib/python2.7/site-packages/click/core.py", line 535, in invoke return callback(*args, **kwargs) File "/home/vagrant/.venv/local/lib/python2.7/site-packages/click/decorators.py", line 64, in new_func return ctx.invoke(f, obj, *args[1:], **kwargs) File "/home/vagrant/.venv/local/lib/python2.7/site-packages/click/core.py", line 535, in invoke return callback(*args, **kwargs) File "/home/vagrant/.venv/local/lib/python2.7/site-packages/SoftLayer/CLI/vlan/detail.py", line 76, in cli hardware['domain'], KeyError: 'domain' Feel free to report this error as it is likely a bug: https://github.com/softlayer/softlayer-python/issues The following snippet should be able to reproduce the error
1 parent 6332ffe commit de7c255

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

SoftLayer/CLI/vlan/detail.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def cli(env, identifier, no_vs, no_hardware):
6060
if vlan.get('virtualGuests'):
6161
vs_table = formatting.KeyValueTable(server_columns)
6262
for vsi in vlan['virtualGuests']:
63-
vs_table.add_row([vsi['hostname'],
64-
vsi['domain'],
63+
vs_table.add_row([vsi.get('hostname'),
64+
vsi.get('domain'),
6565
vsi.get('primaryIpAddress'),
6666
vsi.get('primaryBackendIpAddress')])
6767
table.add_row(['vs', vs_table])
@@ -72,8 +72,8 @@ def cli(env, identifier, no_vs, no_hardware):
7272
if vlan.get('hardware'):
7373
hw_table = formatting.Table(server_columns)
7474
for hardware in vlan['hardware']:
75-
hw_table.add_row([hardware['hostname'],
76-
hardware['domain'],
75+
hw_table.add_row([hardware.get('hostname'),
76+
hardware.get('domain'),
7777
hardware.get('primaryIpAddress'),
7878
hardware.get('primaryBackendIpAddress')])
7979
table.add_row(['hardware', hw_table])

tests/CLI/modules/vlan_tests.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,32 @@ def test_subnet_list(self):
4747
vlan_mock.return_value = getObject
4848
result = self.run_command(['vlan', 'detail', '1234'])
4949
self.assert_no_fail(result)
50+
51+
def test_detail_hardware_without_hostname(self):
52+
vlan_mock = self.set_mock('SoftLayer_Network_Vlan', 'getObject')
53+
getObject = {
54+
'primaryRouter': {
55+
'datacenter': {'id': 1234, 'longName': 'TestDC'},
56+
'fullyQualifiedDomainName': 'fcr01.TestDC'
57+
},
58+
'id': 1234,
59+
'vlanNumber': 4444,
60+
'firewallInterfaces': None,
61+
'subnets': [],
62+
'hardware': [
63+
{'a_hardware': 'that_has_none_of_the_expected_attributes_provided'},
64+
{'domain': 'example.com',
65+
'networkManagementIpAddress': '10.171.202.131',
66+
'hardwareStatus': {'status': 'ACTIVE', 'id': 5},
67+
'notes': '',
68+
'hostname': 'hw1', 'hardwareStatusId': 5,
69+
'globalIdentifier': 'f6ea716a-41d8-4c52-bb2e-48d63105f4b0',
70+
'primaryIpAddress': '169.60.169.169',
71+
'primaryBackendIpAddress': '10.171.202.130', 'id': 826425,
72+
'privateIpAddress': '10.171.202.130',
73+
'fullyQualifiedDomainName': 'hw1.example.com'}
74+
]
75+
}
76+
vlan_mock.return_value = getObject
77+
result = self.run_command(['vlan', 'detail', '1234'])
78+
self.assert_no_fail(result)

0 commit comments

Comments
 (0)