Skip to content

Commit 2ef45d8

Browse files
committed
cancel-all-guests command was refactored and unittests were added on managers and cli
1 parent 3052686 commit 2ef45d8

File tree

6 files changed

+82
-38
lines changed

6 files changed

+82
-38
lines changed

SoftLayer/CLI/dedicatedhost/cancel_guests.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,21 @@
1414
@click.argument('identifier')
1515
@environment.pass_env
1616
def cli(env, identifier):
17-
"""Cancel all virtual guests of the dedicated host immediately"""
17+
"""Cancel all virtual guests of the dedicated host immediately.
18+
19+
Use the 'slcli vs cancel' command to cancel an specific guest
20+
"""
1821

1922
dh_mgr = SoftLayer.DedicatedHostManager(env.client)
20-
vs_mgr = SoftLayer.VSManager(env.client)
2123

2224
host_id = helpers.resolve_id(dh_mgr.resolve_ids, identifier, 'dedicated host')
2325

24-
guests = dh_mgr.list_guests(host_id)
25-
26-
if guests:
27-
msg = '%s guest(s) will be cancelled, ' \
28-
'do you want to continue?' % len(guests)
26+
if not (env.skip_confirmations or formatting.no_going_back(host_id)):
27+
raise exceptions.CLIAbort('Aborted')
2928

30-
if not (env.skip_confirmations or formatting.confirm(msg)):
31-
raise exceptions.CLIAbort('Aborted')
32-
33-
for guest in guests:
34-
vs_mgr.cancel_instance(guest['id'])
29+
result = dh_mgr.cancel_guests(host_id)
3530

31+
if result is True:
3632
click.secho('All guests into the dedicated host %s were cancelled' % host_id, fg='green')
37-
3833
else:
3934
click.secho('There is not any guest into the dedicated host %s' % host_id, fg='red')

SoftLayer/CLI/dedicatedhost/list_guests.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""List dedicated servers."""
1+
"""List guests which are in a dedicated host server."""
22
# :license: MIT, see LICENSE for more details.
33

44
import click
@@ -55,12 +55,13 @@
5555
show_default=True)
5656
@environment.pass_env
5757
def cli(env, identifier, sortby, cpu, domain, hostname, memory, tag, columns):
58-
"""List guests into the dedicated host."""
58+
"""List guests which are in a dedicated host server."""
59+
5960
mgr = SoftLayer.DedicatedHostManager(env.client)
6061
guests = mgr.list_guests(host_id=identifier,
6162
cpus=cpu,
6263
hostname=hostname,
63-
domain= domain,
64+
domain=domain,
6465
memory=memory,
6566
tags=tag,
6667
mask=columns.mask())

SoftLayer/fixtures/SoftLayer_Virtual_DedicatedHost.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@
8080
deleteObject = True
8181

8282
getGuests = [{
83-
'id': 100,
84-
'metricTrackingObjectId': 1,
83+
'id': 200,
8584
'hostname': 'vs-test1',
8685
'domain': 'test.sftlyr.ws',
8786
'fullyQualifiedDomainName': 'vs-test1.test.sftlyr.ws',
@@ -95,7 +94,6 @@
9594
'globalIdentifier': '1a2b3c-1701',
9695
'primaryBackendIpAddress': '10.45.19.37',
9796
'hourlyBillingFlag': False,
98-
9997
'billingItem': {
10098
'id': 6327,
10199
'recurringFee': 1.54,
@@ -108,8 +106,7 @@
108106
}
109107
},
110108
}, {
111-
'id': 104,
112-
'metricTrackingObjectId': 2,
109+
'id': 202,
113110
'hostname': 'vs-test2',
114111
'domain': 'test.sftlyr.ws',
115112
'fullyQualifiedDomainName': 'vs-test2.test.sftlyr.ws',
@@ -133,6 +130,5 @@
133130
}
134131
}
135132
}
136-
},
137-
'virtualRack': {'id': 1, 'bandwidthAllotmentTypeId': 2},
133+
}
138134
}]

SoftLayer/managers/dedicated_host.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(self, client, ordering_manager=None):
3333
self.client = client
3434
self.account = client['Account']
3535
self.host = client['Virtual_DedicatedHost']
36+
self.guest = client['Virtual_Guest']
3637

3738
if ordering_manager is None:
3839
self.ordering_manager = ordering.OrderingManager(client)
@@ -50,26 +51,47 @@ def cancel_host(self, host_id):
5051
"""
5152
return self.host.deleteObject(id=host_id)
5253

