Repository: softlayer/softlayer-python
Pull request #1861 (2023-03-01 17:27:50)

New command `slcli image share`
Issue: #1853
```
softlayer-python/ (issue1853) $ slcli image share 57065 --account-id 1234
Image template 57065 was shared to account 1234.
```


Patch:
diff --git a/SoftLayer/CLI/image/share.py b/SoftLayer/CLI/image/share.py
new file mode 100644
index 00000000..e548bdbe
--- /dev/null
+++ b/SoftLayer/CLI/image/share.py
@@ -0,0 +1,23 @@
+"""Share an image template with another account."""
+# :license: MIT, see LICENSE for more details.
+
+import click
+
+import SoftLayer
+from SoftLayer.CLI import environment
+from SoftLayer.CLI import helpers
+
+
+@click.command(cls=SoftLayer.CLI.command.SLCommand, )
+@click.argument('identifier')
+@click.option('--account-id', help='Account Id for another account to share image template', required=True)
+@environment.pass_env
+def cli(env, identifier, account_id):
+    """Share an image template with another account."""
+
+    image_mgr = SoftLayer.ImageManager(env.client)
+    image_id = helpers.resolve_id(image_mgr.resolve_ids, identifier, 'image')
+    shared_image = image_mgr.share_image(image_id, account_id)
+
+    if shared_image:
+        env.fout("Image template {} was shared to account {}.".format(identifier, account_id))
diff --git a/SoftLayer/CLI/routes.py b/SoftLayer/CLI/routes.py
index b1cb139d..5022292b 100644
--- a/SoftLayer/CLI/routes.py
+++ b/SoftLayer/CLI/routes.py
@@ -209,6 +209,7 @@
     ('image:import', 'SoftLayer.CLI.image.import:cli'),
     ('image:export', 'SoftLayer.CLI.image.export:cli'),
     ('image:datacenter', 'SoftLayer.CLI.image.datacenter:cli'),
+    ('image:share', 'SoftLayer.CLI.image.share:cli'),
     ('image:share-deny', 'SoftLayer.CLI.image.share_deny:cli'),
 
     ('ipsec', 'SoftLayer.CLI.vpn.ipsec'),
diff --git a/SoftLayer/fixtures/SoftLayer_Virtual_Guest_Block_Device_Template_Group.py b/SoftLayer/fixtures/SoftLayer_Virtual_Guest_Block_Device_Template_Group.py
index 937bd93c..bc01a68a 100644
--- a/SoftLayer/fixtures/SoftLayer_Virtual_Guest_Block_Device_Template_Group.py
+++ b/SoftLayer/fixtures/SoftLayer_Virtual_Guest_Block_Device_Template_Group.py
@@ -53,4 +53,5 @@
     {'id': 265592, 'longName': 'Amsterdam 1', 'name': 'ams01', 'statusId': 2},
     {'id': 814994, 'longName': 'Amsterdam 3', 'name': 'ams03', 'statusId': 2},
 ]
+permitSharingAccess = True
 denySharingAccess = True
diff --git a/SoftLayer/managers/image.py b/SoftLayer/managers/image.py
index 6c1fb3a9..26043a8a 100644
--- a/SoftLayer/managers/image.py
+++ b/SoftLayer/managers/image.py
@@ -238,6 +238,15 @@ def get_locations_list(self, image_id, location_names):
 
         return locations_ids
 
+    def share_image(self, image_id, account_id):
+        """Permit sharing image template with another account.
+
+        :param int image_id: The ID of the image.
+        :param int account_id: The ID of the another account to share the image.
+        """
+
+        return self.vgbdtg.permitSharingAccess(account_id, id=image_id)
+
     def deny_share_image(self, image_id, account_id):
         """Deny sharing image template with another account.
 
diff --git a/docs/cli/image.rst b/docs/cli/image.rst
index dc4a24d7..9d36e98b 100644
--- a/docs/cli/image.rst
+++ b/docs/cli/image.rst
@@ -31,6 +31,10 @@ Disk Image Commands
     :prog: image datacenter
     :show-nested:
 
+.. click:: SoftLayer.CLI.image.share:cli
+    :prog: image share
+    :show-nested:
+
 .. click:: SoftLayer.CLI.image.share_deny:cli
     :prog: image share-deny
-    :show-nested:
\ No newline at end of file
+    :show-nested:


Test patch:
diff --git a/tests/CLI/modules/image_tests.py b/tests/CLI/modules/image_tests.py
index 515cdd1b..230b6bcf 100644
--- a/tests/CLI/modules/image_tests.py
+++ b/tests/CLI/modules/image_tests.py
@@ -54,6 +54,21 @@ def test_datacenter_remove_fails(self):
         result = self.run_command(['image', 'datacenter', '100', '--remove'])
         self.assertEqual(2, result.exit_code)
 
+    def test_share(self):
+        result = self.run_command(['image', 'share', '123456', '--account-id', '654321'])
+        self.assert_no_fail(result)
+        self.assertIn("Image template 123456 was shared to account 654321.", result.output)
+
+    def test_share_without_id(self):
+        result = self.run_command(['image', 'share'])
+        self.assertEqual(2, result.exit_code)
+        self.assertIn("Error: Missing argument 'IDENTIFIER'.", result.output)
+
+    def test_share_without_id_account(self):
+        result = self.run_command(['image', 'share', "123456"])
+        self.assertEqual(2, result.exit_code)
+        self.assertIn("Error: Missing option '--account-id'.", result.output)
+
     def test_deny_share(self):
         result = self.run_command(['image', 'share-deny', '123456', '--account-id', '654321'])
         self.assert_no_fail(result)
diff --git a/tests/managers/image_tests.py b/tests/managers/image_tests.py
index 31d7385d..debe1700 100644
--- a/tests/managers/image_tests.py
+++ b/tests/managers/image_tests.py
@@ -224,6 +224,12 @@ def test_get_locations_id_fails(self):
             locations
         )
 
+    def test_share_image(self):
+        result = self.image.share_image(image_id=123456, account_id=654321)
+
+        self.assert_called_with(IMAGE_SERVICE, 'permitSharingAccess', identifier=123456, args=(654321,))
+        self.assertEqual(True, result)
+
     def test_deny_share_image(self):
         result = self.image.deny_share_image(image_id=123456, account_id=654321)