Skip to content

Commit 5a0a8f3

Browse files
Merge pull request #1286 from ATGE/issue1283
Subnet Editing (tagging) #1283
2 parents d2c5018 + 8ea87f7 commit 5a0a8f3

File tree

8 files changed

+141
-27
lines changed

8 files changed

+141
-27
lines changed

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@
286286
('subnet', 'SoftLayer.CLI.subnet'),
287287
('subnet:cancel', 'SoftLayer.CLI.subnet.cancel:cli'),
288288
('subnet:create', 'SoftLayer.CLI.subnet.create:cli'),
289+
('subnet:edit', 'SoftLayer.CLI.subnet.edit:cli'),
289290
('subnet:detail', 'SoftLayer.CLI.subnet.detail:cli'),
290291
('subnet:list', 'SoftLayer.CLI.subnet.list:cli'),
291292
('subnet:lookup', 'SoftLayer.CLI.subnet.lookup:cli'),

SoftLayer/CLI/subnet/detail.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def cli(env, identifier, no_vs, no_hardware):
4747
table.add_row(['datacenter', subnet['datacenter']['name']])
4848
table.add_row(['usable ips',
4949
subnet.get('usableIpAddressCount', formatting.blank())])
50+
table.add_row(['note',
51+
subnet.get('note', formatting.blank())])
52+
table.add_row(['tags',
53+
formatting.tags(subnet.get('tagReferences'))])
5054

5155
ip_address = subnet.get('ipAddresses')
5256

@@ -66,7 +70,7 @@ def cli(env, identifier, no_vs, no_hardware):
6670
vsi.get('primaryBackendIpAddress')])
6771
table.add_row(['vs', vs_table])
6872
else:
69-
table.add_row(['vs', 'none'])
73+
table.add_row(['vs', formatting.blank()])
7074

7175
if not no_hardware:
7276
if subnet['hardware']:
@@ -78,6 +82,6 @@ def cli(env, identifier, no_vs, no_hardware):
7882
hardware.get('primaryBackendIpAddress')])
7983
table.add_row(['hardware', hw_table])
8084
else:
81-
table.add_row(['hardware', 'none'])
85+
table.add_row(['hardware', formatting.blank()])
8286

8387
env.fout(table)

SoftLayer/CLI/subnet/edit.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Edit a subnet."""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import click
5+
6+
import SoftLayer
7+
from SoftLayer.CLI import environment
8+
from SoftLayer.CLI import helpers
9+
10+
11+
@click.command(short_help="Edit note and tags of a subnet")
12+
@click.argument('identifier')
13+
@click.option('--tags', '-t', type=click.STRING,
14+
help='Comma separated list of tags, enclosed in quotes. "tag1, tag2"')
15+
@click.option('--note', '-n', type=click.STRING,
16+
help="The note")
17+
@environment.pass_env
18+
def cli(env, identifier, tags, note):
19+
"""Edit note and tags of a subnet."""
20+
21+
mgr = SoftLayer.NetworkManager(env.client)
22+
subnet_id = helpers.resolve_id(mgr.resolve_subnet_ids, identifier,
23+
name='subnet')
24+
25+
if tags:
26+
result = mgr.set_tags_subnet(subnet_id, tags)
27+
print_result(result, "Set tags")
28+
29+
if note:
30+
result = mgr.edit_note_subnet(subnet_id, note)
31+
print_result(result, "Edit note")
32+
33+
34+
def print_result(result, detail):
35+
"""Prints a successfully or Failed message."""
36+
if result:
37+
click.secho("{} successfully".format(detail), fg='green')
38+
else:
39+
click.secho("Failed to {}".format(detail.lower()), fg='red')

SoftLayer/fixtures/SoftLayer_Network_Subnet.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,20 @@
2626
],
2727
'hardware': [],
2828
'usableIpAddressCount': 22,
29+
'note': 'test note',
30+
'tagReferences': [
31+
{'id': 1000123,
32+
'resourceTableId': 1234,
33+
'tag': {'id': 100123,
34+
'name': 'subnet: test tag'},
35+
}
36+
],
2937
'ipAddresses': [
3038
{'id': 123456,
3139
'ipAddress': '16.26.26.25'},
3240
{'id': 123457,
3341
'ipAddress': '16.26.26.26'}]
3442
}
43+
44+
editNote = True
45+
setTags = True

SoftLayer/managers/network.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@
2424
'datacenter',
2525
'ipAddressCount',
2626
'virtualGuests',
27+
'id',
28+
'networkIdentifier',
29+
'cidr',
30+
'subnetType',
31+
'gateway',
32+
'broadcastAddress',
33+
'usableIpAddressCount',
34+
'note',
35+
'tagReferences[tag]',
2736
'networkVlan[id,networkSpace]'])
2837
DEFAULT_VLAN_MASK = ','.join([
2938
'firewallInterfaces',
@@ -233,6 +242,22 @@ def cancel_subnet(self, subnet_id):
233242
billing_id = subnet['billingItem']['id']
234243
return self.client['Billing_Item'].cancelService(id=billing_id)
235244

245+
def set_tags_subnet(self, subnet_id, tags):
246+
"""Tag a subnet by passing in one or more tags separated by a comma.
247+
248+
:param int subnet_id: The ID of the subnet.
249+
:param string tags: Comma separated list of tags.
250+
"""
251+
return self.subnet.setTags(tags, id=subnet_id)
252+
253+
def edit_note_subnet(self, subnet_id, note):
254+
"""Edit the note for this subnet.
255+
256+
:param int subnet_id: The ID of the subnet.
257+
:param string note: The note.
258+
"""
259+
return self.subnet.editNote(note, id=subnet_id)
260+
236261
def create_securitygroup(self, name=None, description=None):
237262
"""Creates a security group.
238263