54+
def cancel_guests(self, host_id):
55+
"""Cancel all guests into the dedicated host immediately.
56+
57+
To cancel an specified guest use the method VSManager.cancel_instance()
58+
59+
: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
62+
63+
Example::
64+
# Cancel guests of dedicated host id 12345
65+
result = mgr.cancel_guests(12345)
66+
"""
67+
result = False
68+
69+
guest_list = self.host.getGuests(id=host_id, mask="id")
70+
71+
if guest_list:
72+
for virtual_guest in guest_list:
73+
result = self.guest.deleteObject(virtual_guest['id'])
74+
75+
return result
76+
5377
def list_guests(self, host_id, tags=None, cpus=None, memory=None, hostname=None,
5478
domain=None, local_disk=None, nic_speed=None, public_ip=None,
5579
private_ip=None, **kwargs):
5680
"""Retrieve a list of all virtual servers on the dedicated host.
5781
5882
Example::
5983
60-
# Print out a list of hourly instances in the host id 12345.
84+
# Print out a list of instances with 4 cpu cores in the host id 12345.
6185
62-
for vsi in mgr.list_guests(host_id=12345, hourly=True):
86+
for vsi in mgr.list_guests(host_id=12345, cpus=4):
6387
print vsi['fullyQualifiedDomainName'], vsi['primaryIpAddress']
6488
6589
# Using a custom object-mask. Will get ONLY what is specified
6690
object_mask = "mask[hostname,monitoringRobot[robotStatus]]"
67-
for vsi in mgr.list_guests(mask=object_mask,hourly=True):
91+
for vsi in mgr.list_guests(mask=object_mask,cpus=4):
6892
print vsi
6993
7094
:param integer host_id: the identifier of dedicated host
71-
:param boolean hourly: include hourly instances
72-
:param boolean monthly: include monthly instances
7395
:param list tags: filter based on list of tags
7496
:param integer cpus: filter based on number of CPUS
7597
:param integer memory: filter based on amount of memory

tests/CLI/modules/dedicatedhost_tests.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -341,25 +341,39 @@ def test_create_verify_no_price_or_more_than_one(self):
341341
@mock.patch('SoftLayer.DedicatedHostManager.cancel_host')
342342
def test_cancel_host(self, cancel_mock):
343343
result = self.run_command(['--really', 'dedicatedhost', 'cancel', '12345'])
344+
344345
self.assert_no_fail(result)
345346
cancel_mock.assert_called_with(12345)
347+
346348
self.assertEqual(str(result.output), 'Dedicated Host 12345 was cancelled\n')
347349

348350
def test_cancel_host_abort(self):
349351
result = self.run_command(['dedicatedhost', 'cancel', '12345'])
350352
self.assertEqual(result.exit_code, 2)
351353
self.assertIsInstance(result.exception, exceptions.CLIAbort)
352354

353-
@mock.patch('SoftLayer.DedicatedHostManager.cancel_host')
354-
def test_cancel_all_guest(self, cancel_mock):
355-
result = self.run_command(['--really', 'dedicatedhost', 'cancel', '12345'])
355+
def test_cancel_all_guests(self):
356+
guests = self.set_mock('SoftLayer_Virtual_DedicatedHost', 'getGuests')
357+
guests.return_value = [{'id': 987}, {'id': 654}]
358+
359+
result = self.run_command(['--really', 'dedicatedhost', 'cancel-all-guests', '12345'])
356360
self.assert_no_fail(result)
357-
cancel_mock.assert_called_with(12345)
358-
self.assertEqual(str(result.output), 'Dedicated Host 12345 was cancelled\n')
359361

360-
def test_cancel_all_guest_empty_list(self):
362+
self.assertEqual(str(result.output), 'All guests into the dedicated host 12345 were cancelled\n')
363+
364+
def test_cancel_all_guests_empty_list(self):
365+
guests = self.set_mock('SoftLayer_Virtual_DedicatedHost', 'getGuests')
366+
guests.return_value = []
367+
368+
result = self.run_command(['--really', 'dedicatedhost', 'cancel-all-guests', '12345'])
369+
self.assert_no_fail(result)
370+
371+
self.assertEqual(str(result.output), 'There is not any guest into the dedicated host 12345\n')
372+
373+
def test_cancel_all_guests_abort(self):
361374
result = self.run_command(['dedicatedhost', 'cancel', '12345'])
362375
self.assertEqual(result.exit_code, 2)
376+
363377
self.assertIsInstance(result.exception, exceptions.CLIAbort)
364378

365379
def test_list_guests(self):
@@ -370,12 +384,12 @@ def test_list_guests(self):
370384
[{'hostname': 'vs-test1',
371385
'domain': 'test.sftlyr.ws',
372386
'primary_ip': '172.16.240.2',
373-
'id': 100,
387+
'id': 200,
374388
'power_state': 'Running',
375389
'backend_ip': '10.45.19.37'},
376390
{'hostname': 'vs-test2',
377391
'domain': 'test.sftlyr.ws',
378392
'primary_ip': '172.16.240.7',
379-
'id': 104,
393+
'id': 202,
380394
'power_state': 'Running',
381395
'backend_ip': '10.45.19.35'}])

tests/managers/dedicated_host_tests.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,22 @@ def test_cancel_host(self):
546546
self.assertEqual(result, True)
547547
self.assert_called_with('SoftLayer_Virtual_DedicatedHost', 'deleteObject', identifier=789)
548548

549+
def test_cancel_guests(self):
550+
self.dedicated_host.host = mock.Mock()
551+
self.dedicated_host.host.getGuests.return_value = [{'id': 987}, {'id': 654}]
552+
553+
result = self.dedicated_host.cancel_guests(789)
554+
555+
self.assertEqual(result, True)
556+
557+
def test_cancel_guests_empty_list(self):
558+
self.dedicated_host.host = mock.Mock()
559+
self.dedicated_host.host.getGuests.return_value = []
560+
561+
result = self.dedicated_host.cancel_guests(789)
562+
563+
self.assertEqual(result, False)
564+
549565
def _get_routers_sample(self):
550566
routers = [
551567
{
@@ -658,7 +674,7 @@ def test_list_guests(self):
658674
results = self.dedicated_host.list_guests(12345)
659675

660676
for result in results:
661-
self.assertIn(result['id'], [100, 104])
677+
self.assertIn(result['id'], [200, 202])
662678
self.assert_called_with('SoftLayer_Virtual_DedicatedHost', 'getGuests', identifier=12345)
663679

664680
def test_list_guests_with_filters(self):
@@ -683,4 +699,4 @@ def test_list_guests_with_filters(self):
683699
}
684700
}
685701
self.assert_called_with('SoftLayer_Virtual_DedicatedHost', 'getGuests',
686-
identifier=12345, filter=_filter)
702+
identifier=12345, filter=_filter)

0 commit comments

Comments
 (0)