Skip to content

Commit a9a2f4a

Browse files
author
Elias Nygren
committed
server from template
1 parent e9b7c86 commit a9a2f4a

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

test/test_server_creation.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,37 @@ def test_create_server_with_dict(self, manager):
192192
assert server1.video_model == 'cirrus'
193193
assert server1.vnc == 'off'
194194
assert server1.vnc_password == 'aabbccdd'
195+
196+
197+
@responses.activate
198+
def test_create_server_from_template(self, manager):
199+
200+
UUID = '01215a5a-c330-4565-81ca-0e0e22eac672'
201+
202+
def _from_template_callback(request):
203+
request_body = json.loads(request.body)
204+
storage = request_body['server']['storage_devices']['storage_device'][0]
205+
206+
# https://www.upcloud.com/api/8-servers/#creating-from-a-template
207+
assert storage['action'] == 'clone'
208+
assert storage['storage'] == UUID
209+
return (201, {}, Mock.read_from_file('server_create.json'))
210+
211+
responses.add_callback(
212+
responses.POST,
213+
Mock.base_url + '/server',
214+
content_type='application/json',
215+
callback=_from_template_callback
216+
)
217+
218+
manager.create_server(
219+
Server(
220+
core_number=2,
221+
memory_amount=1024,
222+
hostname='my.example.com',
223+
zone=ZONE.Chicago,
224+
storage_devices=[
225+
Storage(storage=UUID, size=10),
226+
]
227+
)
228+
)

upcloud_api/server.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,19 +332,27 @@ def prepare_post_body(self):
332332
storage_title_id += 1
333333
storage_body = storage.to_dict()
334334

335-
# setup default titles for storages unless use has specified them
336-
# at storage.title
335+
# setup default titles for storages unless the user has specified
336+
# them at storage.title
337337
if not hasattr(storage, 'title') or not storage.title:
338338
if hasattr(storage, 'os') and storage.os:
339339
storage_body['title'] = self.hostname + ' OS disk'
340340
else:
341341
storage_body['title'] = self.hostname + ' storage disk ' + str(storage_title_id)
342342

343343

344-
# clone from public template OR create empty storage
344+
# figure out the storage `action` parameter
345+
# public template
345346
if hasattr(storage, 'os') and storage.os:
346347
storage_body['action'] = 'clone'
347348
storage_body['storage'] = OperatingSystems.get_OS_UUID(storage.os)
349+
350+
# private template
351+
elif hasattr(storage, 'uuid'):
352+
storage_body['action'] = 'clone'
353+
storage_body['storage'] = storage.uuid
354+
355+
# create a new storage
348356
else:
349357
storage_body['action'] = 'create'
350358

upcloud_api/storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _reset(self, **kwargs):
3636

3737
if 'uuid' in kwargs:
3838
self.uuid = kwargs['uuid']
39-
elif 'storage' in kwargs:
39+
elif 'storage' in kwargs: # let's never use storage.storage internally
4040
self.uuid = kwargs['storage']
4141

4242
if 'title' in kwargs:

0 commit comments

Comments
 (0)