From 4de733f533d900863ef6e66f2d9c861c78acfd74 Mon Sep 17 00:00:00 2001 From: ATGE Date: Fri, 19 Jun 2020 18:39:02 -0400 Subject: [PATCH 1/9] add tags and note to subnet detail --- SoftLayer/CLI/subnet/detail.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/SoftLayer/CLI/subnet/detail.py b/SoftLayer/CLI/subnet/detail.py index 1c8f7e2dc..2758838ee 100644 --- a/SoftLayer/CLI/subnet/detail.py +++ b/SoftLayer/CLI/subnet/detail.py @@ -44,6 +44,10 @@ def cli(env, identifier, no_vs, no_hardware): table.add_row(['datacenter', subnet['datacenter']['name']]) table.add_row(['usable ips', subnet.get('usableIpAddressCount', formatting.blank())]) + table.add_row(['note', + subnet.get('note', formatting.blank())]) + table.add_row(['tags', + formatting.tags(subnet.get('tagReferences'))]) if not no_vs: if subnet['virtualGuests']: @@ -55,7 +59,7 @@ def cli(env, identifier, no_vs, no_hardware): vsi.get('primaryBackendIpAddress')]) table.add_row(['vs', vs_table]) else: - table.add_row(['vs', 'none']) + table.add_row(['vs', formatting.blank()]) if not no_hardware: if subnet['hardware']: @@ -67,6 +71,6 @@ def cli(env, identifier, no_vs, no_hardware): hardware.get('primaryBackendIpAddress')]) table.add_row(['hardware', hw_table]) else: - table.add_row(['hardware', 'none']) + table.add_row(['hardware', formatting.blank()]) env.fout(table) From a8a50581528c64315e446481c445a647d8c81fa4 Mon Sep 17 00:00:00 2001 From: ATGE Date: Fri, 19 Jun 2020 18:41:55 -0400 Subject: [PATCH 2/9] add subnet edit --- SoftLayer/CLI/routes.py | 1 + SoftLayer/CLI/subnet/edit.py | 38 +++++++++++++++++++++++++++++++++++ SoftLayer/managers/network.py | 23 +++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 SoftLayer/CLI/subnet/edit.py diff --git a/SoftLayer/CLI/routes.py b/SoftLayer/CLI/routes.py index 604841144..e3aa99b0a 100644 --- a/SoftLayer/CLI/routes.py +++ b/SoftLayer/CLI/routes.py @@ -286,6 +286,7 @@ ('subnet', 'SoftLayer.CLI.subnet'), ('subnet:cancel', 'SoftLayer.CLI.subnet.cancel:cli'), ('subnet:create', 'SoftLayer.CLI.subnet.create:cli'), + ('subnet:edit', 'SoftLayer.CLI.subnet.edit:cli'), ('subnet:detail', 'SoftLayer.CLI.subnet.detail:cli'), ('subnet:list', 'SoftLayer.CLI.subnet.list:cli'), ('subnet:lookup', 'SoftLayer.CLI.subnet.lookup:cli'), diff --git a/SoftLayer/CLI/subnet/edit.py b/SoftLayer/CLI/subnet/edit.py new file mode 100644 index 000000000..57856c57b --- /dev/null +++ b/SoftLayer/CLI/subnet/edit.py @@ -0,0 +1,38 @@ +"""Edit a subnet.""" +# :license: MIT, see LICENSE for more details. + +import click + +import SoftLayer +from SoftLayer.CLI import environment +from SoftLayer.CLI import helpers + + +@click.command(short_help="Edit note and tags of a subnet") +@click.argument('identifier') +@click.option('--tags', '-t', type=click.STRING, + help='Comma separated list of tags, enclosed in quotes. "tag1, tag2"') +@click.option('--note', '-n', type=click.STRING, + help="The note") +@environment.pass_env +def cli(env, identifier, tags, note): + """Edit note and tags of a subnet.""" + + mgr = SoftLayer.NetworkManager(env.client) + subnet_id = helpers.resolve_id(mgr.resolve_subnet_ids, identifier, + name='subnet') + + if tags: + result = mgr.set_tags_subnet(subnet_id, tags) + print_result(result, "Set tags") + + if note: + result = mgr.edit_note_subnet(subnet_id, note) + print_result(result, "Edit note") + + +def print_result(result, detail): + if result: + click.secho("{} successfully".format(detail), fg='green') + else: + click.secho("Failed to {}".format(detail.lower()), fg='red') diff --git a/SoftLayer/managers/network.py b/SoftLayer/managers/network.py index b3f6f7b6d..596e4777c 100644 --- a/SoftLayer/managers/network.py +++ b/SoftLayer/managers/network.py @@ -24,6 +24,15 @@ 'datacenter', 'ipAddressCount', 'virtualGuests', + 'id', + 'networkIdentifier', + 'cidr', + 'subnetType', + 'gateway', + 'broadcastAddress', + 'usableIpAddressCount', + 'note', + 'tagReferences[tag]', 'networkVlan[id,networkSpace]']) DEFAULT_VLAN_MASK = ','.join([ 'firewallInterfaces', @@ -233,6 +242,20 @@ def cancel_subnet(self, subnet_id): billing_id = subnet['billingItem']['id'] return self.client['Billing_Item'].cancelService(id=billing_id) + def set_tags_subnet(self, subnet_id, tags): + """Tag a subnet by passing in one or more tags separated by a comma. + + :param int subnet_id: The ID of the subnet. + """ + return self.subnet.setTags(tags, id=subnet_id) + + def edit_note_subnet(self, subnet_id, note): + """Edit the note for this subnet. + + :param int subnet_id: The ID of the subnet. + """ + return self.subnet.editNote(note, id=subnet_id) + def create_securitygroup(self, name=None, description=None): """Creates a security group. From bc29ec3940511996958c6d9e30f99d8e186b1456 Mon Sep 17 00:00:00 2001 From: ATGE Date: Fri, 19 Jun 2020 18:43:09 -0400 Subject: [PATCH 3/9] add subnet edit tests --- .../fixtures/SoftLayer_Network_Subnet.py | 13 +++++- tests/CLI/modules/subnet_tests.py | 40 ++++++++++++++++++- tests/managers/network_tests.py | 20 ++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/SoftLayer/fixtures/SoftLayer_Network_Subnet.py b/SoftLayer/fixtures/SoftLayer_Network_Subnet.py index 7fc1e34dd..d8c20fbc0 100644 --- a/SoftLayer/fixtures/SoftLayer_Network_Subnet.py +++ b/SoftLayer/fixtures/SoftLayer_Network_Subnet.py @@ -25,5 +25,16 @@ } ], 'hardware': [], - 'usableIpAddressCount': 22 + 'usableIpAddressCount': 22, + 'note': 'test note', + 'tagReferences': [ + {'id': 1000123, + 'resourceTableId': 1234, + 'tag': {'id': 100123, + 'name': 'subnet: test tag'}, + } + ] } + +editNote = True +setTags = True diff --git a/tests/CLI/modules/subnet_tests.py b/tests/CLI/modules/subnet_tests.py index 1971aa420..493b30c83 100644 --- a/tests/CLI/modules/subnet_tests.py +++ b/tests/CLI/modules/subnet_tests.py @@ -36,8 +36,12 @@ def test_detail(self): 'private_ip': '10.0.1.2' } ], - 'hardware': 'none', - 'usable ips': 22 + 'hardware': None, + 'usable ips': 22, + 'note': 'test note', + 'tags': [ + 'subnet: test tag' + ], }, json.loads(result.output)) @@ -134,3 +138,35 @@ def test_create_subnet_static_ipv6(self, confirm_mock): ] self.assertEqual(output, json.loads(result.output)) + + @mock.patch('SoftLayer.CLI.subnet.edit.click') + def test_subnet_set_tags(self, click): + result = self.run_command(['subnet', 'edit', '1234', '--tags=tag1,tag2']) + click.secho.assert_called_with('Set tags successfully', fg='green') + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Network_Subnet', 'setTags', identifier=1234, args=("tag1,tag2",)) + + @mock.patch('SoftLayer.CLI.subnet.edit.click') + def test_subnet_edit_note(self, click): + result = self.run_command(['subnet', 'edit', '1234', '--note=test']) + click.secho.assert_called_with('Edit note successfully', fg='green') + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Network_Subnet', 'editNote', identifier=1234, args=("test",)) + + @mock.patch('SoftLayer.CLI.subnet.edit.click') + def test_subnet_set_tags_failure(self, click): + mock = self.set_mock('SoftLayer_Network_Subnet', 'setTags') + mock.return_value = False + result = self.run_command(['subnet', 'edit', '1234', '--tags=tag1,tag2']) + click.secho.assert_called_with('Failed to set tags', fg='red') + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Network_Subnet', 'setTags', identifier=1234, args=("tag1,tag2",)) + + @mock.patch('SoftLayer.CLI.subnet.edit.click') + def test_edit_note_failure(self, click): + mock = self.set_mock('SoftLayer_Network_Subnet', 'editNote') + mock.return_value = False + result = self.run_command(['subnet', 'edit', '1234', '--note=test']) + click.secho.assert_called_with('Failed to edit note', fg='red') + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Network_Subnet', 'editNote', identifier=1234, args=("test",)) diff --git a/tests/managers/network_tests.py b/tests/managers/network_tests.py index 165d04b71..57106ecee 100644 --- a/tests/managers/network_tests.py +++ b/tests/managers/network_tests.py @@ -158,6 +158,26 @@ def test_cancel_subnet(self): self.assert_called_with('SoftLayer_Billing_Item', 'cancelService', identifier=1056) + def test_set_tags_subnet(self): + subnet_id = 1234 + tags = 'tags1,tag2' + result = self.network.set_tags_subnet(subnet_id, tags) + + self.assertEqual(result, True) + self.assert_called_with('SoftLayer_Network_Subnet', 'setTags', + identifier=subnet_id, + args=(tags,)) + + def test_edit_note_subnet(self): + subnet_id = 1234 + note = 'test note' + result = self.network.edit_note_subnet(subnet_id, note) + + self.assertEqual(result, True) + self.assert_called_with('SoftLayer_Network_Subnet', 'editNote', + identifier=subnet_id, + args=(note,)) + def test_create_securitygroup(self): result = self.network.create_securitygroup(name='foo', description='bar') From 5e0068429f05e32ab47ce45cf3c8fe032e0c06f9 Mon Sep 17 00:00:00 2001 From: ATGE Date: Fri, 19 Jun 2020 18:44:08 -0400 Subject: [PATCH 4/9] add subnet edit docs --- docs/cli/subnet.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/cli/subnet.rst b/docs/cli/subnet.rst index 20fce0def..1ad3c379c 100644 --- a/docs/cli/subnet.rst +++ b/docs/cli/subnet.rst @@ -15,6 +15,10 @@ Subnets :prog: subnet detail :show-nested: +.. click:: SoftLayer.CLI.subnet.edit:cli + :prog: subnet edit + :show-nested: + .. click:: SoftLayer.CLI.subnet.list:cli :prog: subnet list :show-nested: From ca7cc9c76c8873ca491fb28d35406706f03fbf41 Mon Sep 17 00:00:00 2001 From: ATGE Date: Fri, 19 Jun 2020 19:07:07 -0400 Subject: [PATCH 5/9] fix tox analysis and conflicts --- SoftLayer/CLI/subnet/edit.py | 1 + 1 file changed, 1 insertion(+) diff --git a/SoftLayer/CLI/subnet/edit.py b/SoftLayer/CLI/subnet/edit.py index 57856c57b..9a62692b1 100644 --- a/SoftLayer/CLI/subnet/edit.py +++ b/SoftLayer/CLI/subnet/edit.py @@ -32,6 +32,7 @@ def cli(env, identifier, tags, note): def print_result(result, detail): + """Prints a successfully or Failed message.""" if result: click.secho("{} successfully".format(detail), fg='green') else: From ae8bba254781c1b3b5d3f2a766f37b536c766d07 Mon Sep 17 00:00:00 2001 From: ATGE Date: Mon, 22 Jun 2020 20:16:02 -0400 Subject: [PATCH 6/9] add docs param for set tag and edit note subnet --- SoftLayer/managers/network.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SoftLayer/managers/network.py b/SoftLayer/managers/network.py index 596e4777c..057977969 100644 --- a/SoftLayer/managers/network.py +++ b/SoftLayer/managers/network.py @@ -246,6 +246,7 @@ def set_tags_subnet(self, subnet_id, tags): """Tag a subnet by passing in one or more tags separated by a comma. :param int subnet_id: The ID of the subnet. + :param string tags: Comma separated list of tags. """ return self.subnet.setTags(tags, id=subnet_id) @@ -253,6 +254,7 @@ def edit_note_subnet(self, subnet_id, note): """Edit the note for this subnet. :param int subnet_id: The ID of the subnet. + :param string note: The note. """ return self.subnet.editNote(note, id=subnet_id) From b168484bac6c56288143d6c1cfbd09ec003a0667 Mon Sep 17 00:00:00 2001 From: Christopher Gallo Date: Thu, 25 Jun 2020 15:50:50 -0500 Subject: [PATCH 7/9] Update SoftLayer_Network_Subnet.py --- SoftLayer/fixtures/SoftLayer_Network_Subnet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SoftLayer/fixtures/SoftLayer_Network_Subnet.py b/SoftLayer/fixtures/SoftLayer_Network_Subnet.py index fd7973b49..24683da15 100644 --- a/SoftLayer/fixtures/SoftLayer_Network_Subnet.py +++ b/SoftLayer/fixtures/SoftLayer_Network_Subnet.py @@ -33,7 +33,7 @@ 'tag': {'id': 100123, 'name': 'subnet: test tag'}, } - ] + ], 'ipAddresses': [ {'id': 123456, 'ipAddress': '16.26.26.25'}, From 0f3803087b424ab4d5af81d9f6cfa89cd08d4151 Mon Sep 17 00:00:00 2001 From: Christopher Gallo Date: Thu, 25 Jun 2020 16:14:05 -0500 Subject: [PATCH 8/9] Update subnet_tests.py --- tests/CLI/modules/subnet_tests.py | 34 +++---------------------------- 1 file changed, 3 insertions(+), 31 deletions(-) diff --git a/tests/CLI/modules/subnet_tests.py b/tests/CLI/modules/subnet_tests.py index 486292abc..d31bede4e 100644 --- a/tests/CLI/modules/subnet_tests.py +++ b/tests/CLI/modules/subnet_tests.py @@ -17,38 +17,10 @@ class SubnetTests(testing.TestCase): def test_detail(self): result = self.run_command(['subnet', 'detail', '1234']) - + subnet = json.loads(result.output) self.assert_no_fail(result) - self.assertEqual( - { - 'id': 1234, - 'identifier': '1.2.3.4/26', - 'subnet type': 'ADDITIONAL_PRIMARY', - 'network space': 'PUBLIC', - 'gateway': '1.2.3.254', - 'broadcast': '1.2.3.255', - 'datacenter': 'dal10', - 'vs': [ - { - 'hostname': 'hostname0', - 'domain': 'sl.test', - 'public_ip': '1.2.3.10', - 'private_ip': '10.0.1.2' - } - ], - 'hardware': None, - 'usable ips': 22, - 'note': 'test note', - 'tags': [ - 'subnet: test tag' - ], - 'ipAddresses': { - '123456': '16.26.26.25', - '123457': '16.26.26.26'}, - 'hardware': 'none', - 'usable ips': 22 - }, - json.loads(result.output)) + self.assertEqual(subnet.get('id'), 1234) + self.assertEqual(subnet.get('identifier'),'1.2.3.4/26') def test_list(self): result = self.run_command(['subnet', 'list']) From 8ea87f7b53cb1fbeac9404e2159d9537ca7b9a2c Mon Sep 17 00:00:00 2001 From: Christopher Gallo Date: Thu, 25 Jun 2020 16:17:16 -0500 Subject: [PATCH 9/9] Update subnet_tests.py --- tests/CLI/modules/subnet_tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CLI/modules/subnet_tests.py b/tests/CLI/modules/subnet_tests.py index d31bede4e..52de2cd27 100644 --- a/tests/CLI/modules/subnet_tests.py +++ b/tests/CLI/modules/subnet_tests.py @@ -20,7 +20,7 @@ def test_detail(self): subnet = json.loads(result.output) self.assert_no_fail(result) self.assertEqual(subnet.get('id'), 1234) - self.assertEqual(subnet.get('identifier'),'1.2.3.4/26') + self.assertEqual(subnet.get('identifier'), '1.2.3.4/26') def test_list(self): result = self.run_command(['subnet', 'list'])