Skip to content

Commit 4a9e7d0

Browse files
committed
Fix to 1.2 version
1 parent cf74985 commit 4a9e7d0

6 files changed

Lines changed: 57 additions & 117 deletions

File tree

libcloud/common/upcloud.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,7 @@ def start_node(self, node_id):
188188
:param node_id: Id of the Node
189189
:type node_id: ``int``
190190
"""
191-
self.connection.request(
192-
"1.2/server/{}/start".format(node_id),
193-
method="POST",
194-
data=json.dumps({"server": {}}),
195-
)
191+
self.connection.request("1.2/server/{}/start".format(node_id), method="POST")
196192

197193
def get_node_state(self, node_id):
198194
"""

libcloud/compute/drivers/upcloud.py

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,6 @@ def create_volume(
240240
location=None,
241241
snapshot=None,
242242
ex_tier="maxiops",
243-
ex_encrypted=False,
244-
ex_labels=None,
245243
ex_backup_rule=None,
246244
):
247245
"""
@@ -256,17 +254,10 @@ def create_volume(
256254
:param location: Which data center to create a volume in. (required)
257255
:type location: :class:`.NodeLocation`
258256
259-
:param ex_tier: UpCloud storage tier: ``maxiops``, ``standard``, or
260-
``hdd``. Default is ``maxiops``. (optional)
257+
:param ex_tier: UpCloud storage tier: ``maxiops`` or ``hdd``.
258+
Default is ``maxiops``. (optional)
261259
:type ex_tier: ``str``
262260
263-
:param ex_encrypted: Create the volume encrypted at rest. Default is
264-
False. (optional)
265-
:type ex_encrypted: ``bool``
266-
267-
:param ex_labels: Labels for the volume. (optional)
268-
:type ex_labels: ``list`` of ``dict``
269-
270261
:param ex_backup_rule: Backup rule block for automatic backups.
271262
(optional)
272263
:type ex_backup_rule: ``dict``
@@ -284,10 +275,7 @@ def create_volume(
284275
"title": name,
285276
"zone": location.id,
286277
"tier": ex_tier,
287-
"encrypted": "yes" if ex_encrypted else "no",
288278
}
289-
if ex_labels is not None:
290-
storage["labels"] = ex_labels
291279
if ex_backup_rule is not None:
292280
storage["backup_rule"] = ex_backup_rule
293281

@@ -327,35 +315,51 @@ def attach_volume(
327315
Default is False. (optional)
328316
:type ex_boot_disk: ``bool``
329317
330-
:rtype: :class:`StorageVolume`
318+
:rtype: ``bool``
331319
"""
332320
storage_device = {
333321
"type": ex_type,
334-
"server": node.id,
322+
"storage": volume.id,
335323
"boot_disk": "1" if ex_boot_disk else "0",
336324
}
337325
if device is not None:
338326
storage_device["address"] = device
339327

340-
response = self.connection.request(
341-
"1.2/storage/{}/attach".format(volume.id),
328+
self.connection.request(
329+
"1.2/server/{}/storage/attach".format(node.id),
342330
method="POST",
343331
data=json.dumps({"storage_device": storage_device}),
344332
)
345-
return self._to_volume(response.object["storage"])
333+
return True
346334

347-
def detach_volume(self, volume):
335+
def detach_volume(self, volume, ex_node=None, ex_address=None):
348336
"""
349337
Detach a storage volume from its server.
350338
351339
:param volume: Volume to detach.
352340
:type volume: :class:`StorageVolume`
353341
342+
:param ex_node: Node where the volume is attached. Required by the
343+
UpCloud 1.2 detach endpoint.
344+
:type ex_node: :class:`Node`
345+
346+
:param ex_address: Device address to detach, for example
347+
``scsi:0:0``. Required by the UpCloud 1.2 detach
348+
endpoint.
349+
:type ex_address: ``str``
350+
354351
:rtype: ``bool``
355352
"""
353+
if ex_node is None or ex_address is None:
354+
raise ValueError(
355+
"UpCloud API 1.2 requires `ex_node` and `ex_address` "
356+
"when detaching a volume."
357+
)
358+
356359
self.connection.request(
357-
"1.2/storage/{}/detach".format(volume.id),
360+
"1.2/server/{}/storage/detach".format(ex_node.id),
358361
method="POST",
362+
data=json.dumps({"storage_device": {"address": ex_address}}),
359363
)
360364
return True
361365

@@ -401,45 +405,16 @@ def reboot_node(self, node):
401405
)
402406
return True
403407

