Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
--force option for slcli firewall add, firewall cancel command
  • Loading branch information
J Jayasilan authored and J Jayasilan committed Jul 21, 2023
1 parent cd58715 commit b2a396e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
17 changes: 7 additions & 10 deletions SoftLayer/CLI/firewall/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@

@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('target')
@click.option('--firewall-type',
type=click.Choice(['vs', 'vlan', 'server']),
help='Firewall type',
required=True)
@click.option('-ha', '--high-availability',
is_flag=True,
help='High available firewall option')
@click.option('--firewall-type', type=click.Choice(['vs', 'vlan', 'server']), help='Firewall type', required=True)
@click.option("-h", '--high-availability', is_flag=True, help='High available firewall option')
@click.option('-f', '--force', default=False, is_flag=True, help="Force addition of firewall to the server")
@environment.pass_env
def cli(env, target, firewall_type, high_availability):
def cli(env, target, firewall_type, high_availability, force):
"""Create new firewall.
TARGET: Id of the server the firewall will protect
Expand All @@ -43,8 +39,9 @@ def cli(env, target, firewall_type, high_availability):
click.echo("Price: $%s monthly" % pkg[0]['prices'][0]['recurringFee'])
click.echo("******************")

if not formatting.confirm("This action will incur charges on your account. Continue?"):
raise exceptions.CLIAbort('Aborted.')
if not force:
if not formatting.confirm("This action will incur charges on your account. Continue?"):
raise exceptions.CLIAbort('Aborted.')

if firewall_type == 'vlan':
mgr.add_vlan_firewall(target, ha_enabled=high_availability)
Expand Down
11 changes: 6 additions & 5 deletions SoftLayer/CLI/firewall/cancel.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
@click.option('--firewall-type', required=True, show_default=True, default='vlan',
type=click.Choice(['vlan', 'server'], case_sensitive=False),
help='Firewall type.')
@click.option('-f', '--force', default=False, is_flag=True, help="Force cancel firewall of the server")
@environment.pass_env
def cli(env, identifier, firewall_type):
def cli(env, identifier, firewall_type, force):
"""Cancels a firewall."""

mgr = SoftLayer.FirewallManager(env.client)

if not (env.skip_confirmations or
formatting.confirm("This action will cancel a firewall from your account. Continue?")):
raise exceptions.CLIAbort('Aborted.')
if not force:
if not (env.skip_confirmations or
formatting.confirm("This action will cancel a firewall from your account. Continue?")):
raise exceptions.CLIAbort('Aborted.')

if firewall_type == 'server':
mgr.cancel_firewall(identifier, dedicated=False)
Expand Down
23 changes: 22 additions & 1 deletion tests/CLI/modules/firewall_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_list_firewalls(self):
@mock.patch('SoftLayer.CLI.formatting.confirm')
def test_add_vs(self, confirm_mock):
confirm_mock.return_value = True
result = self.run_command(['firewall', 'add', '1000', '--firewall-type=vlan', '-ha'])
result = self.run_command(['firewall', 'add', '1000', '--firewall-type=vlan', '-h'])
self.assert_no_fail(result)
self.assertIn("Firewall is being created!", result.output)

Expand Down Expand Up @@ -158,3 +158,24 @@ def test_edit(self, confirm_mock):
def test_monitoring(self):
result = self.run_command(['firewall', 'monitoring', '123456'])
print(result.output)

@mock.patch('SoftLayer.CLI.formatting.confirm')
def test_add_firewall_force(self, confirm_mock):
confirm_mock.return_value = False
result = self.run_command(['firewall', 'add', '1000', '--firewall-type=vlan', '-h', '--force'])
self.assert_no_fail(result)
self.assertIn("Firewall is being created!", result.output)

@mock.patch('SoftLayer.CLI.formatting.confirm')
def test_add_firewall_no_force(self, confirm_mock):
confirm_mock.return_value = False
result = self.run_command(['firewall', 'add', '1000', '--firewall-type=vlan', '-h'])
self.assertEqual(2, result.exit_code)

@mock.patch('SoftLayer.CLI.formatting.confirm')
def test_cancel_firewall_no_force(self, confirm_mock):
confirm_mock.return_value = False
result = self.run_command(['firewall', 'cancel', 'vlan:1234'])
self.assertEqual(2, result.exit_code)
print(result.output)
self.assertEqual('Aborted.', result.exception.message)

0 comments on commit b2a396e

Please sign in to comment.