From b97540bf0eb7bbd9c16d3a745cbf3a5b7d9aa238 Mon Sep 17 00:00:00 2001 From: caberos Date: Thu, 27 May 2021 17:32:58 -0400 Subject: [PATCH 1/4] slcli vlan cancel should report if a vlan is automatic --- SoftLayer/CLI/routes.py | 1 + SoftLayer/CLI/vlan/cancel.py | 30 ++++++++++++++++++++ SoftLayer/fixtures/SoftLayer_Network_Vlan.py | 12 +++++++- SoftLayer/managers/billing.py | 27 ++++++++++++++++++ SoftLayer/managers/network.py | 8 ++++++ docs/api/managers/billing.rst | 5 ++++ docs/cli/vlan.rst | 4 +++ tests/CLI/modules/vlan_tests.py | 12 ++++++++ 8 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 SoftLayer/CLI/vlan/cancel.py create mode 100644 SoftLayer/managers/billing.py create mode 100644 docs/api/managers/billing.rst 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..7bef5e458 --- /dev/null +++ b/SoftLayer/CLI/vlan/cancel.py @@ -0,0 +1,30 @@ +"""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 +from SoftLayer.managers.billing import BillingManager + + +@click.command() +@click.argument('identifier') +@environment.pass_env +def cli(env, identifier): + """Cancel network vlan.""" + + mgr = SoftLayer.NetworkManager(env.client) + billing = BillingManager(env.client) + if not (env.skip_confirmations or formatting.no_going_back(identifier)): + raise exceptions.CLIAbort('Aborted') + + item = mgr.get_vlan(identifier).get('billingItem') + if item: + billing.cancel_item(item.get('id'), 'cancel by cli command') + env.fout('Cancel Successfully') + else: + res = mgr.get_cancel_failure_reasons(identifier) + raise exceptions.ArgumentError(res) diff --git a/SoftLayer/fixtures/SoftLayer_Network_Vlan.py b/SoftLayer/fixtures/SoftLayer_Network_Vlan.py index 758fe3b39..4e1c100ad 100644 --- a/SoftLayer/fixtures/SoftLayer_Network_Vlan.py +++ b/SoftLayer/fixtures/SoftLayer_Network_Vlan.py @@ -5,9 +5,19 @@ }, '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 diff --git a/SoftLayer/managers/billing.py b/SoftLayer/managers/billing.py new file mode 100644 index 000000000..bb2c11e23 --- /dev/null +++ b/SoftLayer/managers/billing.py @@ -0,0 +1,27 @@ +""" + SoftLayer.BillingItem + ~~~~~~~~~~~~~~~~~~~ + BillingItem manager + + :license: MIT, see LICENSE for more details. +""" + + +class BillingManager(object): + """Manager for interacting with Billing item instances.""" + + def __init__(self, client): + self.client = client + + def cancel_item(self, identifier, reason_cancel): + """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, + True, + reason_cancel, + reason_cancel, + id=identifier) diff --git a/SoftLayer/managers/network.py b/SoftLayer/managers/network.py index d609de5d5..50429cbb1 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,10 @@ 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 by cannot cancel the VLAN + + :param integer identifier: the instance ID + """ + return self.vlan.getCancelFailureReasons(id=identifier) diff --git a/docs/api/managers/billing.rst b/docs/api/managers/billing.rst new file mode 100644 index 000000000..f44a9333c --- /dev/null +++ b/docs/api/managers/billing.rst @@ -0,0 +1,5 @@ +.. _billing: + +.. automodule:: SoftLayer.managers.billing + :members: + :inherited-members: \ No newline at end of file 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..59de39ad3 100644 --- a/tests/CLI/modules/vlan_tests.py +++ b/tests/CLI/modules/vlan_tests.py @@ -99,3 +99,15 @@ 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 + result = self.run_command(['vlan', 'cancel', '1234']) + self.assert_no_fail(result) + + @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) From 47d14d0918cfe16cc6c99d088b664c9b65fd46c4 Mon Sep 17 00:00:00 2001 From: caberos Date: Fri, 4 Jun 2021 10:49:25 -0400 Subject: [PATCH 2/4] fix team code review comments --- SoftLayer/CLI/vlan/cancel.py | 17 +++++++----- SoftLayer/fixtures/SoftLayer_Network_Vlan.py | 5 ++++ SoftLayer/managers/billing.py | 27 -------------------- SoftLayer/managers/network.py | 16 +++++++++++- tests/CLI/modules/vlan_tests.py | 8 ++++++ 5 files changed, 39 insertions(+), 34 deletions(-) delete mode 100644 SoftLayer/managers/billing.py diff --git a/SoftLayer/CLI/vlan/cancel.py b/SoftLayer/CLI/vlan/cancel.py index 7bef5e458..79647a7eb 100644 --- a/SoftLayer/CLI/vlan/cancel.py +++ b/SoftLayer/CLI/vlan/cancel.py @@ -7,7 +7,6 @@ from SoftLayer.CLI import environment from SoftLayer.CLI import exceptions from SoftLayer.CLI import formatting -from SoftLayer.managers.billing import BillingManager @click.command() @@ -17,14 +16,20 @@ def cli(env, identifier): """Cancel network vlan.""" mgr = SoftLayer.NetworkManager(env.client) - billing = BillingManager(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: - billing.cancel_item(item.get('id'), 'cancel by cli command') - env.fout('Cancel Successfully') + mgr.cancel_item(item.get('id'), + True, + 'Cancel by cli command', + 'Cancel by cli command') else: - res = mgr.get_cancel_failure_reasons(identifier) - raise exceptions.ArgumentError(res) + 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 4e1c100ad..960c98995 100644 --- a/SoftLayer/fixtures/SoftLayer_Network_Vlan.py +++ b/SoftLayer/fixtures/SoftLayer_Network_Vlan.py @@ -21,3 +21,8 @@ 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/billing.py b/SoftLayer/managers/billing.py deleted file mode 100644 index bb2c11e23..000000000 --- a/SoftLayer/managers/billing.py +++ /dev/null @@ -1,27 +0,0 @@ -""" - SoftLayer.BillingItem - ~~~~~~~~~~~~~~~~~~~ - BillingItem manager - - :license: MIT, see LICENSE for more details. -""" - - -class BillingManager(object): - """Manager for interacting with Billing item instances.""" - - def __init__(self, client): - self.client = client - - def cancel_item(self, identifier, reason_cancel): - """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, - True, - reason_cancel, - reason_cancel, - id=identifier) diff --git a/SoftLayer/managers/network.py b/SoftLayer/managers/network.py index 50429cbb1..7c86dda58 100644 --- a/SoftLayer/managers/network.py +++ b/SoftLayer/managers/network.py @@ -755,8 +755,22 @@ def set_subnet_ipddress_note(self, identifier, note): return result def get_cancel_failure_reasons(self, identifier): - """get the reasons by cannot cancel the VLAN + """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/tests/CLI/modules/vlan_tests.py b/tests/CLI/modules/vlan_tests.py index 59de39ad3..af2b22290 100644 --- a/tests/CLI/modules/vlan_tests.py +++ b/tests/CLI/modules/vlan_tests.py @@ -103,9 +103,17 @@ def test_vlan_list(self): @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 From 0b0e2ef76dc5329314bf888a5b7e8a8f978e487a Mon Sep 17 00:00:00 2001 From: caberos Date: Wed, 9 Jun 2021 11:14:51 -0400 Subject: [PATCH 3/4] fix the last code review comments --- docs/api/managers/billing.rst | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 docs/api/managers/billing.rst diff --git a/docs/api/managers/billing.rst b/docs/api/managers/billing.rst deleted file mode 100644 index f44a9333c..000000000 --- a/docs/api/managers/billing.rst +++ /dev/null @@ -1,5 +0,0 @@ -.. _billing: - -.. automodule:: SoftLayer.managers.billing - :members: - :inherited-members: \ No newline at end of file From bee41a23aead3ab4a46e5ce7dc46a85ef4368dab Mon Sep 17 00:00:00 2001 From: caberos Date: Wed, 9 Jun 2021 14:24:21 -0400 Subject: [PATCH 4/4] fix the last code review comments --- SoftLayer/CLI/vlan/cancel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SoftLayer/CLI/vlan/cancel.py b/SoftLayer/CLI/vlan/cancel.py index 79647a7eb..35f5aa0f9 100644 --- a/SoftLayer/CLI/vlan/cancel.py +++ b/SoftLayer/CLI/vlan/cancel.py @@ -13,7 +13,7 @@ @click.argument('identifier') @environment.pass_env def cli(env, identifier): - """Cancel network vlan.""" + """Cancel network VLAN.""" mgr = SoftLayer.NetworkManager(env.client)