404-
def start_node(
405-
self,
406-
node,
407-
ex_host=None,
408-
ex_avoid_host=None,
409-
ex_start_type=None,
410-
):
408+
def start_node(self, node):
411409
"""
412410
Start the given node.
413411
414412
:param node: the node to start
415413
:type node: :class:`Node`
416414
417-
:param ex_host: Host id to start the node on. Only available for
418-
private cloud hosts. (optional)
419-
:type ex_host: ``int``
420-
421-
:param ex_avoid_host: Host id to avoid when starting the node.
422-
(optional)
423-
:type ex_avoid_host: ``int``
424-
425-
:param ex_start_type: Start type, ``sync`` or ``async``. (optional)
426-
:type ex_start_type: ``str``
427-
428415
:rtype: ``bool``
429416
"""
430-
server = {}
431-
if ex_host is not None:
432-
server["host"] = ex_host
433-
if ex_avoid_host is not None:
434-
server["avoid_host"] = ex_avoid_host
435-
if ex_start_type is not None:
436-
server["start_type"] = ex_start_type
437-
438-
self.connection.request(
439-
"1.2/server/{}/start".format(node.id),
440-
method="POST",
441-
data=json.dumps({"server": server}),
442-
)
417+
self.connection.request("1.2/server/{}/start".format(node.id), method="POST")
443418
return True
444419

445420
def stop_node(self, node, ex_stop_type="hard", ex_timeout=None):

