Skip to content

slcli vlan cancel should report if a vlan is automatic #1495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SoftLayer/CLI/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),

Expand Down
35 changes: 35 additions & 0 deletions SoftLayer/CLI/vlan/cancel.py
Original file line number Diff line number Diff line change
@@ -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")
17 changes: 16 additions & 1 deletion SoftLayer/fixtures/SoftLayer_Network_Vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
]
22 changes: 22 additions & 0 deletions SoftLayer/managers/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'primaryRouter[id, fullyQualifiedDomainName, datacenter]',
'totalPrimaryIpAddressCount',
'networkSpace',
'billingItem',
'hardware',
'subnets',
'virtualGuests',
Expand Down Expand Up @@ -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)
4 changes: 4 additions & 0 deletions docs/cli/vlan.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
20 changes: 20 additions & 0 deletions tests/CLI/modules/vlan_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)