Skip to content

Commit 275fc96

Browse files
Merge pull request #1495 from caberos/issue1489
slcli vlan cancel should report if a vlan is automatic
2 parents 09e714c + bee41a2 commit 275fc96

File tree

6 files changed

+98
-1
lines changed

6 files changed

+98
-1
lines changed

Diff for: SoftLayer/CLI/routes.py

+1
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@
341341
('vlan:detail', 'SoftLayer.CLI.vlan.detail:cli'),
342342
('vlan:edit', 'SoftLayer.CLI.vlan.edit:cli'),
343343
('vlan:list', 'SoftLayer.CLI.vlan.list:cli'),
344+
('vlan:cancel', 'SoftLayer.CLI.vlan.cancel:cli'),
344345

345346
('summary', 'SoftLayer.CLI.summary:cli'),
346347

Diff for: SoftLayer/CLI/vlan/cancel.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Cancel Network Vlan."""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import click
5+
6+
import SoftLayer
7+
from SoftLayer.CLI import environment
8+
from SoftLayer.CLI import exceptions
9+
from SoftLayer.CLI import formatting
10+
11+
12+
@click.command()
13+
@click.argument('identifier')
14+
@environment.pass_env
15+
def cli(env, identifier):
16+
"""Cancel network VLAN."""
17+
18+
mgr = SoftLayer.NetworkManager(env.client)
19+
20+
if not (env.skip_confirmations or formatting.no_going_back(identifier)):
21+
raise exceptions.CLIAbort('Aborted')
22+
23+
reasons = mgr.get_cancel_failure_reasons(identifier)
24+
if len(reasons) > 0:
25+
raise exceptions.CLIAbort(reasons)
26+
item = mgr.get_vlan(identifier).get('billingItem')
27+
if item:
28+
mgr.cancel_item(item.get('id'),
29+
True,
30+
'Cancel by cli command',
31+
'Cancel by cli command')
32+
else:
33+
raise exceptions.CLIAbort(
34+
"VLAN is an automatically assigned and free of charge VLAN,"
35+
" it will automatically be removed from your account when it is empty")

Diff for: SoftLayer/fixtures/SoftLayer_Network_Vlan.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,24 @@
55
},
66
'id': 1234,
77
'vlanNumber': 4444,
8-
'firewallInterfaces': None
8+
'firewallInterfaces': None,
9+
'billingItem': {
10+
'allowCancellationFlag': 1,
11+
'categoryCode': 'network_vlan',
12+
'description': 'Private Network Vlan',
13+
'id': 235689,
14+
'notes': 'test cli',
15+
'orderItemId': 147258,
16+
}
917
}
1018

1119
editObject = True
1220
setTags = True
1321
getList = [getObject]
22+
23+
cancel = True
24+
25+
getCancelFailureReasons = [
26+
"1 bare metal server(s) still on the VLAN ",
27+
"1 virtual guest(s) still on the VLAN "
28+
]

Diff for: SoftLayer/managers/network.py

+22
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
'primaryRouter[id, fullyQualifiedDomainName, datacenter]',
5050
'totalPrimaryIpAddressCount',
5151
'networkSpace',
52+
'billingItem',
5253
'hardware',
5354
'subnets',
5455
'virtualGuests',
@@ -752,3 +753,24 @@ def set_subnet_ipddress_note(self, identifier, note):
752753
"""
753754
result = self.client.call('SoftLayer_Network_Subnet_IpAddress', 'editObject', note, id=identifier)
754755
return result
756+
757+
def get_cancel_failure_reasons(self, identifier):
758+
"""get the reasons why we cannot cancel the VLAN.
759+
760+
:param integer identifier: the instance ID
761+
"""
762+
return self.vlan.getCancelFailureReasons(id=identifier)
763+
764+
def cancel_item(self, identifier, cancel_immediately,
765+
reason_cancel, customer_note):
766+
"""Cancel a billing item immediately, deleting all its data.
767+
768+
:param integer identifier: the instance ID to cancel
769+
:param string reason_cancel: reason cancel
770+
"""
771+
return self.client.call('SoftLayer_Billing_Item', 'cancelItem',
772+
True,
773+
cancel_immediately,
774+
reason_cancel,
775+
customer_note,
776+
id=identifier)

Diff for: docs/cli/vlan.rst

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ VLANs
1414
.. click:: SoftLayer.CLI.vlan.list:cli
1515
:prog: vlan list
1616
:show-nested:
17+
18+
.. click:: SoftLayer.CLI.vlan.cancel:cli
19+
:prog: vlan cancel
20+
:show-nested:

Diff for: tests/CLI/modules/vlan_tests.py

+20
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,23 @@ def test_vlan_list(self):
9999
result = self.run_command(['vlan', 'list'])
100100
self.assert_no_fail(result)
101101
self.assert_called_with('SoftLayer_Account', 'getNetworkVlans')
102+
103+
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
104+
def test_vlan_cancel(self, confirm_mock):
105+
confirm_mock.return_value = True
106+
mock = self.set_mock('SoftLayer_Network_Vlan', 'getCancelFailureReasons')
107+
mock.return_value = []
108+
result = self.run_command(['vlan', 'cancel', '1234'])
109+
self.assert_no_fail(result)
110+
111+
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
112+
def test_vlan_cancel_error(self, confirm_mock):
113+
confirm_mock.return_value = True
114+
result = self.run_command(['vlan', 'cancel', '1234'])
115+
self.assertTrue(result.exit_code, 2)
116+
117+
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
118+
def test_vlan_cancel_fail(self, confirm_mock):
119+
confirm_mock.return_value = False
120+
result = self.run_command(['vlan', 'cancel', '1234'])
121+
self.assertTrue(result.exit_code, 2)

0 commit comments

Comments
 (0)