diff --git a/SoftLayer/CLI/routes.py b/SoftLayer/CLI/routes.py index d5174ffde..6672477ed 100644 --- a/SoftLayer/CLI/routes.py +++ b/SoftLayer/CLI/routes.py @@ -336,6 +336,7 @@ ('vlan:detail', 'SoftLayer.CLI.vlan.detail:cli'), ('vlan:edit', 'SoftLayer.CLI.vlan.edit:cli'), ('vlan:list', 'SoftLayer.CLI.vlan.list:cli'), + ('vlan:cancel', 'SoftLayer.CLI.vlan.cancel:cli'), ('summary', 'SoftLayer.CLI.summary:cli'), diff --git a/SoftLayer/CLI/vlan/cancel.py b/SoftLayer/CLI/vlan/cancel.py new file mode 100644 index 000000000..35f5aa0f9 --- /dev/null +++ b/SoftLayer/CLI/vlan/cancel.py @@ -0,0 +1,35 @@ +"""Cancel Network Vlan.""" +# :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 + + +@click.command() +@click.argument('identifier') +@environment.pass_env +def cli(env, identifier): + """Cancel network VLAN.""" + + mgr = SoftLayer.NetworkManager(env.client) + + if not (env.skip_confirmations or formatting.no_going_back(identifier)): + raise exceptions.CLIAbort('Aborted') + + reasons = mgr.get_cancel_failure_reasons(identifier) + if len(reasons) > 0: + raise exceptions.CLIAbort(reasons) + item = mgr.get_vlan(identifier).get('billingItem') + if item: + mgr.cancel_item(item.get('id'), + True, + 'Cancel by cli command', + 'Cancel by cli command') + else: + raise exceptions.CLIAbort( + "VLAN is an automatically assigned and free of charge VLAN," + " it will automatically be removed from your account when it is empty") diff --git a/SoftLayer/fixtures/SoftLayer_Network_Vlan.py b/SoftLayer/fixtures/SoftLayer_Network_Vlan.py index 758fe3b39..960c98995 100644 --- a/SoftLayer/fixtures/SoftLayer_Network_Vlan.py +++ b/SoftLayer/fixtures/SoftLayer_Network_Vlan.py @@ -5,9 +5,24 @@ }, 'id': 1234, 'vlanNumber': 4444, - 'firewallInterfaces': None + 'firewallInterfaces': None, + 'billingItem': { + 'allowCancellationFlag': 1, + 'categoryCode': 'network_vlan', + 'description': 'Private Network Vlan', + 'id': 235689, + 'notes': 'test cli', + 'orderItemId': 147258, + } } editObject = True setTags = True getList = [getObject] + +cancel = True + +getCancelFailureReasons = [ + "1 bare metal server(s) still on the VLAN ", + "1 virtual guest(s) still on the VLAN " +] diff --git a/SoftLayer/managers/network.py b/SoftLayer/managers/network.py index d609de5d5..7c86dda58 100644 --- a/SoftLayer/managers/network.py +++ b/SoftLayer/managers/network.py @@ -49,6 +49,7 @@ 'primaryRouter[id, fullyQualifiedDomainName, datacenter]', 'totalPrimaryIpAddressCount', 'networkSpace', + 'billingItem', 'hardware', 'subnets', 'virtualGuests', @@ -752,3 +753,24 @@ def set_subnet_ipddress_note(self, identifier, note): """ result = self.client.call('SoftLayer_Network_Subnet_IpAddress', 'editObject', note, id=identifier) return result + + def get_cancel_failure_reasons(self, identifier): + """get the reasons why we cannot cancel the VLAN. + + :param integer identifier: the instance ID + """ + return self.vlan.getCancelFailureReasons(id=identifier) + + def cancel_item(self, identifier, cancel_immediately, + reason_cancel, customer_note): + """Cancel a billing item immediately, deleting all its data. + + :param integer identifier: the instance ID to cancel + :param string reason_cancel: reason cancel + """ + return self.client.call('SoftLayer_Billing_Item', 'cancelItem', + True, + cancel_immediately, + reason_cancel, + customer_note, + id=identifier) diff --git a/docs/cli/vlan.rst b/docs/cli/vlan.rst index 6fc084da7..57a58932a 100644 --- a/docs/cli/vlan.rst +++ b/docs/cli/vlan.rst @@ -14,3 +14,7 @@ VLANs .. click:: SoftLayer.CLI.vlan.list:cli :prog: vlan list :show-nested: + +.. click:: SoftLayer.CLI.vlan.cancel:cli + :prog: vlan cancel + :show-nested: diff --git a/tests/CLI/modules/vlan_tests.py b/tests/CLI/modules/vlan_tests.py index 73c1fab97..af2b22290 100644 --- a/tests/CLI/modules/vlan_tests.py +++ b/tests/CLI/modules/vlan_tests.py @@ -99,3 +99,23 @@ def test_vlan_list(self): result = self.run_command(['vlan', 'list']) self.assert_no_fail(result) self.assert_called_with('SoftLayer_Account', 'getNetworkVlans') + + @mock.patch('SoftLayer.CLI.formatting.no_going_back') + def test_vlan_cancel(self, confirm_mock): + confirm_mock.return_value = True + mock = self.set_mock('SoftLayer_Network_Vlan', 'getCancelFailureReasons') + mock.return_value = [] + result = self.run_command(['vlan', 'cancel', '1234']) + self.assert_no_fail(result) + + @mock.patch('SoftLayer.CLI.formatting.no_going_back') + def test_vlan_cancel_error(self, confirm_mock): + confirm_mock.return_value = True + result = self.run_command(['vlan', 'cancel', '1234']) + self.assertTrue(result.exit_code, 2) + + @mock.patch('SoftLayer.CLI.formatting.no_going_back') + def test_vlan_cancel_fail(self, confirm_mock): + confirm_mock.return_value = False + result = self.run_command(['vlan', 'cancel', '1234']) + self.assertTrue(result.exit_code, 2)