Skip to content
Open
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
14 changes: 13 additions & 1 deletion SoftLayer/CLI/securitygroup/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
'interface',
'ipAddress', ]

REQUEST_COLUMNS = ['requestId']


@click.command()
@click.argument('securitygroup_id')
Expand Down Expand Up @@ -95,6 +97,11 @@ def add(env, securitygroup_id, network_component, server, interface):
if not success:
raise exceptions.CLIAbort("Could not attach network component")

table = formatting.Table(REQUEST_COLUMNS)
table.add_row([success['requestId']])

env.fout(table)


@click.command()
@click.argument('securitygroup_id')
Expand All @@ -118,6 +125,11 @@ def remove(env, securitygroup_id, network_component, server, interface):
if not success:
raise exceptions.CLIAbort("Could not detach network component")

table = formatting.Table(REQUEST_COLUMNS)
table.add_row([success['requestId']])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rename the return value from the detach call above. success['requestId'] is kinda confusing


env.fout(table)


def _validate_args(network_component, server, interface):
use_server = bool(server and interface and not network_component)
Expand Down Expand Up @@ -147,4 +159,4 @@ def _get_component_id(env, network_component, server, interface):
else:
component_id = network_component

return component_id
return component_id
27 changes: 25 additions & 2 deletions SoftLayer/CLI/securitygroup/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
'portRangeMax',
'protocol']

REQUEST_BOOL_COLUMNS = ['requestId', 'response']
REQUEST_RULES_COLUMNS = ['requestId', 'rules']


@click.command()
@click.argument('securitygroup_id')
Expand Down Expand Up @@ -85,6 +88,11 @@ def add(env, securitygroup_id, remote_ip, remote_group,
if not ret:
raise exceptions.CLIAbort("Failed to add security group rule")

table = formatting.Table(REQUEST_RULES_COLUMNS)
table.add_row([ret['requestId'], str(ret['rules'])])

env.fout(table)


@click.command()
@click.argument('securitygroup_id')
Expand Down Expand Up @@ -125,9 +133,16 @@ def edit(env, securitygroup_id, rule_id, remote_ip, remote_group,
if protocol:
data['protocol'] = protocol

if not mgr.edit_securitygroup_rule(securitygroup_id, rule_id, **data):
ret = mgr.edit_securitygroup_rule(securitygroup_id, rule_id, **data)

if not ret:
raise exceptions.CLIAbort("Failed to edit security group rule")

table = formatting.Table(REQUEST_BOOL_COLUMNS)
table.add_row([ret['requestId']])

env.fout(table)


@click.command()
@click.argument('securitygroup_id')
Expand All @@ -136,5 +151,13 @@ def edit(env, securitygroup_id, rule_id, remote_ip, remote_group,
def remove(env, securitygroup_id, rule_id):
"""Remove a rule from a security group."""
mgr = SoftLayer.NetworkManager(env.client)
if not mgr.remove_securitygroup_rule(securitygroup_id, rule_id):

ret = mgr.remove_securitygroup_rule(securitygroup_id, rule_id)

if not ret:
raise exceptions.CLIAbort("Failed to remove security group rule")

table = formatting.Table(REQUEST_BOOL_COLUMNS)
table.add_row([ret['requestId']])

env.fout(table)
17 changes: 12 additions & 5 deletions SoftLayer/fixtures/SoftLayer_Network_SecurityGroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@
'createDate': '2017-05-05T12:44:43-06:00'}
editObject = True
deleteObject = True
addRules = True
editRules = True
removeRules = True
attachNetworkComponents = True
detachNetworkComponents = True
addRules = {"requestId": "addRules",
"rules": "[{'direction': 'ingress', "
"'portRangeMax': '', "
"'portRangeMin': '', "
"'ethertype': 'IPv4', "
"'securityGroupId': 100, "
"'remoteGroupId': '', "
"'id': 100}]"}
editRules = {'requestId': 'editRules'}
removeRules = {'requestId': 'removeRules'}
attachNetworkComponents = {'requestId': 'interfaceAdd'}
detachNetworkComponents = {'requestId': 'interfaceRemove'}
20 changes: 19 additions & 1 deletion tests/CLI/modules/securitygroup_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,21 @@ def test_securitygroup_rule_add(self):
result = self.run_command(['sg', 'rule-add', '100',
'--direction=ingress'])

json.loads(result.output)

self.assert_no_fail(result)
self.assert_called_with('SoftLayer_Network_SecurityGroup', 'addRules',
identifier='100',
args=([{'direction': 'ingress'}],))

self.assertEqual([{"requestId": "addRules",
"rules": "[{'direction': 'ingress', "
"'portRangeMax': '', "
"'portRangeMin': '', "
"'ethertype': 'IPv4', "
"'securityGroupId': 100, 'remoteGroupId': '', "
"'id': 100}]"}], json.loads(result.output))

def test_securitygroup_rule_add_fail(self):
fixture = self.set_mock('SoftLayer_Network_SecurityGroup', 'addRules')
fixture.return_value = False
Expand All @@ -149,6 +159,8 @@ def test_securitygroup_rule_edit(self):
args=([{'id': '520',
'direction': 'ingress'}],))

self.assertEqual([{'requestId': 'editRules'}], json.loads(result.output))

def test_securitygroup_rule_edit_fail(self):
fixture = self.set_mock('SoftLayer_Network_SecurityGroup', 'editRules')
fixture.return_value = False
Expand All @@ -166,6 +178,8 @@ def test_securitygroup_rule_remove(self):
'removeRules', identifier='100',
args=(['520'],))

self.assertEqual([{'requestId': 'removeRules'}], json.loads(result.output))

def test_securitygroup_rule_remove_fail(self):
fixture = self.set_mock('SoftLayer_Network_SecurityGroup',
'removeRules')
Expand Down Expand Up @@ -203,6 +217,8 @@ def test_securitygroup_interface_add(self):
identifier='100',
args=(['1000'],))

self.assertEqual([{'requestId': 'interfaceAdd'}], json.loads(result.output))

def test_securitygroup_interface_add_fail(self):
fixture = self.set_mock('SoftLayer_Network_SecurityGroup',
'attachNetworkComponents')
Expand All @@ -223,6 +239,8 @@ def test_securitygroup_interface_remove(self):
identifier='100',
args=(['500'],))

self.assertEqual([{'requestId': 'interfaceRemove'}], json.loads(result.output))

def test_securitygroup_interface_remove_fail(self):
fixture = self.set_mock('SoftLayer_Network_SecurityGroup',
'detachNetworkComponents')
Expand All @@ -231,4 +249,4 @@ def test_securitygroup_interface_remove_fail(self):
result = self.run_command(['sg', 'interface-remove', '100',
'--network-component=500'])

self.assertEqual(result.exit_code, 2)
self.assertEqual(result.exit_code, 2)