libcloud/test/compute/fixtures/upcloud/api_1_2_storage_01d4fcd4-e446-433b-8a9c-551a1284952e.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
{
22
"storage": {
33
"access": "private",
4-
"encrypted": "yes",
54
"backup_rule": "",
65
"backups": {
76
"backup": []
87
},
9-
"labels": [
10-
{
11-
"key": "env",
12-
"value": "test"
13-
}
14-
],
158
"license": 0,
169
"servers": {
1710
"server": [
@@ -20,7 +13,7 @@
2013
},
2114
"size": 50,
2215
"state": "online",
23-
"tier": "standard",
16+
"tier": "maxiops",
2417
"title": "data",
2518
"type": "normal",
2619
"uuid": "01d4fcd4-e446-433b-8a9c-551a1284952e",

libcloud/test/compute/fixtures/upcloud/api_1_2_storage_create.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
{
22
"storage": {
33
"access": "private",
4-
"encrypted": "yes",
54
"backup_rule": "",
65
"backups": {
76
"backup": []
87
},
9-
"labels": [
10-
{
11-
"key": "env",
12-
"value": "test"
13-
}
14-
],
158
"license": 0,
169
"servers": {
1710
"server": []
1811
},
1912
"size": 50,
2013
"state": "online",
21-
"tier": "standard",
14+
"tier": "maxiops",
2215
"title": "data",
2316
"type": "normal",
2417
"uuid": "01d4fcd4-e446-433b-8a9c-551a1284952e",

libcloud/test/compute/fixtures/upcloud/api_1_2_storage_normal.json

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
"storage": [
44
{
55
"access": "private",
6-
"encrypted": "no",
7-
"labels": [],
86
"license": 0,
97
"size": 10,
108
"state": "online",
@@ -16,20 +14,13 @@
1614
},
1715
{
1816
"access": "private",
19-
"encrypted": "yes",
20-
"labels": [
21-
{
22-
"key": "env",
23-
"value": "test"
24-
}
25-
],
2617
"license": 0,
2718
"servers": {
2819
"server": []
2920
},
3021
"size": 50,
3122
"state": "online",
32-
"tier": "standard",
23+
"tier": "maxiops",
3324
"title": "data",
3425
"type": "normal",
3526
"uuid": "01d4fcd4-e446-433b-8a9c-551a1284952e",

libcloud/test/compute/test_upcloud.py

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def test_create_node_with_extra_storage_devices(self):
221221
"action": "create",
222222
"title": "data",
223223
"size": 25,
224-
"tier": "standard",
224+
"tier": "maxiops",
225225
}
226226

227227
self.driver.create_node(
@@ -256,19 +256,16 @@ def test_create_volume(self):
256256
size=50,
257257
name="data",
258258
location=location,
259-
ex_tier="standard",
260-
ex_encrypted=True,
261-
ex_labels=[{"key": "env", "value": "test"}],
259+
ex_tier="maxiops",
262260
)
263261

264262
self.assertEqual(volume.id, "01d4fcd4-e446-433b-8a9c-551a1284952e")
265263
self.assertEqual(volume.name, "data")
266264
self.assertEqual(volume.size, 50)
267-
self.assertEqual(volume.extra["encrypted"], "yes")
268265
request_storage = UpcloudMockHttp.last_request_body["storage"]
269-
self.assertEqual(request_storage["tier"], "standard")
270-
self.assertEqual(request_storage["encrypted"], "yes")
271-
self.assertEqual(request_storage["labels"], [{"key": "env", "value": "test"}])
266+
self.assertEqual(request_storage["tier"], "maxiops")
267+
self.assertNotIn("encrypted", request_storage)
268+
self.assertNotIn("labels", request_storage)
272269

273270
def test_create_volume_requires_location(self):
274271
with self.assertRaises(ValueError):
@@ -278,18 +275,26 @@ def test_attach_volume(self):
278275
node = self.driver.list_nodes()[0]
279276
volume = self.driver.list_volumes()[1]
280277

281-
attached_volume = self.driver.attach_volume(node, volume, device="scsi")
278+
success = self.driver.attach_volume(node, volume, device="scsi")
282279

283-
self.assertIsInstance(attached_volume, StorageVolume)
284-
self.assertEqual(attached_volume.id, volume.id)
280+
self.assertTrue(success)
285281
request_device = UpcloudMockHttp.last_request_body["storage_device"]
286-
self.assertEqual(request_device["server"], node.id)
282+
self.assertEqual(request_device["storage"], volume.id)
287283
self.assertEqual(request_device["address"], "scsi")
288284
self.assertEqual(request_device["boot_disk"], "0")
289285

290286
def test_detach_volume(self):
287+
node = self.driver.list_nodes()[0]
288+
volume = self.driver.list_volumes()[1]
289+
self.assertTrue(self.driver.detach_volume(volume, ex_node=node, ex_address="scsi:0:0"))
290+
291+
request_device = UpcloudMockHttp.last_request_body["storage_device"]
292+
self.assertEqual(request_device["address"], "scsi:0:0")
293+
294+
def test_detach_volume_requires_node_and_address(self):
291295
volume = self.driver.list_volumes()[1]
292-
self.assertTrue(self.driver.detach_volume(volume))
296+
with self.assertRaises(ValueError):
297+
self.driver.detach_volume(volume)
293298

294299
def test_destroy_volume(self):
295300
volume = self.driver.list_volumes()[1]
@@ -313,24 +318,10 @@ def test_reboot_node(self):
313318

314319
def test_start_node(self):
315320
nodes = self.driver.list_nodes()
316-
success = self.driver.start_node(
317-
nodes[0],
318-
ex_host=8055964291,
319-
ex_avoid_host=7653311107,
320-
ex_start_type="async",
321-
)
321+
success = self.driver.start_node(nodes[0])
322322

323323
self.assertTrue(success)
324-
self.assertEqual(
325-
UpcloudMockHttp.last_request_body,
326-
{
327-
"server": {
328-
"host": 8055964291,
329-
"avoid_host": 7653311107,
330-
"start_type": "async",
331-
}
332-
},
333-
)
324+
self.assertIsNone(UpcloudMockHttp.last_request_body)
334325

335326
def test_stop_node(self):
336327
nodes = self.driver.list_nodes()
@@ -443,17 +434,18 @@ def _1_2_storage(self, method, url, body, headers):
443434
body = self.fixtures.load("api_1_2_storage_create.json")
444435
return (httplib.CREATED, body, {}, httplib.responses[httplib.CREATED])
445436

446-
def _1_2_storage_01d4fcd4_e446_433b_8a9c_551a1284952e_attach(
437+
def _1_2_server_00f8c525_7e62_4108_8115_3958df5b43dc_storage_attach(
447438
self, method, url, body, headers
448439
):
449440
self.__class__.last_request_body = json.loads(body)
450-
body = self.fixtures.load("api_1_2_storage_01d4fcd4-e446-433b-8a9c-551a1284952e.json")
441+
body = self.fixtures.load("api_1_2_server_00f8c525-7e62-4108-8115-3958df5b43dc.json")
451442
return (httplib.OK, body, {}, httplib.responses[httplib.OK])
452443

453-
def _1_2_storage_01d4fcd4_e446_433b_8a9c_551a1284952e_detach(
444+
def _1_2_server_00f8c525_7e62_4108_8115_3958df5b43dc_storage_detach(
454445
self, method, url, body, headers
455446
):
456-
body = self.fixtures.load("api_1_2_storage_01d4fcd4-e446-433b-8a9c-551a1284952e.json")
447+
self.__class__.last_request_body = json.loads(body)
448+
body = self.fixtures.load("api_1_2_server_00f8c525-7e62-4108-8115-3958df5b43dc.json")
457449
return (httplib.OK, body, {}, httplib.responses[httplib.OK])
458450

459451
def _1_2_storage_01d4fcd4_e446_433b_8a9c_551a1284952e(self, method, url, body, headers):
@@ -470,7 +462,7 @@ def _1_2_server_00f8c525_7e62_4108_8115_3958df5b43dc_restart(self, method, url,
470462
return (httplib.OK, body, {}, httplib.responses[httplib.OK])
471463

472464
def _1_2_server_00f8c525_7e62_4108_8115_3958df5b43dc_start(self, method, url, body, headers):
473-
self.__class__.last_request_body = json.loads(body)
465+
self.__class__.last_request_body = body
474466
body = self.fixtures.load("api_1_2_server_00f8c525-7e62-4108-8115-3958df5b43dc.json")
475467
return (httplib.OK, body, {}, httplib.responses[httplib.OK])
476468

0 commit comments

Comments
 (0)