Skip to content

Commit b97540b

Browse files
caberoscaberos
authored andcommitted
slcli vlan cancel should report if a vlan is automatic
1 parent 41a196d commit b97540b

File tree

8 files changed

+98
-1
lines changed

8 files changed

+98
-1
lines changed

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@
336336
('vlan:detail', 'SoftLayer.CLI.vlan.detail:cli'),
337337
('vlan:edit', 'SoftLayer.CLI.vlan.edit:cli'),
338338
('vlan:list', 'SoftLayer.CLI.vlan.list:cli'),
339+
('vlan:cancel', 'SoftLayer.CLI.vlan.cancel:cli'),
339340

340341
('summary', 'SoftLayer.CLI.summary:cli'),
341342

SoftLayer/CLI/vlan/cancel.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
from SoftLayer.managers.billing import BillingManager
11+
12+
13+
@click.command()
14+
@click.argument('identifier')
15+
@environment.pass_env
16+
def cli(env, identifier):
17+
"""Cancel network vlan."""
18+
19+
mgr = SoftLayer.NetworkManager(env.client)
20+
billing = BillingManager(env.client)
21+
if not (env.skip_confirmations or formatting.no_going_back(identifier)):
22+
raise exceptions.CLIAbort('Aborted')
23+
24+
item = mgr.get_vlan(identifier).get('billingItem')
25+
if item:
26+
billing.cancel_item(item.get('id'), 'cancel by cli command')
27+
env.fout('Cancel Successfully')
28+
else:
29+
res = mgr.get_cancel_failure_reasons(identifier)
30+
raise exceptions.ArgumentError(res)

SoftLayer/fixtures/SoftLayer_Network_Vlan.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@
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

SoftLayer/managers/billing.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
SoftLayer.BillingItem
3+
~~~~~~~~~~~~~~~~~~~
4+
BillingItem manager
5+
6+
:license: MIT, see LICENSE for more details.
7+
"""
8+
9+
10+
class BillingManager(object):
11+
"""Manager for interacting with Billing item instances."""
12+
13+
def __init__(self, client):
14+
self.client = client
15+
16+
def cancel_item(self, identifier, reason_cancel):
17+
"""Cancel a billing item immediately, deleting all its data.
18+
19+
:param integer identifier: the instance ID to cancel
20+
:param string reason_cancel: reason cancel
21+
"""
22+
return self.client.call('SoftLayer_Billing_Item', 'cancelItem',
23+
True,
24+
True,
25+
reason_cancel,
26+
reason_cancel,
27+
id=identifier)

SoftLayer/managers/network.py

Lines changed: 8 additions & 0 deletions
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,10 @@ 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 by cannot cancel the VLAN
759+
760+
:param integer identifier: the instance ID
761+
"""
762+
return self.vlan.getCancelFailureReasons(id=identifier)

docs/api/managers/billing.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.. _billing:
2+
3+
.. automodule:: SoftLayer.managers.billing
4+
:members:
5+
:inherited-members:

docs/cli/vlan.rst

Lines changed: 4 additions & 0 deletions
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:

tests/CLI/modules/vlan_tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,15 @@ 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+
result = self.run_command(['vlan', 'cancel', '1234'])
107+
self.assert_no_fail(result)
108+
109+
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
110+
def test_vlan_cancel_fail(self, confirm_mock):
111+
confirm_mock.return_value = False
112+
result = self.run_command(['vlan', 'cancel', '1234'])
113+
self.assertTrue(result.exit_code, 2)

0 commit comments

Comments
 (0)