Skip to content

Commit cd6134e

Browse files
author
caberos
committed
fix the christopher code review
2 parents d2e3958 + d863b88 commit cd6134e

File tree

10 files changed

+142
-14
lines changed

10 files changed

+142
-14
lines changed

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@
323323

324324
('vlan', 'SoftLayer.CLI.vlan'),
325325
('vlan:detail', 'SoftLayer.CLI.vlan.detail:cli'),
326+
('vlan:edit', 'SoftLayer.CLI.vlan.edit:cli'),
326327
('vlan:list', 'SoftLayer.CLI.vlan.list:cli'),
327328

328329
('summary', 'SoftLayer.CLI.summary:cli'),

SoftLayer/CLI/subnet/detail.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def cli(env, identifier, no_vs, no_hardware):
2626
subnet_id = helpers.resolve_id(mgr.resolve_subnet_ids, identifier,
2727
name='subnet')
2828

29-
mask = 'mask[ipAddresses[id, ipAddress], datacenter, virtualGuests,hardware]'
29+
mask = 'mask[ipAddresses[id, ipAddress,note], datacenter, virtualGuests, hardware]'
3030

3131
subnet = mgr.get_subnet(subnet_id, mask=mask)
3232

@@ -50,9 +50,9 @@ def cli(env, identifier, no_vs, no_hardware):
5050

5151
ip_address = subnet.get('ipAddresses')
5252

53-
ip_table = formatting.KeyValueTable(['ipAddress', 'value'])
53+
ip_table = formatting.KeyValueTable(['id', 'ip', 'note'])
5454
for address in ip_address:
55-
ip_table.add_row([address.get('id'), address.get('ipAddress')])
55+
ip_table.add_row([address.get('id'), address.get('ipAddress'), address.get('note')])
5656

5757
table.add_row(['ipAddresses', ip_table])
5858

SoftLayer/CLI/subnet/edit_ip.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99

1010
@click.command()
1111
@click.argument('identifier')
12-
@click.option('--ip-address', required=True,
13-
help='Assume the ipAddress to set the note.')
1412
@click.option('--note', help="set ip address note of subnet")
1513
@environment.pass_env
16-
def cli(env, identifier, ip_address, note):
17-
"""Set the note of the ipAddress subnet"""
14+
def cli(env, identifier, note):
15+
"""Set the note of the ipAddress"""
1816

1917
data = {
2018
'note': note
2119
}
2220
mgr = SoftLayer.NetworkManager(env.client)
23-
ips = mgr.get_subnet(identifier, mask='id,ipAddresses[id,ipAddress]').get('ipAddresses')
24-
25-
for address in ips:
26-
if ip_address == address.get('ipAddress'):
27-
mgr.set_subnet_ipddress_note(address.get('id'), data)
21+
ip_id = None
22+
if str.isdigit(identifier):
23+
ip_id = identifier
24+
else:
25+
ip_object = mgr.get_ip_by_address(identifier)
26+
ip_id = ip_object.get('id')
27+
mgr.set_subnet_ipddress_note(ip_id, data)

SoftLayer/CLI/vlan/edit.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Edit a vlan's details."""
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()
12+
@click.argument('identifier')
13+
@click.option('--name', '-n',
14+
help="The optional name for this VLAN")
15+
@click.option('--note', '-e',
16+
help="The note for this vlan.")
17+
@click.option('--tags', '-g',
18+
multiple=True,
19+
help='Tags to set e.g. "tag1,tag2", or empty string to remove all'
20+
)
21+
@environment.pass_env
22+
def cli(env, identifier, name, note, tags):
23+
"""Edit a vlan's details."""
24+
25+
new_tags = None
26+
27+
if tags:
28+
new_tags = ','.join(tags)
29+
30+
mgr = SoftLayer.NetworkManager(env.client)
31+
vlan_id = helpers.resolve_id(mgr.resolve_vlan_ids, identifier, 'VLAN')
32+
vlan = mgr.edit(vlan_id, name=name, note=note, tags=new_tags)
33+
34+
if vlan:
35+
click.secho("Vlan edited successfully", fg='green')
36+
else:
37+
click.secho("Failed to edit the vlan", fg='red')

SoftLayer/fixtures/SoftLayer_Network_Vlan.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
'vlanNumber': 4444,
88
'firewallInterfaces': None
99
}
10+
11+
editObject = True
12+
setTags = True

