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 @@ -293,6 +293,7 @@
('tags', 'SoftLayer.CLI.tags'),
('tags:list', 'SoftLayer.CLI.tags.list:cli'),
('tags:set', 'SoftLayer.CLI.tags.set:cli'),
('tags:details', 'SoftLayer.CLI.tags.details:cli'),

('ticket', 'SoftLayer.CLI.ticket'),
('ticket:create', 'SoftLayer.CLI.ticket.create:cli'),
Expand Down
24 changes: 24 additions & 0 deletions SoftLayer/CLI/tags/details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Details of a Tag."""
# :license: MIT, see LICENSE for more details.

import click

from SoftLayer.CLI import environment
from SoftLayer.CLI.tags.list import detailed_table
from SoftLayer.managers.tags import TagManager


@click.command()
@click.argument('identifier')
@environment.pass_env
def cli(env, identifier):
"""Get details for a Tag."""

tag_manager = TagManager(env.client)

if str.isdigit(identifier):
tags = [tag_manager.get_tag(identifier)]
else:
tags = tag_manager.get_tag_by_name(identifier)
table = detailed_table(tag_manager, tags)
env.fout(table)
6 changes: 3 additions & 3 deletions SoftLayer/CLI/tags/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def cli(env, detail):
tag_manager = TagManager(env.client)

if detail:
tables = detailed_table(tag_manager)
tables = detailed_table(tag_manager, tag_manager.get_attached_tags())
for table in tables:
env.fout(table)
else:
Expand All @@ -36,9 +36,8 @@ def tag_row(tag):
return [tag.get('id'), tag.get('name'), tag.get('referenceCount', 0)]


def detailed_table(tag_manager):
def detailed_table(tag_manager, tags):
"""Creates a table for each tag, with details about resources using it"""
tags = tag_manager.get_attached_tags()
tables = []
for tag in tags:
references = tag_manager.get_tag_references(tag.get('id'))
Expand Down Expand Up @@ -76,3 +75,4 @@ def get_resource_name(tag_manager, resource_id, tag_type):
except SoftLayerAPIError as exception:
resource_row = "{}".format(exception.reason)
return resource_row

4 changes: 4 additions & 0 deletions SoftLayer/fixtures/SoftLayer_Tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@
]

setTags = True

getObject = getAttachedTagsForCurrentUser[0]

getTagByTagName = getAttachedTagsForCurrentUser
22 changes: 22 additions & 0 deletions SoftLayer/managers/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ def get_tag_references(self, tag_id, mask=None):
mask = "mask[tagType]"
return self.client.call('SoftLayer_Tag', 'getReferences', id=tag_id, mask=mask, iter=True)

def get_tag(self, tag_id, mask=None):
"""Calls SoftLayer_Tag::getObject(id=tag_id)

:params int tag_id: Tag id to get object from
:params string mask: Mask to use.
"""
if mask is None:
mask = "mask[id,name]"
result = self.client.call('SoftLayer_Tag', 'getObject', id=tag_id, mask=mask)
return result

def get_tag_by_name(self, tag_name, mask=None):
"""Calls SoftLayer_Tag::getTagByTagName(tag_name)

:params string tag_name: Tag name to get object from
:params string mask: Mask to use.
"""
if mask is None:
mask = "mask[id,name]"
result = self.client.call('SoftLayer_Tag', 'getTagByTagName', tag_name, mask=mask)
return result

def reference_lookup(self, resource_table_id, tag_type):
"""Returns the SoftLayer Service for the corresponding type

Expand Down
8 changes: 8 additions & 0 deletions docs/cli/tags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ Tag Commands
.. click:: SoftLayer.CLI.tags.list:cli
:prog: tags list
:show-nested:

.. click:: SoftLayer.CLI.tags.set:cli
:prog: tags set
:show-nested:

.. click:: SoftLayer.CLI.tags.details:cli
:prog: tags details
:show-nested:
16 changes: 14 additions & 2 deletions tests/CLI/modules/tag_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_set_tags(self, click):
click.secho.assert_called_with('Set tags successfully', fg='green')
self.assert_no_fail(result)
self.assert_called_with('SoftLayer_Tag', 'setTags',
args=("tag1,tag2", "GUEST", 100),)
args=("tag1,tag2", "GUEST", 100), )

@mock.patch('SoftLayer.CLI.tags.set.click')
def test_set_tags_failure(self, click):
Expand All @@ -43,4 +43,16 @@ def test_set_tags_failure(self, click):
click.secho.assert_called_with('Failed to set tags', fg='red')
self.assert_no_fail(result)
self.assert_called_with('SoftLayer_Tag', 'setTags',
args=("tag1,tag2", "GUEST", 100),)
args=("tag1,tag2", "GUEST", 100), )

def test_details_by_name(self):
tag_name = 'bs_test_instance'
result = self.run_command(['tags', 'details', tag_name])
self.assert_no_fail(result)
self.assert_called_with('SoftLayer_Tag', 'getTagByTagName', args=(tag_name,))

def test_details_by_id(self):
tag_id = '1286571'
result = self.run_command(['tags', 'details', tag_id])
self.assert_no_fail(result)
self.assert_called_with('SoftLayer_Tag', 'getObject', identifier=tag_id)
26 changes: 26 additions & 0 deletions tests/managers/tag_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,29 @@ def test_set_tags(self):

self.tag_manager.set_tags(tags, key_name, resource_id)
self.assert_called_with('SoftLayer_Tag', 'setTags')

def test_get_tag(self):
tag_id = 1286571
result = self.tag_manager.get_tag(tag_id)
self.assertEqual(tag_id, result.get('id'))
self.assert_called_with('SoftLayer_Tag', 'getObject', identifier=tag_id)

def test_get_tag_mask(self):
tag_id = 1286571
result = self.tag_manager.get_tag(tag_id, mask=self.test_mask)
self.assertEqual(tag_id, result.get('id'))
self.assert_called_with('SoftLayer_Tag', 'getObject', identifier=tag_id, mask=self.test_mask)

def test_get_tag_by_name(self):
tag_name = 'bs_test_instance'
result = self.tag_manager.get_tag_by_name(tag_name)
args = (tag_name,)
self.assertEqual(tag_name, result[0].get('name'))
self.assert_called_with('SoftLayer_Tag', 'getTagByTagName', args=args)

def test_get_tag_by_name_mask(self):
tag_name = 'bs_test_instance'
result = self.tag_manager.get_tag_by_name(tag_name, mask=self.test_mask)
args = (tag_name,)
self.assertEqual(tag_name, result[0].get('name'))
self.assert_called_with('SoftLayer_Tag', 'getTagByTagName', mask=self.test_mask, args=args)