docs/cli/subnet.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Subnets
1515
:prog: subnet detail
1616
:show-nested:
1717

18+
.. click:: SoftLayer.CLI.subnet.edit:cli
19+
:prog: subnet edit
20+
:show-nested:
21+
1822
.. click:: SoftLayer.CLI.subnet.list:cli
1923
:prog: subnet list
2024
:show-nested:

tests/CLI/modules/subnet_tests.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,10 @@ class SubnetTests(testing.TestCase):
1717

1818
def test_detail(self):
1919
result = self.run_command(['subnet', 'detail', '1234'])
20-
20+
subnet = json.loads(result.output)
2121
self.assert_no_fail(result)
22-
self.assertEqual(
23-
{
24-
'id': 1234,
25-
'identifier': '1.2.3.4/26',
26-
'subnet type': 'ADDITIONAL_PRIMARY',
27-
'network space': 'PUBLIC',
28-
'gateway': '1.2.3.254',
29-
'broadcast': '1.2.3.255',
30-
'datacenter': 'dal10',
31-
'vs': [
32-
{
33-
'hostname': 'hostname0',
34-
'domain': 'sl.test',
35-
'public_ip': '1.2.3.10',
36-
'private_ip': '10.0.1.2'
37-
}
38-
],
39-
'ipAddresses': {
40-
'123456': '16.26.26.25',
41-
'123457': '16.26.26.26'},
42-
'hardware': 'none',
43-
'usable ips': 22
44-
},
45-
json.loads(result.output))
22+
self.assertEqual(subnet.get('id'), 1234)
23+
self.assertEqual(subnet.get('identifier'), '1.2.3.4/26')
4624

4725
def test_list(self):
4826
result = self.run_command(['subnet', 'list'])
@@ -138,6 +116,38 @@ def test_create_subnet_static_ipv6(self, confirm_mock):
138116

139117
self.assertEqual(output, json.loads(result.output))
140118

119+
@mock.patch('SoftLayer.CLI.subnet.edit.click')
120+
def test_subnet_set_tags(self, click):
121+
result = self.run_command(['subnet', 'edit', '1234', '--tags=tag1,tag2'])
122+
click.secho.assert_called_with('Set tags successfully', fg='green')
123+
self.assert_no_fail(result)
124+
self.assert_called_with('SoftLayer_Network_Subnet', 'setTags', identifier=1234, args=("tag1,tag2",))
125+
126+
@mock.patch('SoftLayer.CLI.subnet.edit.click')
127+
def test_subnet_edit_note(self, click):
128+
result = self.run_command(['subnet', 'edit', '1234', '--note=test'])
129+
click.secho.assert_called_with('Edit note successfully', fg='green')
130+
self.assert_no_fail(result)
131+
self.assert_called_with('SoftLayer_Network_Subnet', 'editNote', identifier=1234, args=("test",))
132+
133+
@mock.patch('SoftLayer.CLI.subnet.edit.click')
134+
def test_subnet_set_tags_failure(self, click):
135+
mock = self.set_mock('SoftLayer_Network_Subnet', 'setTags')
136+
mock.return_value = False
137+
result = self.run_command(['subnet', 'edit', '1234', '--tags=tag1,tag2'])
138+
click.secho.assert_called_with('Failed to set tags', fg='red')
139+
self.assert_no_fail(result)
140+
self.assert_called_with('SoftLayer_Network_Subnet', 'setTags', identifier=1234, args=("tag1,tag2",))
141+
142+
@mock.patch('SoftLayer.CLI.subnet.edit.click')
143+
def test_edit_note_failure(self, click):
144+
mock = self.set_mock('SoftLayer_Network_Subnet', 'editNote')
145+
mock.return_value = False
146+
result = self.run_command(['subnet', 'edit', '1234', '--note=test'])
147+
click.secho.assert_called_with('Failed to edit note', fg='red')
148+
self.assert_no_fail(result)
149+
self.assert_called_with('SoftLayer_Network_Subnet', 'editNote', identifier=1234, args=("test",))
150+
141151
def test_editrou_Ip(self):
142152
result = self.run_command(['subnet', 'edit-ip', '16.26.26.26', '--note=test'])
143153
self.assert_no_fail(result)

tests/managers/network_tests.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,26 @@ def test_cancel_subnet(self):
158158
self.assert_called_with('SoftLayer_Billing_Item', 'cancelService',
159159
identifier=1056)
160160

161+
def test_set_tags_subnet(self):
162+
subnet_id = 1234
163+
tags = 'tags1,tag2'
164+
result = self.network.set_tags_subnet(subnet_id, tags)
165+
166+
self.assertEqual(result, True)
167+
self.assert_called_with('SoftLayer_Network_Subnet', 'setTags',
168+
identifier=subnet_id,
169+
args=(tags,))
170+
171+
def test_edit_note_subnet(self):
172+
subnet_id = 1234
173+
note = 'test note'
174+
result = self.network.edit_note_subnet(subnet_id, note)
175+
176+
self.assertEqual(result, True)
177+
self.assert_called_with('SoftLayer_Network_Subnet', 'editNote',
178+
identifier=subnet_id,
179+
args=(note,))
180+
161181
def test_create_securitygroup(self):
162182
result = self.network.create_securitygroup(name='foo',
163183
description='bar')

0 commit comments

Comments
 (0)