Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SoftLayer/CLI/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@

('vlan', 'SoftLayer.CLI.vlan'),
('vlan:detail', 'SoftLayer.CLI.vlan.detail:cli'),
('vlan:edit', 'SoftLayer.CLI.vlan.edit:cli'),
('vlan:list', 'SoftLayer.CLI.vlan.list:cli'),

('summary', 'SoftLayer.CLI.summary:cli'),
Expand Down
37 changes: 37 additions & 0 deletions SoftLayer/CLI/vlan/edit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Edit a vlan's details."""
# :license: MIT, see LICENSE for more details.

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import helpers


@click.command()
@click.argument('identifier')
@click.option('--name', '-n',
help="The optional name for this VLAN")
@click.option('--note', '-e',
help="The note for this vlan.")
@click.option('--tags', '-g',
multiple=True,
help='Tags to set e.g. "tag1,tag2", or empty string to remove all'
)
@environment.pass_env
def cli(env, identifier, name, note, tags):
"""Edit a vlan's details."""

new_tags = None

if tags:
new_tags = ','.join(tags)

mgr = SoftLayer.NetworkManager(env.client)
vlan_id = helpers.resolve_id(mgr.resolve_vlan_ids, identifier, 'VLAN')
vlan = mgr.edit(vlan_id, name=name, note=note, tags=new_tags)

if vlan:
click.secho("Vlan edited successfully", fg='green')
else:
click.secho("Failed to edit the vlan", fg='red')
3 changes: 3 additions & 0 deletions SoftLayer/fixtures/SoftLayer_Network_Vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
'vlanNumber': 4444,
'firewallInterfaces': None
}

editObject = True
setTags = True
42 changes: 42 additions & 0 deletions SoftLayer/managers/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@
"""
import collections
import json
import logging

from SoftLayer.decoration import retry

from SoftLayer import exceptions
from SoftLayer import utils

from SoftLayer.managers import event_log

LOGGER = logging.getLogger(__name__)

# pylint: disable=too-many-public-methods

DEFAULT_SUBNET_MASK = ','.join(['hardware',
'datacenter',
'ipAddressCount',
Expand Down Expand Up @@ -685,3 +692,38 @@ def get_nas_credentials(self, identifier, **kwargs):
"""
result = self.network_storage.getObject(id=identifier, **kwargs)
return result

def edit(self, instance_id, name=None, note=None, tags=None):
"""Edit a vlan.

:param integer instance_id: the instance ID to edit.
:param string name: valid name.
:param string note: note about this particular vlan.
:param string tags: tags to set on the vlan as a comma separated list.
Use the empty string to remove all tags.
:returns: bool -- True or an Exception
"""

obj = {}

if tags is not None:
self.set_tags(tags, vlan_id=instance_id)

if name:
obj['name'] = name

if note:
obj['note'] = note

if not obj:
return True

return self.vlan.editObject(obj, id=instance_id)

@retry(logger=LOGGER)
def set_tags(self, tags, vlan_id):
"""Sets tags on a vlan with a retry decorator

Just calls vlan.setTags, but if it fails from an APIError will retry.
"""
self.vlan.setTags(tags, id=vlan_id)
4 changes: 4 additions & 0 deletions docs/cli/vlan.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ VLANs
:prog: vlan detail
:show-nested:

.. click:: SoftLayer.CLI.vlan.edit:cli
:prog: vlan edit
:show-nested:

.. click:: SoftLayer.CLI.vlan.list:cli
:prog: vlan list
:show-nested:
18 changes: 18 additions & 0 deletions tests/CLI/modules/vlan_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

:license: MIT, see LICENSE for more details.
"""
import mock

from SoftLayer import testing


Expand Down Expand Up @@ -76,3 +78,19 @@ def test_detail_hardware_without_hostname(self):
vlan_mock.return_value = getObject
result = self.run_command(['vlan', 'detail', '1234'])
self.assert_no_fail(result)

@mock.patch('SoftLayer.CLI.vlan.edit.click')
def test_vlan_edit(self, click):
result = self.run_command(['vlan', 'edit', '--name=nameTest', '--note=noteTest', '--tags=tag1,tag2', '100'])
click.secho.assert_called_with('Vlan edited successfully', fg='green')
self.assert_no_fail(result)
self.assert_called_with('SoftLayer_Network_Vlan', 'editObject', identifier=100)

@mock.patch('SoftLayer.CLI.vlan.edit.click')
def test_vlan_edit_failure(self, click):
mock = self.set_mock('SoftLayer_Network_Vlan', 'editObject')
mock.return_value = False
result = self.run_command(['vlan', 'edit', '--name=nameTest', '--note=noteTest', '--tags=tag1,tag2', '100'])
click.secho.assert_called_with('Failed to edit the vlan', fg='red')
self.assert_no_fail(result)
self.assert_called_with('SoftLayer_Network_Vlan', 'editObject', identifier=100)
9 changes: 9 additions & 0 deletions tests/managers/network_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,3 +625,12 @@ def test_get_cci_event_logs(self):
_filter = {'objectName': {'operation': 'CCI'}}
self.assert_called_with('SoftLayer_Event_Log', 'getAllObjects', filter=_filter)
self.assertEqual(100, log['accountId'])

def test_vlan_edit(self):
vlan_id = 100
name = "test"
note = "test note"
tags = "tag1,tag2"

self.network.edit(vlan_id, name, note, tags)
self.assert_called_with('SoftLayer_Network_Vlan', 'editObject')