diff --git a/SoftLayer/CLI/routes.py b/SoftLayer/CLI/routes.py index 9304e2b33..a71b62439 100644 --- a/SoftLayer/CLI/routes.py +++ b/SoftLayer/CLI/routes.py @@ -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'), diff --git a/SoftLayer/CLI/tags/details.py b/SoftLayer/CLI/tags/details.py new file mode 100644 index 000000000..eb22bfada --- /dev/null +++ b/SoftLayer/CLI/tags/details.py @@ -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) diff --git a/SoftLayer/CLI/tags/list.py b/SoftLayer/CLI/tags/list.py index 870d5faf5..e2f136581 100644 --- a/SoftLayer/CLI/tags/list.py +++ b/SoftLayer/CLI/tags/list.py @@ -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: @@ -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')) @@ -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 + diff --git a/SoftLayer/fixtures/SoftLayer_Tag.py b/SoftLayer/fixtures/SoftLayer_Tag.py index ca0d952ef..6839f7398 100644 --- a/SoftLayer/fixtures/SoftLayer_Tag.py +++ b/SoftLayer/fixtures/SoftLayer_Tag.py @@ -16,3 +16,7 @@ ] setTags = True + +getObject = getAttachedTagsForCurrentUser[0] + +getTagByTagName = getAttachedTagsForCurrentUser diff --git a/SoftLayer/managers/tags.py b/SoftLayer/managers/tags.py index fa19d3d3f..e5b43a60f 100644 --- a/SoftLayer/managers/tags.py +++ b/SoftLayer/managers/tags.py @@ -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 diff --git a/docs/cli/tags.rst b/docs/cli/tags.rst index 5cca010f8..3aa9d75f9 100644 --- a/docs/cli/tags.rst +++ b/docs/cli/tags.rst @@ -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: \ No newline at end of file diff --git a/tests/CLI/modules/tag_tests.py b/tests/CLI/modules/tag_tests.py index 847511e80..f7eac8430 100644 --- a/tests/CLI/modules/tag_tests.py +++ b/tests/CLI/modules/tag_tests.py @@ -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): @@ -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) diff --git a/tests/managers/tag_tests.py b/tests/managers/tag_tests.py index f7ab940bb..e2dba99c9 100644 --- a/tests/managers/tag_tests.py +++ b/tests/managers/tag_tests.py @@ -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)