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 1 commit
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
30 changes: 30 additions & 0 deletions SoftLayer/CLI/vlan/cancel.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 11 additions & 1 deletion SoftLayer/fixtures/SoftLayer_Network_Vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 27 additions & 0 deletions SoftLayer/managers/billing.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 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,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)
5 changes: 5 additions & 0 deletions docs/api/managers/billing.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. _billing:

.. automodule:: SoftLayer.managers.billing
:members:
:inherited-members:
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:
12 changes: 12 additions & 0 deletions tests/CLI/modules/vlan_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)