Repository: softlayer/softlayer-python
Pull request #1276 (2020-06-03 19:47:21)

Set tags.
Set tags https://github.com/softlayer/softlayer-python/issues/1274.


Patch:
diff --git a/SoftLayer/CLI/routes.py b/SoftLayer/CLI/routes.py
index 6c2bae94..9304e2b3 100644
--- a/SoftLayer/CLI/routes.py
+++ b/SoftLayer/CLI/routes.py
@@ -292,6 +292,7 @@
 
     ('tags', 'SoftLayer.CLI.tags'),
     ('tags:list', 'SoftLayer.CLI.tags.list:cli'),
+    ('tags:set', 'SoftLayer.CLI.tags.set:cli'),
 
     ('ticket', 'SoftLayer.CLI.ticket'),
     ('ticket:create', 'SoftLayer.CLI.ticket.create:cli'),
diff --git a/SoftLayer/CLI/tags/set.py b/SoftLayer/CLI/tags/set.py
new file mode 100644
index 00000000..aa0879c6
--- /dev/null
+++ b/SoftLayer/CLI/tags/set.py
@@ -0,0 +1,24 @@
+"""Set Tags."""
+# :license: MIT, see LICENSE for more details.
+
+import click
+
+from SoftLayer.CLI import environment
+from SoftLayer.managers.tags import TagManager
+
+
+@click.command()
+@click.option('--tags', '-t',  type=click.STRING, required=True, help='List of tags e.g. "tag1, tag2"')
+@click.option('--key-name', '-k', type=click.STRING, required=True, help="Key name of a tag type e.g. GUEST, HARDWARE")
+@click.option('--resource-id', '-r', type=click.INT, required=True, help="ID of the object being tagged")
+@environment.pass_env
+def cli(env, tags, key_name, resource_id):
+    """Set Tags."""
+
+    tag_manager = TagManager(env.client)
+    tags = tag_manager.set_tags(tags, key_name, resource_id)
+
+    if tags:
+        click.secho("Set tags successfully", fg='green')
+    else:
+        click.secho("Failed to set tags", fg='red')
diff --git a/SoftLayer/fixtures/SoftLayer_Tag.py b/SoftLayer/fixtures/SoftLayer_Tag.py
index 3246870c..ca0d952e 100644
--- a/SoftLayer/fixtures/SoftLayer_Tag.py
+++ b/SoftLayer/fixtures/SoftLayer_Tag.py
@@ -14,3 +14,5 @@
         'usrRecordId': 6625205
     }
 ]
+
+setTags = True
diff --git a/SoftLayer/managers/tags.py b/SoftLayer/managers/tags.py
index e22e9465..fa19d3d3 100644
--- a/SoftLayer/managers/tags.py
+++ b/SoftLayer/managers/tags.py
@@ -93,3 +93,12 @@ def reference_lookup(self, resource_table_id, tag_type):
 
         # return {}
         return self.client.call(service, 'getObject', id=resource_table_id)
+
+    def set_tags(self, tags, key_name, resource_id):
+        """Calls SoftLayer_Tag::setTags()
+
+        :param string tags: List of tags.
+        :param string key_name: Key name of a tag type.
+        :param int resource_id: ID of the object being tagged.
+        """
+        return self.client.call('SoftLayer_Tag', 'setTags', tags, key_name, resource_id)


Test patch:
diff --git a/tests/CLI/modules/tag_tests.py b/tests/CLI/modules/tag_tests.py
index 35736406..847511e8 100644
--- a/tests/CLI/modules/tag_tests.py
+++ b/tests/CLI/modules/tag_tests.py
@@ -4,6 +4,8 @@
 
     Tests for the user cli command
 """
+import mock
+
 from SoftLayer import testing
 
 
@@ -24,3 +26,21 @@ def test_list_detail(self):
         self.assert_called_with('SoftLayer_Tag', 'getAttachedTagsForCurrentUser')
         self.assert_called_with('SoftLayer_Tag', 'getReferences', identifier=1286571)
         self.assert_called_with('SoftLayer_Virtual_Guest', 'getObject', identifier=33488921)
+
+    @mock.patch('SoftLayer.CLI.tags.set.click')
+    def test_set_tags(self, click):
+        result = self.run_command(['tags', 'set', '--tags=tag1,tag2', '--key-name=GUEST', '--resource-id=100'])
+        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),)
+
+    @mock.patch('SoftLayer.CLI.tags.set.click')
+    def test_set_tags_failure(self, click):
+        mock = self.set_mock('SoftLayer_Tag', 'setTags')
+        mock.return_value = False
+        result = self.run_command(['tags', 'set', '--tags=tag1,tag2', '--key-name=GUEST', '--resource-id=100'])
+        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),)
diff --git a/tests/managers/tag_tests.py b/tests/managers/tag_tests.py
index 38f198ae..f7ab940b 100644
--- a/tests/managers/tag_tests.py
+++ b/tests/managers/tag_tests.py
@@ -103,3 +103,11 @@ def test_reference_lookup_document(self):
         )
         self.assertEqual(exception.faultCode, 404)
         self.assertEqual(exception.reason, "Unable to lookup ACCOUNT_DOCUMENT types")
+
+    def test_set_tags(self):
+        tags = "tag1,tag2"
+        key_name = "GUEST"
+        resource_id = 100
+
+        self.tag_manager.set_tags(tags, key_name, resource_id)
+        self.assert_called_with('SoftLayer_Tag', 'setTags')