From 54f72c1be288040c07f2c337c9e1b331fafdbfd7 Mon Sep 17 00:00:00 2001 From: Anthony Fisher Date: Wed, 23 Aug 2017 14:59:59 -0500 Subject: [PATCH 1/6] security-groups-request-ids : Add output for RequestIDs The python api will now output a table with requestIDs for specific calls to security groups apis --- SoftLayer/CLI/securitygroup/interface.py | 12 ++++++++++ SoftLayer/CLI/securitygroup/rule.py | 28 ++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/SoftLayer/CLI/securitygroup/interface.py b/SoftLayer/CLI/securitygroup/interface.py index a2f33ee87..b08f7daa6 100644 --- a/SoftLayer/CLI/securitygroup/interface.py +++ b/SoftLayer/CLI/securitygroup/interface.py @@ -14,6 +14,8 @@ 'interface', 'ipAddress', ] +REQUEST_COLUMNS = ['requestId'] + @click.command() @click.argument('securitygroup_id') @@ -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['id']]) + + env.fout(table) + @click.command() @click.argument('securitygroup_id') @@ -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['id']]) + + env.fout(table) + def _validate_args(network_component, server, interface): use_server = bool(server and interface and not network_component) diff --git a/SoftLayer/CLI/securitygroup/rule.py b/SoftLayer/CLI/securitygroup/rule.py index dfbc93ed9..7a9a0fd8e 100644 --- a/SoftLayer/CLI/securitygroup/rule.py +++ b/SoftLayer/CLI/securitygroup/rule.py @@ -17,6 +17,8 @@ 'portRangeMax', 'protocol'] +REQUEST_COLUMNS = ['requestId'] + @click.command() @click.argument('securitygroup_id') @@ -85,6 +87,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_COLUMNS) + table.add_row([ret['id']]) + + env.fout(table) + @click.command() @click.argument('securitygroup_id') @@ -125,9 +132,18 @@ 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_COLUMNS) + table.add_row([ret['id']]) + + env.fout(table) + + + @click.command() @click.argument('securitygroup_id') @@ -136,5 +152,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_COLUMNS) + table.add_row([ret['id']]) + + env.fout(table) From 4c0badf5b13be7f9f2346e9dc2aef78a7df14067 Mon Sep 17 00:00:00 2001 From: Anthony Fisher Date: Fri, 25 Aug 2017 10:31:08 -0500 Subject: [PATCH 2/6] security-groups-request-ids : Add output for RequestIDs Fix unit tests Fix code style issues --- SoftLayer/CLI/securitygroup/rule.py | 2 -- SoftLayer/fixtures/SoftLayer_Network_SecurityGroup.py | 10 +++++----- tests/CLI/modules/securitygroup_tests.py | 10 ++++++++++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/SoftLayer/CLI/securitygroup/rule.py b/SoftLayer/CLI/securitygroup/rule.py index 7a9a0fd8e..2500bdc22 100644 --- a/SoftLayer/CLI/securitygroup/rule.py +++ b/SoftLayer/CLI/securitygroup/rule.py @@ -143,8 +143,6 @@ def edit(env, securitygroup_id, rule_id, remote_ip, remote_group, env.fout(table) - - @click.command() @click.argument('securitygroup_id') @click.argument('rule_id') diff --git a/SoftLayer/fixtures/SoftLayer_Network_SecurityGroup.py b/SoftLayer/fixtures/SoftLayer_Network_SecurityGroup.py index 7e79560f7..48cc05bdd 100644 --- a/SoftLayer/fixtures/SoftLayer_Network_SecurityGroup.py +++ b/SoftLayer/fixtures/SoftLayer_Network_SecurityGroup.py @@ -39,8 +39,8 @@ 'createDate': '2017-05-05T12:44:43-06:00'}] editObjects = True deleteObjects = True -addRules = True -editRules = True -removeRules = True -attachNetworkComponents = True -detachNetworkComponents = True +addRules = {'id': 'addRules'} +editRules = {'id': 'editRules'} +removeRules = {'id': 'removeRules'} +attachNetworkComponents = {'id': 'interfaceAdd'} +detachNetworkComponents = {'id': 'interfaceRemove'} diff --git a/tests/CLI/modules/securitygroup_tests.py b/tests/CLI/modules/securitygroup_tests.py index b234941d6..ba366dfab 100644 --- a/tests/CLI/modules/securitygroup_tests.py +++ b/tests/CLI/modules/securitygroup_tests.py @@ -129,6 +129,8 @@ def test_securitygroup_rule_add(self): identifier='100', args=([{'direction': 'ingress'}],)) + self.assertEqual([{'requestId': 'addRules'}], json.loads(result.output)) + def test_securitygroup_rule_add_fail(self): fixture = self.set_mock('SoftLayer_Network_SecurityGroup', 'addRules') fixture.return_value = False @@ -148,6 +150,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 @@ -165,6 +169,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') @@ -202,6 +208,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') @@ -222,6 +230,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') From 7768a83e2624cdd1bb91482c0c7d6ef77e7c8995 Mon Sep 17 00:00:00 2001 From: Anthony Fisher Date: Fri, 6 Oct 2017 15:20:58 -0500 Subject: [PATCH 3/6] security-groups-request-ids : Add output for RequestIDs Add new return format --- SoftLayer/CLI/securitygroup/rule.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/SoftLayer/CLI/securitygroup/rule.py b/SoftLayer/CLI/securitygroup/rule.py index 2500bdc22..609332258 100644 --- a/SoftLayer/CLI/securitygroup/rule.py +++ b/SoftLayer/CLI/securitygroup/rule.py @@ -17,7 +17,8 @@ 'portRangeMax', 'protocol'] -REQUEST_COLUMNS = ['requestId'] +REQUEST_BOOL_COLUMNS = ['requestId', 'response'] +REQUEST_RULES_COLUMNS = ['requestId', 'rules'] @click.command() @@ -84,11 +85,13 @@ def add(env, securitygroup_id, remote_ip, remote_group, direction, ethertype, port_max, port_min, protocol) + print ret + if not ret: raise exceptions.CLIAbort("Failed to add security group rule") - table = formatting.Table(REQUEST_COLUMNS) - table.add_row([ret['id']]) + table = formatting.Table(REQUEST_RULES_COLUMNS) + table.add_row([ret['requestId'], str(ret['rules'])]) env.fout(table) @@ -137,8 +140,9 @@ def edit(env, securitygroup_id, rule_id, remote_ip, remote_group, if not ret: raise exceptions.CLIAbort("Failed to edit security group rule") - table = formatting.Table(REQUEST_COLUMNS) - table.add_row([ret['id']]) + table = formatting.Table(REQUEST_BOOL_COLUMNS) + table.add_row([ret['requestId']]) + table.add_row([ret['response']]) env.fout(table) @@ -156,7 +160,8 @@ def remove(env, securitygroup_id, rule_id): if not ret: raise exceptions.CLIAbort("Failed to remove security group rule") - table = formatting.Table(REQUEST_COLUMNS) - table.add_row([ret['id']]) + table = formatting.Table(REQUEST_BOOL_COLUMNS) + table.add_row([ret['requestId']]) + table.add_row([ret['response']]) env.fout(table) From b6c5c27f969c56d29a9934d69c555d43ed0cdc78 Mon Sep 17 00:00:00 2001 From: fisherma91 <31142699+fisherma91@users.noreply.github.com> Date: Thu, 18 Jan 2018 10:22:56 -0600 Subject: [PATCH 4/6] Remove extra fields from table --- SoftLayer/CLI/securitygroup/rule.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/SoftLayer/CLI/securitygroup/rule.py b/SoftLayer/CLI/securitygroup/rule.py index 609332258..00d69be7c 100644 --- a/SoftLayer/CLI/securitygroup/rule.py +++ b/SoftLayer/CLI/securitygroup/rule.py @@ -142,7 +142,6 @@ def edit(env, securitygroup_id, rule_id, remote_ip, remote_group, table = formatting.Table(REQUEST_BOOL_COLUMNS) table.add_row([ret['requestId']]) - table.add_row([ret['response']]) env.fout(table) @@ -162,6 +161,5 @@ def remove(env, securitygroup_id, rule_id): table = formatting.Table(REQUEST_BOOL_COLUMNS) table.add_row([ret['requestId']]) - table.add_row([ret['response']]) env.fout(table) From 915e9b1aca44397ce0446a17acfa43e71208132f Mon Sep 17 00:00:00 2001 From: fisherma91 <31142699+fisherma91@users.noreply.github.com> Date: Thu, 18 Jan 2018 10:25:18 -0600 Subject: [PATCH 5/6] Fix Fixture --- .../SoftLayer_Network_SecurityGroup.py | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/SoftLayer/fixtures/SoftLayer_Network_SecurityGroup.py b/SoftLayer/fixtures/SoftLayer_Network_SecurityGroup.py index 22d4d0732..8d4b73283 100644 --- a/SoftLayer/fixtures/SoftLayer_Network_SecurityGroup.py +++ b/SoftLayer/fixtures/SoftLayer_Network_SecurityGroup.py @@ -33,20 +33,21 @@ 'rules': getRules } -createObjects = [{'id': 100, - 'name': 'secgroup1', - 'description': 'Securitygroup1', - 'createDate': '2017-05-05T12:44:43-06:00'}] createObject = {'id': 100, 'name': 'secgroup1', 'description': 'Securitygroup1', 'createDate': '2017-05-05T12:44:43-06:00'} -editObjects = True editObject = True -deleteObjects = True deleteObject = True -addRules = {'id': 'addRules'} -editRules = {'id': 'editRules'} -removeRules = {'id': 'removeRules'} -attachNetworkComponents = {'id': 'interfaceAdd'} -detachNetworkComponents = {'id': 'interfaceRemove'} +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'} From dd83da9953878fc2ec2bae54646707ff3f835979 Mon Sep 17 00:00:00 2001 From: Anthony Fisher Date: Thu, 18 Jan 2018 10:36:27 -0600 Subject: [PATCH 6/6] Fix unit tests --- SoftLayer/CLI/securitygroup/interface.py | 6 +++--- SoftLayer/CLI/securitygroup/rule.py | 2 -- tests/CLI/modules/securitygroup_tests.py | 12 ++++++++++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/SoftLayer/CLI/securitygroup/interface.py b/SoftLayer/CLI/securitygroup/interface.py index b08f7daa6..ecda177bb 100644 --- a/SoftLayer/CLI/securitygroup/interface.py +++ b/SoftLayer/CLI/securitygroup/interface.py @@ -98,7 +98,7 @@ def add(env, securitygroup_id, network_component, server, interface): raise exceptions.CLIAbort("Could not attach network component") table = formatting.Table(REQUEST_COLUMNS) - table.add_row([success['id']]) + table.add_row([success['requestId']]) env.fout(table) @@ -126,7 +126,7 @@ def remove(env, securitygroup_id, network_component, server, interface): raise exceptions.CLIAbort("Could not detach network component") table = formatting.Table(REQUEST_COLUMNS) - table.add_row([success['id']]) + table.add_row([success['requestId']]) env.fout(table) @@ -159,4 +159,4 @@ def _get_component_id(env, network_component, server, interface): else: component_id = network_component - return component_id + return component_id \ No newline at end of file diff --git a/SoftLayer/CLI/securitygroup/rule.py b/SoftLayer/CLI/securitygroup/rule.py index 00d69be7c..be9b4909d 100644 --- a/SoftLayer/CLI/securitygroup/rule.py +++ b/SoftLayer/CLI/securitygroup/rule.py @@ -85,8 +85,6 @@ def add(env, securitygroup_id, remote_ip, remote_group, direction, ethertype, port_max, port_min, protocol) - print ret - if not ret: raise exceptions.CLIAbort("Failed to add security group rule") diff --git a/tests/CLI/modules/securitygroup_tests.py b/tests/CLI/modules/securitygroup_tests.py index 886ffb3c9..43d9902e3 100644 --- a/tests/CLI/modules/securitygroup_tests.py +++ b/tests/CLI/modules/securitygroup_tests.py @@ -125,12 +125,20 @@ 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'}], json.loads(result.output)) + 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') @@ -241,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) \ No newline at end of file