Skip to content

Commit 107100f

Browse files
Merge pull request #1861 from edsonarios/issue1853
New command `slcli image share`
2 parents ca21eec + b691f21 commit 107100f

File tree

7 files changed

+60
-1
lines changed

7 files changed

+60
-1
lines changed

SoftLayer/CLI/image/share.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Share an image template with another account."""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import click
5+
6+
import SoftLayer
7+
from SoftLayer.CLI import environment
8+
from SoftLayer.CLI import helpers
9+
10+
11+
@click.command(cls=SoftLayer.CLI.command.SLCommand, )
12+
@click.argument('identifier')
13+
@click.option('--account-id', help='Account Id for another account to share image template', required=True)
14+
@environment.pass_env
15+
def cli(env, identifier, account_id):
16+
"""Share an image template with another account."""
17+
18+
image_mgr = SoftLayer.ImageManager(env.client)
19+
image_id = helpers.resolve_id(image_mgr.resolve_ids, identifier, 'image')
20+
shared_image = image_mgr.share_image(image_id, account_id)
21+
22+
if shared_image:
23+
env.fout("Image template {} was shared to account {}.".format(identifier, account_id))

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
('image:import', 'SoftLayer.CLI.image.import:cli'),
210210
('image:export', 'SoftLayer.CLI.image.export:cli'),
211211
('image:datacenter', 'SoftLayer.CLI.image.datacenter:cli'),
212+
('image:share', 'SoftLayer.CLI.image.share:cli'),
212213
('image:share-deny', 'SoftLayer.CLI.image.share_deny:cli'),
213214

214215
('ipsec', 'SoftLayer.CLI.vpn.ipsec'),

SoftLayer/fixtures/SoftLayer_Virtual_Guest_Block_Device_Template_Group.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@
5353
{'id': 265592, 'longName': 'Amsterdam 1', 'name': 'ams01', 'statusId': 2},
5454
{'id': 814994, 'longName': 'Amsterdam 3', 'name': 'ams03', 'statusId': 2},
5555
]
56+
permitSharingAccess = True
5657
denySharingAccess = True

SoftLayer/managers/image.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ def get_locations_list(self, image_id, location_names):
238238

239239
return locations_ids
240240

241+
def share_image(self, image_id, account_id):
242+
"""Permit sharing image template with another account.
243+
244+
:param int image_id: The ID of the image.
245+
:param int account_id: The ID of the another account to share the image.
246+
"""
247+
248+
return self.vgbdtg.permitSharingAccess(account_id, id=image_id)
249+
241250
def deny_share_image(self, image_id, account_id):
242251
"""Deny sharing image template with another account.
243252

docs/cli/image.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ Disk Image Commands
3131
:prog: image datacenter
3232
:show-nested:
3333

34+
.. click:: SoftLayer.CLI.image.share:cli
35+
:prog: image share
36+
:show-nested:
37+
3438
.. click:: SoftLayer.CLI.image.share_deny:cli
3539
:prog: image share-deny
36-
:show-nested:
40+
:show-nested:

tests/CLI/modules/image_tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ def test_datacenter_remove_fails(self):
5454
result = self.run_command(['image', 'datacenter', '100', '--remove'])
5555
self.assertEqual(2, result.exit_code)
5656

57+
def test_share(self):
58+
result = self.run_command(['image', 'share', '123456', '--account-id', '654321'])
59+
self.assert_no_fail(result)
60+
self.assertIn("Image template 123456 was shared to account 654321.", result.output)
61+
62+
def test_share_without_id(self):
63+
result = self.run_command(['image', 'share'])
64+
self.assertEqual(2, result.exit_code)
65+
self.assertIn("Error: Missing argument 'IDENTIFIER'.", result.output)
66+
67+
def test_share_without_id_account(self):
68+
result = self.run_command(['image', 'share', "123456"])
69+
self.assertEqual(2, result.exit_code)
70+
self.assertIn("Error: Missing option '--account-id'.", result.output)
71+
5772
def test_deny_share(self):
5873
result = self.run_command(['image', 'share-deny', '123456', '--account-id', '654321'])
5974
self.assert_no_fail(result)

tests/managers/image_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ def test_get_locations_id_fails(self):
224224
locations
225225
)
226226

227+
def test_share_image(self):
228+
result = self.image.share_image(image_id=123456, account_id=654321)
229+
230+
self.assert_called_with(IMAGE_SERVICE, 'permitSharingAccess', identifier=123456, args=(654321,))
231+
self.assertEqual(True, result)
232+
227233
def test_deny_share_image(self):
228234
result = self.image.deny_share_image(image_id=123456, account_id=654321)
229235

0 commit comments

Comments
 (0)