diff --git a/CHANGELOG.md b/CHANGELOG.md index 17b9b49df..779b035de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,16 @@ # Change Log + +## [5.6.4] - 2018-11-16 + +- Changes: https://github.com/softlayer/softlayer-python/compare/v5.6.3...v5.6.4 + ++ #1041 Dedicated host cancel, cancel-guests, list-guests ++ #1071 added createDate and modifyDate parameters to sg rule-list ++ #1060 Fixed slcli subnet list ++ #1056 Fixed documentation link in image manager ++ #1062 Added description to slcli order + ## [5.6.3] - 2018-11-07 - Changes: https://github.com/softlayer/softlayer-python/compare/v5.6.0...v5.6.3 diff --git a/SoftLayer/CLI/dedicatedhost/cancel.py b/SoftLayer/CLI/dedicatedhost/cancel.py new file mode 100644 index 000000000..58ed85d30 --- /dev/null +++ b/SoftLayer/CLI/dedicatedhost/cancel.py @@ -0,0 +1,28 @@ +"""Cancel a dedicated host.""" +# :license: MIT, see LICENSE for more details. + +import click + +import SoftLayer +from SoftLayer.CLI import environment +from SoftLayer.CLI import exceptions +from SoftLayer.CLI import formatting +from SoftLayer.CLI import helpers + + +@click.command() +@click.argument('identifier') +@environment.pass_env +def cli(env, identifier): + """Cancel a dedicated host server immediately""" + + mgr = SoftLayer.DedicatedHostManager(env.client) + + host_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'dedicated host') + + if not (env.skip_confirmations or formatting.no_going_back(host_id)): + raise exceptions.CLIAbort('Aborted') + + mgr.cancel_host(host_id) + + click.secho('Dedicated Host %s was cancelled' % host_id, fg='green') diff --git a/SoftLayer/CLI/dedicatedhost/cancel_guests.py b/SoftLayer/CLI/dedicatedhost/cancel_guests.py new file mode 100644 index 000000000..537828de1 --- /dev/null +++ b/SoftLayer/CLI/dedicatedhost/cancel_guests.py @@ -0,0 +1,43 @@ +"""Cancel a dedicated host.""" +# :license: MIT, see LICENSE for more details. + +import click + +import SoftLayer +from SoftLayer.CLI import environment +from SoftLayer.CLI import exceptions +from SoftLayer.CLI import formatting +from SoftLayer.CLI import helpers + + +@click.command() +@click.argument('identifier') +@environment.pass_env +def cli(env, identifier): + """Cancel all virtual guests of the dedicated host immediately. + + Use the 'slcli vs cancel' command to cancel an specific guest + """ + + dh_mgr = SoftLayer.DedicatedHostManager(env.client) + + host_id = helpers.resolve_id(dh_mgr.resolve_ids, identifier, 'dedicated host') + + if not (env.skip_confirmations or formatting.no_going_back(host_id)): + raise exceptions.CLIAbort('Aborted') + + table = formatting.Table(['id', 'server name', 'status']) + + result = dh_mgr.cancel_guests(host_id) + + if result: + for status in result: + table.add_row([ + status['id'], + status['fqdn'], + status['status'] + ]) + + env.fout(table) + else: + click.secho('There is not any guest into the dedicated host %s' % host_id, fg='red') diff --git a/SoftLayer/CLI/dedicatedhost/list_guests.py b/SoftLayer/CLI/dedicatedhost/list_guests.py new file mode 100644 index 000000000..bec37a89f --- /dev/null +++ b/SoftLayer/CLI/dedicatedhost/list_guests.py @@ -0,0 +1,76 @@ +"""List guests which are in a dedicated host server.""" +# :license: MIT, see LICENSE for more details. + +import click + +import SoftLayer +from SoftLayer.CLI import columns as column_helper +from SoftLayer.CLI import environment +from SoftLayer.CLI import formatting +from SoftLayer.CLI import helpers + +COLUMNS = [ + column_helper.Column('guid', ('globalIdentifier',)), + column_helper.Column('cpu', ('maxCpu',)), + column_helper.Column('memory', ('maxMemory',)), + column_helper.Column('datacenter', ('datacenter', 'name')), + column_helper.Column('primary_ip', ('primaryIpAddress',)), + column_helper.Column('backend_ip', ('primaryBackendIpAddress',)), + column_helper.Column( + 'created_by', + ('billingItem', 'orderItem', 'order', 'userRecord', 'username')), + column_helper.Column('power_state', ('powerState', 'name')), + column_helper.Column( + 'tags', + lambda server: formatting.tags(server.get('tagReferences')), + mask="tagReferences.tag.name"), +] + +DEFAULT_COLUMNS = [ + 'id', + 'hostname', + 'domain', + 'primary_ip', + 'backend_ip', + 'power_state' +] + + +@click.command() +@click.argument('identifier') +@click.option('--cpu', '-c', help='Number of CPU cores', type=click.INT) +@click.option('--domain', '-D', help='Domain portion of the FQDN') +@click.option('--hostname', '-H', help='Host portion of the FQDN') +@click.option('--memory', '-m', help='Memory in mebibytes', type=click.INT) +@helpers.multi_option('--tag', help='Filter by tags') +@click.option('--sortby', + help='Column to sort by', + default='hostname', + show_default=True) +@click.option('--columns', + callback=column_helper.get_formatter(COLUMNS), + help='Columns to display. [options: %s]' + % ', '.join(column.name for column in COLUMNS), + default=','.join(DEFAULT_COLUMNS), + show_default=True) +@environment.pass_env +def cli(env, identifier, sortby, cpu, domain, hostname, memory, tag, columns): + """List guests which are in a dedicated host server.""" + + mgr = SoftLayer.DedicatedHostManager(env.client) + guests = mgr.list_guests(host_id=identifier, + cpus=cpu, + hostname=hostname, + domain=domain, + memory=memory, + tags=tag, + mask=columns.mask()) + + table = formatting.Table(columns.columns) + table.sortby = sortby + + for guest in guests: + table.add_row([value or formatting.blank() + for value in columns.row(guest)]) + + env.fout(table) diff --git a/SoftLayer/CLI/order/__init__.py b/SoftLayer/CLI/order/__init__.py index e69de29bb..7dd721082 100644 --- a/SoftLayer/CLI/order/__init__.py +++ b/SoftLayer/CLI/order/__init__.py @@ -0,0 +1,2 @@ +"""View and order from the catalog.""" +# :license: MIT, see LICENSE for more details. diff --git a/SoftLayer/CLI/routes.py b/SoftLayer/CLI/routes.py index fa46f36ac..e80a0b50a 100644 --- a/SoftLayer/CLI/routes.py +++ b/SoftLayer/CLI/routes.py @@ -37,6 +37,9 @@ ('dedicatedhost:create', 'SoftLayer.CLI.dedicatedhost.create:cli'), ('dedicatedhost:create-options', 'SoftLayer.CLI.dedicatedhost.create_options:cli'), ('dedicatedhost:detail', 'SoftLayer.CLI.dedicatedhost.detail:cli'), + ('dedicatedhost:cancel', 'SoftLayer.CLI.dedicatedhost.cancel:cli'), + ('dedicatedhost:cancel-guests', 'SoftLayer.CLI.dedicatedhost.cancel_guests:cli'), + ('dedicatedhost:list-guests', 'SoftLayer.CLI.dedicatedhost.list_guests:cli'), ('cdn', 'SoftLayer.CLI.cdn'), ('cdn:detail', 'SoftLayer.CLI.cdn.detail:cli'), diff --git a/SoftLayer/CLI/subnet/list.py b/SoftLayer/CLI/subnet/list.py index e5d0d83a3..508d649a2 100644 --- a/SoftLayer/CLI/subnet/list.py +++ b/SoftLayer/CLI/subnet/list.py @@ -26,11 +26,10 @@ @click.option('--identifier', help="Filter by network identifier") @click.option('--subnet-type', '-t', help="Filter by subnet type") @click.option('--network-space', help="Filter by network space") -@click.option('--v4', '--ipv4', is_flag=True, help="Display only IPv4 subnets") -@click.option('--v6', '--ipv6', is_flag=True, help="Display only IPv6 subnets") +@click.option('--ipv4', '--v4', is_flag=True, help="Display only IPv4 subnets") +@click.option('--ipv6', '--v6', is_flag=True, help="Display only IPv6 subnets") @environment.pass_env -def cli(env, sortby, datacenter, identifier, subnet_type, network_space, - ipv4, ipv6): +def cli(env, sortby, datacenter, identifier, subnet_type, network_space, ipv4, ipv6): """List subnets.""" mgr = SoftLayer.NetworkManager(env.client) diff --git a/SoftLayer/consts.py b/SoftLayer/consts.py index ee5f29ab0..8a1368b26 100644 --- a/SoftLayer/consts.py +++ b/SoftLayer/consts.py @@ -5,7 +5,7 @@ :license: MIT, see LICENSE for more details. """ -VERSION = 'v5.6.3' +VERSION = 'v5.6.4' API_PUBLIC_ENDPOINT = 'https://api.softlayer.com/xmlrpc/v3.1/' API_PRIVATE_ENDPOINT = 'https://api.service.softlayer.com/xmlrpc/v3.1/' API_PUBLIC_ENDPOINT_REST = 'https://api.softlayer.com/rest/v3.1/' diff --git a/SoftLayer/fixtures/SoftLayer_Account.py b/SoftLayer/fixtures/SoftLayer_Account.py index 9b7b41da0..891df9ecb 100644 --- a/SoftLayer/fixtures/SoftLayer_Account.py +++ b/SoftLayer/fixtures/SoftLayer_Account.py @@ -316,9 +316,14 @@ { 'id': '100', 'networkIdentifier': '10.0.0.1', + 'cidr': '/24', + 'networkVlanId': 123, 'datacenter': {'name': 'dal00'}, 'version': 4, - 'subnetType': 'PRIMARY' + 'subnetType': 'PRIMARY', + 'ipAddressCount': 10, + 'virtualGuests': [], + 'hardware': [] }] getSshKeys = [{'id': '100', 'label': 'Test 1'}, diff --git a/SoftLayer/fixtures/SoftLayer_Virtual_DedicatedHost.py b/SoftLayer/fixtures/SoftLayer_Virtual_DedicatedHost.py index 94c8e5cc4..43ab0ec34 100644 --- a/SoftLayer/fixtures/SoftLayer_Virtual_DedicatedHost.py +++ b/SoftLayer/fixtures/SoftLayer_Virtual_DedicatedHost.py @@ -76,3 +76,59 @@ 'id': 12345, 'createDate': '2017-11-02T11:40:56-07:00' } + +deleteObject = True + +getGuests = [{ + 'id': 200, + 'hostname': 'vs-test1', + 'domain': 'test.sftlyr.ws', + 'fullyQualifiedDomainName': 'vs-test1.test.sftlyr.ws', + 'status': {'keyName': 'ACTIVE', 'name': 'Active'}, + 'datacenter': {'id': 50, 'name': 'TEST00', + 'description': 'Test Data Center'}, + 'powerState': {'keyName': 'RUNNING', 'name': 'Running'}, + 'maxCpu': 2, + 'maxMemory': 1024, + 'primaryIpAddress': '172.16.240.2', + 'globalIdentifier': '1a2b3c-1701', + 'primaryBackendIpAddress': '10.45.19.37', + 'hourlyBillingFlag': False, + 'billingItem': { + 'id': 6327, + 'recurringFee': 1.54, + 'orderItem': { + 'order': { + 'userRecord': { + 'username': 'chechu', + } + } + } + }, +}, { + 'id': 202, + 'hostname': 'vs-test2', + 'domain': 'test.sftlyr.ws', + 'fullyQualifiedDomainName': 'vs-test2.test.sftlyr.ws', + 'status': {'keyName': 'ACTIVE', 'name': 'Active'}, + 'datacenter': {'id': 50, 'name': 'TEST00', + 'description': 'Test Data Center'}, + 'powerState': {'keyName': 'RUNNING', 'name': 'Running'}, + 'maxCpu': 4, + 'maxMemory': 4096, + 'primaryIpAddress': '172.16.240.7', + 'globalIdentifier': '05a8ac-6abf0', + 'primaryBackendIpAddress': '10.45.19.35', + 'hourlyBillingFlag': True, + 'billingItem': { + 'id': 6327, + 'recurringFee': 1.54, + 'orderItem': { + 'order': { + 'userRecord': { + 'username': 'chechu', + } + } + } + } +}] diff --git a/SoftLayer/managers/dedicated_host.py b/SoftLayer/managers/dedicated_host.py index e041e8d74..86258416b 100644 --- a/SoftLayer/managers/dedicated_host.py +++ b/SoftLayer/managers/dedicated_host.py @@ -33,10 +33,144 @@ def __init__(self, client, ordering_manager=None): self.client = client self.account = client['Account'] self.host = client['Virtual_DedicatedHost'] + self.guest = client['Virtual_Guest'] if ordering_manager is None: self.ordering_manager = ordering.OrderingManager(client) + def cancel_host(self, host_id): + """Cancel a dedicated host immediately, it fails if there are still guests in the host. + + :param host_id: The ID of the dedicated host to be cancelled. + :return: True on success or an exception + + Example:: + # Cancels dedicated host id 12345 + result = mgr.cancel_host(12345) + + """ + return self.host.deleteObject(id=host_id) + + def cancel_guests(self, host_id): + """Cancel all guests into the dedicated host immediately. + + To cancel an specified guest use the method VSManager.cancel_instance() + + :param host_id: The ID of the dedicated host. + :return: The id, fqdn and status of all guests into a dictionary. The status + could be 'Cancelled' or an exception message, The dictionary is empty + if there isn't any guest in the dedicated host. + + Example:: + # Cancel guests of dedicated host id 12345 + result = mgr.cancel_guests(12345) + """ + result = [] + + guests = self.host.getGuests(id=host_id, mask='id,fullyQualifiedDomainName') + + if guests: + for vs in guests: + status_info = { + 'id': vs['id'], + 'fqdn': vs['fullyQualifiedDomainName'], + 'status': self._delete_guest(vs['id']) + } + result.append(status_info) + + return result + + def list_guests(self, host_id, tags=None, cpus=None, memory=None, hostname=None, + domain=None, local_disk=None, nic_speed=None, public_ip=None, + private_ip=None, **kwargs): + """Retrieve a list of all virtual servers on the dedicated host. + + Example:: + + # Print out a list of instances with 4 cpu cores in the host id 12345. + + for vsi in mgr.list_guests(host_id=12345, cpus=4): + print vsi['fullyQualifiedDomainName'], vsi['primaryIpAddress'] + + # Using a custom object-mask. Will get ONLY what is specified + object_mask = "mask[hostname,monitoringRobot[robotStatus]]" + for vsi in mgr.list_guests(mask=object_mask,cpus=4): + print vsi + + :param integer host_id: the identifier of dedicated host + :param list tags: filter based on list of tags + :param integer cpus: filter based on number of CPUS + :param integer memory: filter based on amount of memory + :param string hostname: filter based on hostname + :param string domain: filter based on domain + :param string local_disk: filter based on local_disk + :param integer nic_speed: filter based on network speed (in MBPS) + :param string public_ip: filter based on public ip address + :param string private_ip: filter based on private ip address + :param dict \\*\\*kwargs: response-level options (mask, limit, etc.) + :returns: Returns a list of dictionaries representing the matching + virtual servers + """ + if 'mask' not in kwargs: + items = [ + 'id', + 'globalIdentifier', + 'hostname', + 'domain', + 'fullyQualifiedDomainName', + 'primaryBackendIpAddress', + 'primaryIpAddress', + 'lastKnownPowerState.name', + 'hourlyBillingFlag', + 'powerState', + 'maxCpu', + 'maxMemory', + 'datacenter', + 'activeTransaction.transactionStatus[friendlyName,name]', + 'status', + ] + kwargs['mask'] = "mask[%s]" % ','.join(items) + + _filter = utils.NestedDict(kwargs.get('filter') or {}) + + if tags: + _filter['guests']['tagReferences']['tag']['name'] = { + 'operation': 'in', + 'options': [{'name': 'data', 'value': tags}], + } + + if cpus: + _filter['guests']['maxCpu'] = utils.query_filter(cpus) + + if memory: + _filter['guests']['maxMemory'] = utils.query_filter(memory) + + if hostname: + _filter['guests']['hostname'] = utils.query_filter(hostname) + + if domain: + _filter['guests']['domain'] = utils.query_filter(domain) + + if local_disk is not None: + _filter['guests']['localDiskFlag'] = ( + utils.query_filter(bool(local_disk))) + + if nic_speed: + _filter['guests']['networkComponents']['maxSpeed'] = ( + utils.query_filter(nic_speed)) + + if public_ip: + _filter['guests']['primaryIpAddress'] = ( + utils.query_filter(public_ip)) + + if private_ip: + _filter['guests']['primaryBackendIpAddress'] = ( + utils.query_filter(private_ip)) + + kwargs['filter'] = _filter.to_dict() + kwargs['iter'] = True + return self.host.getGuests(id=host_id, **kwargs) + def list_instances(self, tags=None, cpus=None, memory=None, hostname=None, disk=None, datacenter=None, **kwargs): """Retrieve a list of all dedicated hosts on the account @@ -384,3 +518,13 @@ def get_router_options(self, datacenter=None, flavor=None): item = self._get_item(package, flavor) return self._get_backend_router(location['location']['locationPackageDetails'], item) + + def _delete_guest(self, guest_id): + """Deletes a guest and returns 'Cancelled' or and Exception message""" + msg = 'Cancelled' + try: + self.guest.deleteObject(id=guest_id) + except SoftLayer.SoftLayerAPIError as e: + msg = 'Exception: ' + e.faultString + + return msg diff --git a/SoftLayer/managers/image.py b/SoftLayer/managers/image.py index 35f7c60c5..2eb4bc982 100644 --- a/SoftLayer/managers/image.py +++ b/SoftLayer/managers/image.py @@ -16,7 +16,7 @@ class ImageManager(utils.IdentifierMixin, object): """Manages SoftLayer server images. See product information here: - https://knowledgelayer.softlayer.com/topic/image-templates + https://console.bluemix.net/docs/infrastructure/image-templates/image_index.html :param SoftLayer.API.BaseClient client: the client instance """ diff --git a/setup.py b/setup.py index e99860a97..a345d2749 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ setup( name='SoftLayer', - version='5.6.3', + version='5.6.4', description=DESCRIPTION, long_description=LONG_DESCRIPTION, author='SoftLayer Technologies, Inc.', diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index f5ec61df5..dda9306a0 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -1,5 +1,5 @@ name: slcli # check to see if it's available -version: '5.6.3+git' # check versioning +version: '5.6.4+git' # check versioning summary: Python based SoftLayer API Tool. # 79 char long summary description: | A command-line interface is also included and can be used to manage various SoftLayer products and services. diff --git a/tests/CLI/modules/dedicatedhost_tests.py b/tests/CLI/modules/dedicatedhost_tests.py index 1769d8cbf..077c3f033 100644 --- a/tests/CLI/modules/dedicatedhost_tests.py +++ b/tests/CLI/modules/dedicatedhost_tests.py @@ -337,3 +337,70 @@ def test_create_verify_no_price_or_more_than_one(self): 'quantity': 1},) self.assert_called_with('SoftLayer_Product_Order', 'verifyOrder', args=args) + + @mock.patch('SoftLayer.DedicatedHostManager.cancel_host') + def test_cancel_host(self, cancel_mock): + result = self.run_command(['--really', 'dedicatedhost', 'cancel', '12345']) + + self.assert_no_fail(result) + cancel_mock.assert_called_with(12345) + + self.assertEqual(str(result.output), 'Dedicated Host 12345 was cancelled\n') + + def test_cancel_host_abort(self): + result = self.run_command(['dedicatedhost', 'cancel', '12345']) + self.assertEqual(result.exit_code, 2) + self.assertIsInstance(result.exception, exceptions.CLIAbort) + + def test_cancel_guests(self): + vs1 = {'id': 987, 'fullyQualifiedDomainName': 'foobar.example.com'} + vs2 = {'id': 654, 'fullyQualifiedDomainName': 'wombat.example.com'} + guests = self.set_mock('SoftLayer_Virtual_DedicatedHost', 'getGuests') + guests.return_value = [vs1, vs2] + + vs_status1 = {'id': 987, 'server name': 'foobar.example.com', 'status': 'Cancelled'} + vs_status2 = {'id': 654, 'server name': 'wombat.example.com', 'status': 'Cancelled'} + expected_result = [vs_status1, vs_status2] + + result = self.run_command(['--really', 'dedicatedhost', 'cancel-guests', '12345']) + self.assert_no_fail(result) + + self.assertEqual(expected_result, json.loads(result.output)) + + def test_cancel_guests_empty_list(self): + guests = self.set_mock('SoftLayer_Virtual_DedicatedHost', 'getGuests') + guests.return_value = [] + + result = self.run_command(['--really', 'dedicatedhost', 'cancel-guests', '12345']) + self.assert_no_fail(result) + + self.assertEqual(str(result.output), 'There is not any guest into the dedicated host 12345\n') + + def test_cancel_guests_abort(self): + result = self.run_command(['dedicatedhost', 'cancel-guests', '12345']) + self.assertEqual(result.exit_code, 2) + + self.assertIsInstance(result.exception, exceptions.CLIAbort) + + def test_list_guests(self): + result = self.run_command(['dh', 'list-guests', '123', '--tag=tag']) + + self.assert_no_fail(result) + self.assertEqual(json.loads(result.output), + [{'hostname': 'vs-test1', + 'domain': 'test.sftlyr.ws', + 'primary_ip': '172.16.240.2', + 'id': 200, + 'power_state': 'Running', + 'backend_ip': '10.45.19.37'}, + {'hostname': 'vs-test2', + 'domain': 'test.sftlyr.ws', + 'primary_ip': '172.16.240.7', + 'id': 202, + 'power_state': 'Running', + 'backend_ip': '10.45.19.35'}]) + + def _get_cancel_guests_return(self): + vs_status1 = {'id': 123, 'fqdn': 'foobar.example.com', 'status': 'Cancelled'} + vs_status2 = {'id': 456, 'fqdn': 'wombat.example.com', 'status': 'Cancelled'} + return [vs_status1, vs_status2] diff --git a/tests/CLI/modules/subnet_tests.py b/tests/CLI/modules/subnet_tests.py index b1b7e8f2b..72825e0f9 100644 --- a/tests/CLI/modules/subnet_tests.py +++ b/tests/CLI/modules/subnet_tests.py @@ -35,3 +35,7 @@ def test_detail(self): 'usable ips': 22 }, json.loads(result.output)) + + def test_list(self): + result = self.run_command(['subnet', 'list']) + self.assert_no_fail(result) diff --git a/tests/managers/dedicated_host_tests.py b/tests/managers/dedicated_host_tests.py index bdfa6d3f6..6888db3ce 100644 --- a/tests/managers/dedicated_host_tests.py +++ b/tests/managers/dedicated_host_tests.py @@ -540,6 +540,47 @@ def test_get_default_router_no_router_found(self): self.assertRaises(exceptions.SoftLayerError, self.dedicated_host._get_default_router, routers, 'notFound') + def test_cancel_host(self): + result = self.dedicated_host.cancel_host(789) + + self.assertEqual(result, True) + self.assert_called_with('SoftLayer_Virtual_DedicatedHost', 'deleteObject', identifier=789) + + def test_cancel_guests(self): + vs1 = {'id': 987, 'fullyQualifiedDomainName': 'foobar.example.com'} + vs2 = {'id': 654, 'fullyQualifiedDomainName': 'wombat.example.com'} + self.dedicated_host.host = mock.Mock() + self.dedicated_host.host.getGuests.return_value = [vs1, vs2] + + # Expected result + vs_status1 = {'id': 987, 'fqdn': 'foobar.example.com', 'status': 'Cancelled'} + vs_status2 = {'id': 654, 'fqdn': 'wombat.example.com', 'status': 'Cancelled'} + delete_status = [vs_status1, vs_status2] + + result = self.dedicated_host.cancel_guests(789) + + self.assertEqual(result, delete_status) + + def test_cancel_guests_empty_list(self): + self.dedicated_host.host = mock.Mock() + self.dedicated_host.host.getGuests.return_value = [] + + result = self.dedicated_host.cancel_guests(789) + + self.assertEqual(result, []) + + def test_delete_guest(self): + result = self.dedicated_host._delete_guest(123) + self.assertEqual(result, 'Cancelled') + + # delete_guest should return the exception message in case it fails + error_raised = SoftLayer.SoftLayerAPIError('SL Exception', 'SL message') + self.dedicated_host.guest = mock.Mock() + self.dedicated_host.guest.deleteObject.side_effect = error_raised + + result = self.dedicated_host._delete_guest(369) + self.assertEqual(result, 'Exception: SL message') + def _get_routers_sample(self): routers = [ { @@ -647,3 +688,34 @@ def _get_package(self): } return package + + def test_list_guests(self): + results = self.dedicated_host.list_guests(12345) + + for result in results: + self.assertIn(result['id'], [200, 202]) + self.assert_called_with('SoftLayer_Virtual_DedicatedHost', 'getGuests', identifier=12345) + + def test_list_guests_with_filters(self): + self.dedicated_host.list_guests(12345, tags=['tag1', 'tag2'], cpus=2, memory=1024, + hostname='hostname', domain='example.com', nic_speed=100, + public_ip='1.2.3.4', private_ip='4.3.2.1') + + _filter = { + 'guests': { + 'domain': {'operation': '_= example.com'}, + 'tagReferences': { + 'tag': {'name': { + 'operation': 'in', + 'options': [{ + 'name': 'data', 'value': ['tag1', 'tag2']}]}}}, + 'maxCpu': {'operation': 2}, + 'maxMemory': {'operation': 1024}, + 'hostname': {'operation': '_= hostname'}, + 'networkComponents': {'maxSpeed': {'operation': 100}}, + 'primaryIpAddress': {'operation': '_= 1.2.3.4'}, + 'primaryBackendIpAddress': {'operation': '_= 4.3.2.1'} + } + } + self.assert_called_with('SoftLayer_Virtual_DedicatedHost', 'getGuests', + identifier=12345, filter=_filter)