Skip to content

Commit 2987fd9

Browse files
committed
cancel-guests returns a dictionary about delete status
1 parent 4a82445 commit 2987fd9

File tree

4 files changed

+67
-12
lines changed

4 files changed

+67
-12
lines changed

SoftLayer/CLI/dedicatedhost/cancel_guests.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,18 @@ def cli(env, identifier):
2626
if not (env.skip_confirmations or formatting.no_going_back(host_id)):
2727
raise exceptions.CLIAbort('Aborted')
2828

29+
table = formatting.Table(['id', 'server name', 'status'])
30+
2931
result = dh_mgr.cancel_guests(host_id)
3032

31-
if result is True:
32-
click.secho('All guests into the dedicated host %s were cancelled' % host_id, fg='green')
33+
if result:
34+
for status in result:
35+
table.add_row([
36+
status['id'],
37+
status['fqdn'],
38+
status['status']
39+
])
40+
41+
env.fout(table)
3342
else:
3443
click.secho('There is not any guest into the dedicated host %s' % host_id, fg='red')

SoftLayer/managers/dedicated_host.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,26 @@ def cancel_guests(self, host_id):
5757
To cancel an specified guest use the method VSManager.cancel_instance()
5858
5959
:param host_id: The ID of the dedicated host.
60-
:return: True on success, False if there isn't any guest or
61-
an exception from the API
60+
:return: The id, fqdn and status of all guests into a dictionary. The status
61+
could be 'Cancelled' or an exception message, The dictionary is empty
62+
if there isn't any guest in the dedicated host.
6263
6364
Example::
6465
# Cancel guests of dedicated host id 12345
6566
result = mgr.cancel_guests(12345)
6667
"""
67-
result = False
68+
result = []
6869

69-
guests = self.host.getGuests(id=host_id, mask='id')
70+
guests = self.host.getGuests(id=host_id, mask='id,fullyQualifiedDomainName')
7071

7172
if guests:
7273
for vs in guests:
73-
result = self.guest.deleteObject(id=vs['id'])
74+
status_info = {
75+
'id': vs['id'],
76+
'fqdn': vs['fullyQualifiedDomainName'],
77+
'status': self._delete_guest(vs['id'])
78+
}
79+
result.append(status_info)
7480

7581
return result
7682

@@ -512,3 +518,13 @@ def get_router_options(self, datacenter=None, flavor=None):
512518
item = self._get_item(package, flavor)
513519

514520
return self._get_backend_router(location['location']['locationPackageDetails'], item)
521+
522+
def _delete_guest(self, guest_id):
523+
"""Deletes a guest and returns 'Cancelled' or and Exception message"""
524+
msg = 'Cancelled'
525+
try:
526+
self.guest.deleteObject(id=guest_id)
527+
except SoftLayer.SoftLayerAPIError as e:
528+
msg = 'Exception: ' + e.faultString
529+
530+
return msg

tests/CLI/modules/dedicatedhost_tests.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,19 @@ def test_cancel_host_abort(self):
353353
self.assertIsInstance(result.exception, exceptions.CLIAbort)
354354

355355
def test_cancel_guests(self):
356+
vs1 = {'id': 987, 'fullyQualifiedDomainName': 'foobar.example.com'}
357+
vs2 = {'id': 654, 'fullyQualifiedDomainName': 'wombat.example.com'}
356358
guests = self.set_mock('SoftLayer_Virtual_DedicatedHost', 'getGuests')
357-
guests.return_value = [{'id': 987}, {'id': 654}]
359+
guests.return_value = [vs1, vs2]
360+
361+
vs_status1 = {'id': 987, 'server name': 'foobar.example.com', 'status': 'Cancelled'}
362+
vs_status2 = {'id': 654, 'server name': 'wombat.example.com', 'status': 'Cancelled'}
363+
expected_result = [vs_status1, vs_status2]
358364

359365
result = self.run_command(['--really', 'dedicatedhost', 'cancel-guests', '12345'])
360366
self.assert_no_fail(result)
361367

362-
self.assertEqual(str(result.output), 'All guests into the dedicated host 12345 were cancelled\n')
368+
self.assertEqual(expected_result, json.loads(result.output))
363369

364370
def test_cancel_guests_empty_list(self):
365371
guests = self.set_mock('SoftLayer_Virtual_DedicatedHost', 'getGuests')
@@ -393,3 +399,8 @@ def test_list_guests(self):
393399
'id': 202,
394400
'power_state': 'Running',
395401
'backend_ip': '10.45.19.35'}])
402+
403+
def _get_cancel_guests_return(self):
404+
vs_status1 = {'id': 123, 'fqdn': 'foobar.example.com', 'status': 'Cancelled'}
405+
vs_status2 = {'id': 456, 'fqdn': 'wombat.example.com', 'status': 'Cancelled'}
406+
return [vs_status1, vs_status2]

tests/managers/dedicated_host_tests.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,20 +547,39 @@ def test_cancel_host(self):
547547
self.assert_called_with('SoftLayer_Virtual_DedicatedHost', 'deleteObject', identifier=789)
548548

549549
def test_cancel_guests(self):
550+
vs1 = {'id': 987, 'fullyQualifiedDomainName': 'foobar.example.com'}
551+
vs2 = {'id': 654, 'fullyQualifiedDomainName': 'wombat.example.com'}
550552
self.dedicated_host.host = mock.Mock()
551-
self.dedicated_host.host.getGuests.return_value = [{'id': 987}, {'id': 654}]
553+
self.dedicated_host.host.getGuests.return_value = [vs1, vs2]
554+
555+
# Expected result
556+
vs_status1 = {'id': 987, 'fqdn': 'foobar.example.com', 'status': 'Cancelled'}
557+
vs_status2 = {'id': 654, 'fqdn': 'wombat.example.com', 'status': 'Cancelled'}
558+
delete_status = [vs_status1, vs_status2]
552559

553560
result = self.dedicated_host.cancel_guests(789)
554561

555-
self.assertEqual(result, True)
562+
self.assertEqual(result, delete_status)
556563

557564
def test_cancel_guests_empty_list(self):
558565
self.dedicated_host.host = mock.Mock()
559566
self.dedicated_host.host.getGuests.return_value = []
560567

561568
result = self.dedicated_host.cancel_guests(789)
562569

563-
self.assertEqual(result, False)
570+
self.assertEqual(result, [])
571+
572+
def test_delete_guest(self):
573+
result = self.dedicated_host._delete_guest(123)
574+
self.assertEqual(result, 'Cancelled')
575+
576+
# delete_guest should return the exception message in case it fails
577+
error_raised = SoftLayer.SoftLayerAPIError('SL Exception', 'SL message')
578+
self.dedicated_host.guest = mock.Mock()
579+
self.dedicated_host.guest.deleteObject.side_effect = error_raised
580+
581+
result = self.dedicated_host._delete_guest(369)
582+
self.assertEqual(result, 'Exception: SL message')
564583

565584
def _get_routers_sample(self):
566585
routers = [

0 commit comments

Comments
 (0)