SoftLayer/managers/network.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77
"""
88
import collections
99
import json
10+
import logging
11+
12+
from SoftLayer.decoration import retry
1013

1114
from SoftLayer import exceptions
1215
from SoftLayer import utils
1316

1417
from SoftLayer.managers import event_log
1518

19+
LOGGER = logging.getLogger(__name__)
20+
1621
# pylint: disable=too-many-public-methods
1722

1823
DEFAULT_SUBNET_MASK = ','.join(['hardware',
@@ -688,7 +693,53 @@ def get_nas_credentials(self, identifier, **kwargs):
688693
result = self.network_storage.getObject(id=identifier, **kwargs)
689694
return result
690695

696+
def edit(self, instance_id, name=None, note=None, tags=None):
697+
"""Edit a vlan.
698+
699+
:param integer instance_id: the instance ID to edit.
700+
:param string name: valid name.
701+
:param string note: note about this particular vlan.
702+
:param string tags: tags to set on the vlan as a comma separated list.
703+
Use the empty string to remove all tags.
704+
:returns: bool -- True or an Exception
705+
"""
706+
707+
obj = {}
708+
709+
if tags is not None:
710+
self.set_tags(tags, vlan_id=instance_id)
711+
712+
if name:
713+
obj['name'] = name
714+
715+
if note:
716+
obj['note'] = note
717+
718+
if not obj:
719+
return True
720+
721+
return self.vlan.editObject(obj, id=instance_id)
722+
723+
@retry(logger=LOGGER)
724+
def set_tags(self, tags, vlan_id):
725+
"""Sets tags on a vlan with a retry decorator
726+
727+
Just calls vlan.setTags, but if it fails from an APIError will retry.
728+
"""
729+
self.vlan.setTags(tags, id=vlan_id)
730+
731+
def get_ip_by_address(self, ip_address):
732+
"""get the ip address object
733+
734+
:param string ip_address: the ip address to edit.
735+
"""
736+
return self.client.call('SoftLayer_Network_Subnet_IpAddress', 'getByIpAddress', ip_address)
737+
691738
def set_subnet_ipddress_note(self, identifier, note):
692-
"""Set the ip address note of the subnet"""
739+
"""Set the ip address note of the subnet
740+
741+
:param integer identifier: the ip address ID to edit.
742+
:param json note: the note to edit.
743+
"""
693744
result = self.client.call('SoftLayer_Network_Subnet_IpAddress', 'editObject', note, id=identifier)
694745
return result

docs/cli/vlan.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ VLANs
77
:prog: vlan detail
88
:show-nested:
99

10+
.. click:: SoftLayer.CLI.vlan.edit:cli
11+
:prog: vlan edit
12+
:show-nested:
13+
1014
.. click:: SoftLayer.CLI.vlan.list:cli
1115
:prog: vlan list
1216
:show-nested:

tests/CLI/modules/subnet_tests.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ def test_create_subnet_static_ipv6(self, confirm_mock):
139139
self.assertEqual(output, json.loads(result.output))
140140

141141
def test_editrou_Ip(self):
142-
result = self.run_command(['subnet', 'edit-ip', '123456', '--ip-address=16.26.26.26', '--note=test'])
142+
result = self.run_command(['subnet', 'edit-ip', '16.26.26.26', '--note=test'])
143+
self.assert_no_fail(result)
144+
self.assertTrue(result)
145+
146+
def test_editrou_Id(self):
147+
result = self.run_command(['subnet', 'edit-ip', '123456', '--note=test'])
143148
self.assert_no_fail(result)
144149
self.assertTrue(result)

tests/CLI/modules/vlan_tests.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
55
:license: MIT, see LICENSE for more details.
66
"""
7+
import mock
8+
79
from SoftLayer import testing
810

911

@@ -76,3 +78,19 @@ def test_detail_hardware_without_hostname(self):
7678
vlan_mock.return_value = getObject
7779
result = self.run_command(['vlan', 'detail', '1234'])
7880
self.assert_no_fail(result)
81+
82+
@mock.patch('SoftLayer.CLI.vlan.edit.click')
83+
def test_vlan_edit(self, click):
84+
result = self.run_command(['vlan', 'edit', '--name=nameTest', '--note=noteTest', '--tags=tag1,tag2', '100'])
85+
click.secho.assert_called_with('Vlan edited successfully', fg='green')
86+
self.assert_no_fail(result)
87+
self.assert_called_with('SoftLayer_Network_Vlan', 'editObject', identifier=100)
88+
89+
@mock.patch('SoftLayer.CLI.vlan.edit.click')
90+
def test_vlan_edit_failure(self, click):
91+
mock = self.set_mock('SoftLayer_Network_Vlan', 'editObject')
92+
mock.return_value = False
93+
result = self.run_command(['vlan', 'edit', '--name=nameTest', '--note=noteTest', '--tags=tag1,tag2', '100'])
94+
click.secho.assert_called_with('Failed to edit the vlan', fg='red')
95+
self.assert_no_fail(result)
96+
self.assert_called_with('SoftLayer_Network_Vlan', 'editObject', identifier=100)

tests/managers/network_tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,3 +625,12 @@ def test_get_cci_event_logs(self):
625625
_filter = {'objectName': {'operation': 'CCI'}}
626626
self.assert_called_with('SoftLayer_Event_Log', 'getAllObjects', filter=_filter)
627627
self.assertEqual(100, log['accountId'])
628+
629+
def test_vlan_edit(self):
630+
vlan_id = 100
631+
name = "test"
632+
note = "test note"
633+
tags = "tag1,tag2"
634+
635+
self.network.edit(vlan_id, name, note, tags)
636+
self.assert_called_with('SoftLayer_Network_Vlan', 'editObject')

0 commit comments

Comments
 (0)