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

Fixes firewall cancel so it can properly do multiVlan firewalls #1822

Merged
merged 3 commits into from
Jan 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion SoftLayer/CLI/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@


class OptionHighlighter(RegexHighlighter):
"""Provides highlighter regex for the Command help"""
"""Provides highlighter regex for the Command help.

Defined in SoftLayer\\utils.py console_color_themes()
"""
highlights = [
r"(?P<switch>^\-\w)", # single options like -v
r"(?P<option>\-\-[\w\-]+)", # long options like --verbose
r"(?P<default_option>\[[^\]]+\])", # anything between [], usually default options
r"(?P<option_choices>Choices: )",
]


Expand Down Expand Up @@ -223,6 +227,11 @@ def format_options(self, ctx, formatter):
if help_record:
help_message = param.get_help_record(ctx)[-1]

# Add Click choices to help message
if isinstance(param.type, click.Choice):
choices = ", ".join(param.type.choices)
help_message += f" Choices: {choices}"

if param.metavar:
options += f" {param.metavar}"
options_table.add_row(opt1, opt2, self.highlighter(help_message))
Expand Down
18 changes: 8 additions & 10 deletions SoftLayer/CLI/firewall/cancel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,27 @@
import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import exceptions
from SoftLayer.CLI import firewall
from SoftLayer.CLI import formatting


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@click.option('--firewall-type', required=True, show_default=True, default='vlan',
type=click.Choice(['vlan', 'server'], case_sensitive=False),
help='Firewall type.')
@environment.pass_env
def cli(env, identifier):
def cli(env, identifier, firewall_type):
"""Cancels a firewall."""

mgr = SoftLayer.FirewallManager(env.client)
firewall_type, firewall_id = firewall.parse_id(identifier)

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

if firewall_type in ['vs', 'server']:
mgr.cancel_firewall(firewall_id, dedicated=False)
elif firewall_type == 'vlan':
mgr.cancel_firewall(firewall_id, dedicated=True)
if firewall_type == 'server':
mgr.cancel_firewall(identifier, dedicated=False)
else:
raise exceptions.CLIAbort('Unknown firewall type: %s' % firewall_type)
mgr.cancel_firewall(identifier, dedicated=True)

env.fout('Firewall with id %s is being cancelled!' % identifier)
6 changes: 2 additions & 4 deletions SoftLayer/managers/firewall.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,9 @@ def _get_fwl_billing_item(self, firewall_id, dedicated=False):
firewall_service = self.client['Network_Component_Firewall']
firewall = firewall_service.getObject(id=firewall_id, mask=mask)
if firewall is None:
raise exceptions.SoftLayerError(
"Unable to find firewall %d" % firewall_id)
raise exceptions.SoftLayerError(f"Unable to find firewall {firewall_id}")
if firewall.get('billingItem') is None:
raise exceptions.SoftLayerError(
"Unable to find billing item for firewall %d" % firewall_id)
raise exceptions.SoftLayerError(f"Unable to find billing item for firewall {firewall_id}")

return firewall['billingItem']

Expand Down
2 changes: 2 additions & 0 deletions SoftLayer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ def console_color_themes(theme):
"default_option": "light_coral",
"option_keyword": "bold dark_cyan",
"args_keyword": "bold green4",
"option_choices": "gold3",
})
)
return Console(theme=Theme(
Expand All @@ -497,6 +498,7 @@ def console_color_themes(theme):
"default_option": "light_pink1",
"option_keyword": "bold cyan",
"args_keyword": "bold green",
"option_choices": "gold3",
})
)

Expand Down