diff --git a/SoftLayer/CLI/subnet/create.py b/SoftLayer/CLI/subnet/create.py index 0f81c574b..1844cf627 100644 --- a/SoftLayer/CLI/subnet/create.py +++ b/SoftLayer/CLI/subnet/create.py @@ -13,7 +13,7 @@ @click.argument('network', type=click.Choice(['public', 'private'])) @click.argument('quantity', type=click.INT) @click.argument('vlan-id') -@click.option('--v6', '--ipv6', is_flag=True, help="Order IPv6 Addresses") +@click.option('--ipv6', '--v6', is_flag=True, help="Order IPv6 Addresses") @click.option('--test', is_flag=True, help="Do not order the subnet; just get a quote") @@ -42,14 +42,10 @@ def cli(env, network, quantity, vlan_id, ipv6, test): if ipv6: version = 6 - result = mgr.add_subnet(network, - quantity=quantity, - vlan_id=vlan_id, - version=version, - test_order=test) - if not result: - raise exceptions.CLIAbort( - 'Unable to place order: No valid price IDs found.') + try: + result = mgr.add_subnet(network, quantity=quantity, vlan_id=vlan_id, version=version, test_order=test) + except SoftLayer.SoftLayerAPIError: + raise exceptions.CLIAbort('There is no price id for {} {} ipv{}'.format(quantity, network, version)) table = formatting.Table(['Item', 'cost']) table.align['Item'] = 'r' diff --git a/SoftLayer/managers/network.py b/SoftLayer/managers/network.py index 4223143ae..4ecd76d3f 100644 --- a/SoftLayer/managers/network.py +++ b/SoftLayer/managers/network.py @@ -150,10 +150,6 @@ def add_subnet(self, subnet_type, quantity=None, vlan_id=None, version=4, price_id = item['prices'][0]['id'] break - if not price_id: - raise TypeError('Invalid combination specified for ordering a' - ' subnet.') - order = { 'packageId': 0, 'prices': [{'id': price_id}], diff --git a/tests/CLI/modules/subnet_tests.py b/tests/CLI/modules/subnet_tests.py index 72825e0f9..47cb7e473 100644 --- a/tests/CLI/modules/subnet_tests.py +++ b/tests/CLI/modules/subnet_tests.py @@ -4,12 +4,17 @@ :license: MIT, see LICENSE for more details. """ +from SoftLayer.fixtures import SoftLayer_Product_Order +from SoftLayer.fixtures import SoftLayer_Product_Package from SoftLayer import testing import json +import mock +import SoftLayer class SubnetTests(testing.TestCase): + def test_detail(self): result = self.run_command(['subnet', 'detail', '1234']) @@ -39,3 +44,54 @@ def test_detail(self): def test_list(self): result = self.run_command(['subnet', 'list']) self.assert_no_fail(result) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_subnet_ipv4(self, confirm_mock): + confirm_mock.return_value = True + + item_mock = self.set_mock('SoftLayer_Product_Package', 'getItems') + item_mock.return_value = SoftLayer_Product_Package.getItems + + place_mock = self.set_mock('SoftLayer_Product_Order', 'placeOrder') + place_mock.return_value = SoftLayer_Product_Order.placeOrder + + result = self.run_command(['subnet', 'create', 'private', '8', '12346']) + self.assert_no_fail(result) + + output = [ + {'Item': 'Total monthly cost', 'cost': '0.00'} + ] + + self.assertEqual(output, json.loads(result.output)) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_create_subnet_ipv6(self, confirm_mock): + confirm_mock.return_value = True + + item_mock = self.set_mock('SoftLayer_Product_Package', 'getItems') + item_mock.return_value = SoftLayer_Product_Package.getItems + + place_mock = self.set_mock('SoftLayer_Product_Order', 'verifyOrder') + place_mock.return_value = SoftLayer_Product_Order.verifyOrder + + result = self.run_command(['subnet', 'create', '--v6', 'public', '64', '12346', '--test']) + self.assert_no_fail(result) + + output = [ + {'Item': 'this is a thing', 'cost': '2.00'}, + {'Item': 'Total monthly cost', 'cost': '2.00'} + ] + + self.assertEqual(output, json.loads(result.output)) + + def test_create_subnet_no_prices_found(self): + item_mock = self.set_mock('SoftLayer_Product_Package', 'getItems') + item_mock.return_value = SoftLayer_Product_Package.getItems + + verify_mock = self.set_mock('SoftLayer_Product_Order', 'verifyOrder') + verify_mock.side_effect = SoftLayer.SoftLayerAPIError('SoftLayer_Exception', 'Price not found') + + result = self.run_command(['subnet', 'create', '--v6', 'public', '32', '12346', '--test']) + + self.assertRaises(SoftLayer.SoftLayerAPIError, verify_mock) + self.assertEqual(result.exception.message, 'There is no price id for 32 public ipv6') diff --git a/tests/managers/network_tests.py b/tests/managers/network_tests.py index 7a84bebae..223b7a3b5 100644 --- a/tests/managers/network_tests.py +++ b/tests/managers/network_tests.py @@ -24,9 +24,6 @@ def test_ip_lookup(self): 'getByIpAddress', args=('10.0.1.37',)) - def test_add_subnet_raises_exception_on_failure(self): - self.assertRaises(TypeError, self.network.add_subnet, ('bad')) - def test_add_global_ip(self): # Test a global IPv4 order result = self.network.add_global_ip(test_order=True)