Skip to content

Commit d863b88

Browse files
Merge pull request #1285 from FernandoOjeda/fo_vlan_edit
Implement slcli vlan edit functionality.
2 parents 8e516be + 38e61a2 commit d863b88

File tree

7 files changed

+114
-0
lines changed

7 files changed

+114
-0
lines changed

SoftLayer/CLI/routes.py

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

323323
('vlan', 'SoftLayer.CLI.vlan'),
324324
('vlan:detail', 'SoftLayer.CLI.vlan.detail:cli'),
325+
('vlan:edit', 'SoftLayer.CLI.vlan.edit:cli'),
325326
('vlan:list', 'SoftLayer.CLI.vlan.list:cli'),
326327

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

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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@
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+
21+
# pylint: disable=too-many-public-methods
22+
1623
DEFAULT_SUBNET_MASK = ','.join(['hardware',
1724
'datacenter',
1825
'ipAddressCount',
@@ -685,3 +692,38 @@ def get_nas_credentials(self, identifier, **kwargs):
685692
"""
686693
result = self.network_storage.getObject(id=identifier, **kwargs)
687694
return result
695+
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)

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/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)