Skip to content
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

Example and sub feature for slcli firewall monitoring, slcli globalip specific commands #2033 #2056

Merged
merged 2 commits into from Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion SoftLayer/CLI/firewall/monitoring.py
Expand Up @@ -13,7 +13,11 @@
@click.argument('identifier')
@environment.pass_env
def cli(env, identifier):
"""Gets bandwidth details for a firewall from the past 30 days."""
"""Gets bandwidth details for a firewall from the past 30 days.

Example::
slcli firewall monitoring vs:12345
"""

mgr = SoftLayer.FirewallManager(env.client)

Expand Down
7 changes: 6 additions & 1 deletion SoftLayer/CLI/globalip/assign.py
Expand Up @@ -20,7 +20,12 @@
@click.option('--target-id', help='The identifier for the destination resource to route this subnet to. ')
@environment.pass_env
def cli(env, identifier, target, target_id):
"""Assigns the subnet to a target."""
"""Assigns the subnet to a target.

Example::
slcli globalip assign 12345678 9.111.123.456
This command assigns IP address with ID 12345678 to a target device whose IP address is 9.111.123.456
"""

mgr = SoftLayer.NetworkManager(env.client)
mgr.route(identifier, target_types.get(target), target_id)
15 changes: 11 additions & 4 deletions SoftLayer/CLI/globalip/cancel.py
Expand Up @@ -12,15 +12,22 @@

@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@click.option('-f', '--force', default=False, is_flag=True, help="Force operation without confirmation")
@environment.pass_env
def cli(env, identifier):
"""Cancel global IP."""
def cli(env, identifier, force):
"""Cancel global IP.

Example::
slcli globalip cancel 12345
"""

mgr = SoftLayer.NetworkManager(env.client)
global_ip_id = helpers.resolve_id(mgr.resolve_global_ip_ids, identifier,
name='global ip')

if not (env.skip_confirmations or formatting.no_going_back(global_ip_id)):
raise exceptions.CLIAbort('Aborted')
if not force:
if not (env.skip_confirmations or
formatting.confirm(f"This will cancel the IP address: {global_ip_id} and cannot be undone. Continue?")):
raise exceptions.CLIAbort('Aborted')

mgr.cancel_global_ip(global_ip_id)
18 changes: 12 additions & 6 deletions SoftLayer/CLI/globalip/create.py
Expand Up @@ -12,20 +12,26 @@
@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.option('-v6', '--ipv6', is_flag=True, help='Order a IPv6 IP')
@click.option('--test', help='test order')
@click.option('-f', '--force', default=False, is_flag=True, help="Force operation without confirmation")
@environment.pass_env
def cli(env, ipv6, test):
"""Creates a global IP."""
def cli(env, ipv6, test, force):
"""Creates a global IP.

Example::
slcli globalip create -v6
This command creates an IPv6 address.
"""

mgr = SoftLayer.NetworkManager(env.client)

version = 4
if ipv6:
version = 6

if not (test or env.skip_confirmations):
if not formatting.confirm("This action will incur charges on your "
"account. Continue?"):
raise exceptions.CLIAbort('Cancelling order.')
if not force:
if not (test or env.skip_confirmations):
if not formatting.confirm("This action will incur charges on your account. Continue?"):
raise exceptions.CLIAbort('Cancelling order.')

result = mgr.add_global_ip(version=version, test_order=test)

Expand Down
6 changes: 5 additions & 1 deletion SoftLayer/CLI/globalip/list.py
Expand Up @@ -14,7 +14,11 @@
type=click.Choice(['v4', 'v6']))
@environment.pass_env
def cli(env, ip_version):
"""List all global IPs."""
"""List all global IPs.

Example::
slcli globalip list
"""

mgr = SoftLayer.NetworkManager(env.client)

Expand Down
27 changes: 25 additions & 2 deletions tests/CLI/modules/globalip_tests.py
Expand Up @@ -39,8 +39,7 @@ def test_ip_cancel(self, no_going_back_mock):
no_going_back_mock.return_value = False
result = self.run_command(['globalip', 'cancel', '1'])

self.assertEqual(result.exit_code, 2)
self.assertIsInstance(result.exception, exceptions.CLIAbort)
self.assertEqual(result.exit_code, 0)

def test_ip_list(self):
result = self.run_command(['globalip', 'list', '--ip-version=v4'])
Expand Down Expand Up @@ -85,3 +84,27 @@ def test_ip_unassign(self):
result = self.run_command(['globalip', 'unassign', '1'])
self.assert_no_fail(result)
self.assertEqual(result.output, "")

def test_ip_cancel_force(self):
result = self.run_command(['globalip', 'cancel', '1', '--force'])

self.assert_no_fail(result)
self.assertEqual(result.exit_code, 0)

@mock.patch('SoftLayer.CLI.formatting.confirm')
def test_ip_cancel_no_abort(self, confirm_mock):
# Test with confirmation and responding negatively
confirm_mock.return_value = True
result = self.run_command(['globalip', 'cancel', '1'])

self.assert_no_fail(result)
self.assertEqual(result.exit_code, 0)

@mock.patch('SoftLayer.CLI.formatting.confirm')
def test_ip_cancel_abort(self, confirm_mock):
# Test with confirmation and responding negatively
confirm_mock.return_value = False
result = self.run_command(['globalip', 'cancel', '1'])

self.assertEqual(result.exit_code, 2)
self.assertIsInstance(result.exception, exceptions.CLIAbort)