From ad94609c78c1fc6e43f79790b4b0758a05233a79 Mon Sep 17 00:00:00 2001 From: Flavio Fernandes Date: Sun, 22 Oct 2017 08:06:05 -0400 Subject: [PATCH 1/3] virtual detail: show dedicated host info, if applicable This is only applicable for VSIs that were created to be in a dedicated host. Provide id and name of the dedicated host where VSI is running. --- SoftLayer/CLI/virt/detail.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/SoftLayer/CLI/virt/detail.py b/SoftLayer/CLI/virt/detail.py index baa2b8553..26c2a2a2d 100644 --- a/SoftLayer/CLI/virt/detail.py +++ b/SoftLayer/CLI/virt/detail.py @@ -45,6 +45,7 @@ def cli(env, identifier, passwords=False, price=False): table.add_row(['active_transaction', formatting.active_txn(result)]) table.add_row(['datacenter', result['datacenter']['name'] or formatting.blank()]) + _cli_helper_dedicated_host(env, result, table) operating_system = utils.lookup(result, 'operatingSystem', 'softwareLicense', @@ -138,3 +139,19 @@ def cli(env, identifier, passwords=False, price=False): pass env.fout(table) + + +def _cli_helper_dedicated_host(env, result, table): + """Get details on dedicated host for a virtual server.""" + + dedicated_host_id = utils.lookup(result, 'dedicatedHost', 'id') + if dedicated_host_id: + table.add_row(['dedicated_host_id', dedicated_host_id]) + # Try to find name of dedicated host + try: + dedicated_host = env.client.call('Virtual_DedicatedHost', 'getObject', + id=dedicated_host_id) + except SoftLayer.SoftLayerAPIError: + dedicated_host = {} + table.add_row(['dedicated_host', + dedicated_host.get('name') or formatting.blank()]) From 7a3af08ce9389443a41ce93c736a4b5cd5061d37 Mon Sep 17 00:00:00 2001 From: Flavio Fernandes Date: Sun, 22 Oct 2017 21:18:14 -0400 Subject: [PATCH 2/3] virtual detail: improve coverage Add test for verifying host_id attribute --- .../SoftLayer_Virtual_DedicatedHost.py | 10 +++++++++ SoftLayer/fixtures/SoftLayer_Virtual_Guest.py | 1 + tests/CLI/modules/vs_tests.py | 21 +++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 SoftLayer/fixtures/SoftLayer_Virtual_DedicatedHost.py diff --git a/SoftLayer/fixtures/SoftLayer_Virtual_DedicatedHost.py b/SoftLayer/fixtures/SoftLayer_Virtual_DedicatedHost.py new file mode 100644 index 000000000..926d84ed9 --- /dev/null +++ b/SoftLayer/fixtures/SoftLayer_Virtual_DedicatedHost.py @@ -0,0 +1,10 @@ +getObject = { + 'id': 37401, + 'memoryCapacity': 242, + 'modifyDate': '', + 'name': 'test-dedicated', + 'diskCapacity': 1200, + 'createDate': '2017-10-16T12:50:23-05:00', + 'cpuCount': 56, + 'accountId': 1199911 +} diff --git a/SoftLayer/fixtures/SoftLayer_Virtual_Guest.py b/SoftLayer/fixtures/SoftLayer_Virtual_Guest.py index 2a8cd8c9e..83ddcb15e 100644 --- a/SoftLayer/fixtures/SoftLayer_Virtual_Guest.py +++ b/SoftLayer/fixtures/SoftLayer_Virtual_Guest.py @@ -44,6 +44,7 @@ 'networkVlans': [{'networkSpace': 'PUBLIC', 'vlanNumber': 23, 'id': 1}], + 'dedicatedHost': {'id': 37401}, 'operatingSystem': { 'passwords': [{'username': 'user', 'password': 'pass'}], 'softwareLicense': { diff --git a/tests/CLI/modules/vs_tests.py b/tests/CLI/modules/vs_tests.py index cc98ee3f3..dced520e0 100644 --- a/tests/CLI/modules/vs_tests.py +++ b/tests/CLI/modules/vs_tests.py @@ -9,6 +9,7 @@ import mock from SoftLayer.CLI import exceptions +from SoftLayer import SoftLayerAPIError from SoftLayer import testing @@ -42,6 +43,8 @@ def test_detail_vs(self): 'cores': 2, 'created': '2013-08-01 15:23:45', 'datacenter': 'TEST00', + 'dedicated_host': 'test-dedicated', + 'dedicated_host_id': 37401, 'hostname': 'vs-test1', 'domain': 'test.sftlyr.ws', 'fqdn': 'vs-test1.test.sftlyr.ws', @@ -88,6 +91,24 @@ def test_detail_vs_empty_tag(self): ['example-tag'], ) + def test_detail_vs_dedicated_host_not_found(self): + ex = SoftLayerAPIError('SoftLayer_Exception', 'Not found') + mock = self.set_mock('SoftLayer_Virtual_DedicatedHost', 'getObject') + mock.side_effect = ex + result = self.run_command(['vs', 'detail', '100']) + self.assert_no_fail(result) + self.assertEqual(json.loads(result.output)['dedicated_host_id'], 37401) + self.assertIsNone(json.loads(result.output)['dedicated_host']) + + def test_detail_vs_no_dedicated_host_hostname(self): + mock = self.set_mock('SoftLayer_Virtual_DedicatedHost', 'getObject') + mock.return_value = {'this_is_a_fudged_Virtual_DedicatedHost': True, + 'name_is_not_provided': ''} + result = self.run_command(['vs', 'detail', '100']) + self.assert_no_fail(result) + self.assertEqual(json.loads(result.output)['dedicated_host_id'], 37401) + self.assertIsNone(json.loads(result.output)['dedicated_host']) + def test_create_options(self): result = self.run_command(['vs', 'create-options']) From 11b478f5c993ac977018b80fe037e4e4e17fd559 Mon Sep 17 00:00:00 2001 From: Flavio Fernandes Date: Tue, 24 Oct 2017 14:49:12 -0400 Subject: [PATCH 3/3] virtual detail: add logging From code review request. Thanks Christopher! --- SoftLayer/CLI/virt/detail.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/SoftLayer/CLI/virt/detail.py b/SoftLayer/CLI/virt/detail.py index 26c2a2a2d..7a418dadb 100644 --- a/SoftLayer/CLI/virt/detail.py +++ b/SoftLayer/CLI/virt/detail.py @@ -1,6 +1,8 @@ """Get details for a virtual server.""" # :license: MIT, see LICENSE for more details. +import logging + import click import SoftLayer @@ -9,6 +11,8 @@ from SoftLayer.CLI import helpers from SoftLayer import utils +LOGGER = logging.getLogger(__name__) + @click.command() @click.argument('identifier') @@ -152,6 +156,7 @@ def _cli_helper_dedicated_host(env, result, table): dedicated_host = env.client.call('Virtual_DedicatedHost', 'getObject', id=dedicated_host_id) except SoftLayer.SoftLayerAPIError: + LOGGER.error('Unable to get dedicated host id %s', dedicated_host_id) dedicated_host = {} table.add_row(['dedicated_host', dedicated_host.get('name') or formatting.blank()])