Skip to content

Commit e68b5b8

Browse files
committed
Merge pull request #447 from Neetuj/issue420
adding import image
2 parents 0cbcf3f + 6941679 commit e68b5b8

File tree

5 files changed

+79
-2
lines changed

5 files changed

+79
-2
lines changed

SoftLayer/CLI/image/import.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""Import an image."""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import SoftLayer
5+
from SoftLayer.CLI import environment
6+
from SoftLayer.CLI import exceptions
7+
from SoftLayer.CLI import formatting
8+
9+
10+
import click
11+
12+
13+
@click.command()
14+
@click.argument('name')
15+
@click.argument('uri')
16+
@click.option('--note', default="",
17+
help="The note to be applied to the imported template")
18+
@click.option('--osrefcode', default="",
19+
help="The referenceCode of the operating system software"
20+
" description for the imported VHD")
21+
@environment.pass_env
22+
def cli(env, name, note, osrefcode, uri):
23+
"""Import an image."""
24+
25+
image_mgr = SoftLayer.ImageManager(env.client)
26+
data = {}
27+
output = []
28+
if name:
29+
data['name'] = name
30+
if note:
31+
data['note'] = note
32+
if osrefcode:
33+
data['operatingSystemReferenceCode'] = osrefcode
34+
if uri:
35+
data['uri'] = uri
36+
37+
# not sure if u should validate here or not
38+
# if uri.endswith(".vhd") and osrefcode == "":
39+
# raise exceptions.CLIAbort("Please specify osrefcode for .vhdImage")
40+
41+
result = image_mgr.import_image_from_uri(data)
42+
43+
if not result:
44+
raise exceptions.CLIAbort("Failed to import Image")
45+
46+
table = formatting.KeyValueTable(['name', 'value'])
47+
table.align['name'] = 'r'
48+
table.align['value'] = 'l'
49+
table.add_row(['name', result['name']])
50+
table.add_row(['id', result['id']])
51+
table.add_row(['created', result['createDate']])
52+
table.add_row(['guid', result['globalIdentifier']])
53+
output.append(table)
54+
return output

SoftLayer/CLI/routes.py

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
('image:detail', 'SoftLayer.CLI.image.detail:cli'),
7474
('image:edit', 'SoftLayer.CLI.image.edit:cli'),
7575
('image:list', 'SoftLayer.CLI.image.list:cli'),
76+
('image:import', 'SoftLayer.CLI.image.import:cli'),
7677

7778
('iscsi', 'SoftLayer.CLI.iscsi'),
7879
('iscsi:cancel', 'SoftLayer.CLI.iscsi.cancel:cli'),

SoftLayer/managers/image.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def get_image(self, image_id, **kwargs):
3838
def delete_image(self, image_id):
3939
"""Deletes the specified image.
4040
41-
:param int image: The ID of the image.
41+
:param int image_id: The ID of the image.
4242
"""
4343
self.vgbdtg.deleteObject(id=image_id)
4444

@@ -100,7 +100,7 @@ def _get_ids_from_name_private(self, name):
100100
def edit(self, image_id, name=None, note=None, tag=None):
101101
"""Edit image related details.
102102
103-
:param int image: The ID of the image
103+
:param int image_id: The ID of the image
104104
:param string name: Name of the Image.
105105
:param string note: Note of the image.
106106
:param string tag: Tags of the image to be updated to.
@@ -118,3 +118,8 @@ def edit(self, image_id, name=None, note=None, tag=None):
118118
return True
119119
else:
120120
return False
121+
122+
def import_image_from_uri(self, data):
123+
"""Import images which match the given uri."""
124+
result = self.vgbdtg.createFromExternalSource(data)
125+
return result

SoftLayer/testing/fixtures/SoftLayer_Virtual_Guest_Block_Device_Template_Group.py

+6
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@
2121
deleteObject = {}
2222
editObject = True
2323
setTags = True
24+
createFromExternalSource = [{
25+
'createDate': '2013-12-05T21:53:03-06:00',
26+
'globalIdentifier': '0B5DEAF4-643D-46CA-A695-CECBE8832C9D',
27+
'id': 100,
28+
'name': 'test_image',
29+
}]

SoftLayer/tests/managers/image_tests.py

+11
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,14 @@ def test_edit_full(self):
130130
self.assert_called_with(IMAGE_SERVICE, 'editObject',
131131
identifier=1,
132132
args=({'name': 'abc', 'note': 'xyz'},))
133+
134+
def test_import_image(self):
135+
# Test import image
136+
self.image.import_image_from_uri({'name': 'test_image',
137+
'note': 'testimage',
138+
'uri': 'invaliduri'})
139+
140+
self.assert_called_with(IMAGE_SERVICE, 'createFromExternalSource',
141+
args=({'name': 'test_image',
142+
'note': 'testimage',
143+
'uri': 'invaliduri'},))

0 commit comments

Comments
 (0)