Skip to content

Commit f8af52e

Browse files
author
faustas@techconsult.lt
committed
Reference new api-version, implement storage cloning and cancel operation + tests
1 parent 578c714 commit f8af52e

File tree

6 files changed

+72
-6
lines changed

6 files changed

+72
-6
lines changed

test/conftest.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def read_from_file(filename):
3232

3333

3434
class Mock(object):
35-
base_url = 'https://api.upcloud.com/1.2'
35+
base_url = 'https://api.upcloud.com/1.3'
3636

3737
@staticmethod
3838
def read_from_file(filename):
@@ -61,11 +61,14 @@ def __put_post_callback(request, target, data):
6161
return(200, {}, json.dumps(data))
6262

6363
@staticmethod
64-
def mock_post(target):
65-
data = json.loads(Mock.read_from_file(target + '_post.json'))
64+
def mock_post(target, empty_content=False):
6665

6766
def callback(request):
68-
return Mock.__put_post_callback(request, target, data)
67+
if not empty_content:
68+
data = json.loads(Mock.read_from_file(target + '_post.json'))
69+
return Mock.__put_post_callback(request, target, data)
70+
else:
71+
return(200, {}, '{}')
6972

7073
responses.add_callback(responses.POST, Mock.base_url + '/' + target,
7174
callback=callback,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"storage" : {
3+
"access" : "private",
4+
"backup_rule": "",
5+
"backups" : {
6+
"backup" : []
7+
},
8+
"license" : 0,
9+
"servers" : {
10+
"server" : [
11+
"00798b85-efdc-41ca-8021-f6ef457b8531"
12+
]
13+
},
14+
"size" : 666,
15+
"state" : "online",
16+
"tier" : "maxiops",
17+
"title" : "Operating system disk",
18+
"type" : "normal",
19+
"uuid" : "01d3e9ad-8ff5-4a52-9fa2-48938e488e78",
20+
"zone" : "fi-hel1"
21+
}
22+
}

test/test_storage.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,31 @@ def test_storage_create(self, manager):
3434
assert storage.title == "My data collection"
3535
assert storage.zone == "fi-hel1"
3636

37+
@responses.activate
38+
def test_clone_storage(self, manager):
39+
data = Mock.mock_get("storage/01d4fcd4-e446-433b-8a9c-551a1284952e")
40+
storage = manager.get_storage("01d4fcd4-e446-433b-8a9c-551a1284952e")
41+
42+
Mock.mock_post("storage/01d4fcd4-e446-433b-8a9c-551a1284952e/clone")
43+
cloned_storage = manager.clone_storage(storage, 'cloned-storage-test', 'fi-hel1')
44+
assert type(cloned_storage).__name__ == "Storage"
45+
assert cloned_storage.size == 666
46+
assert cloned_storage.tier == "maxiops"
47+
assert cloned_storage.title == "cloned-storage-test"
48+
assert cloned_storage.zone == "fi-hel1"
49+
50+
@responses.activate
51+
def test_cancel_clone_storage(self, manager):
52+
data = Mock.mock_get("storage/01d4fcd4-e446-433b-8a9c-551a1284952e")
53+
storage = manager.get_storage("01d4fcd4-e446-433b-8a9c-551a1284952e")
54+
55+
Mock.mock_post("storage/01d4fcd4-e446-433b-8a9c-551a1284952e/clone")
56+
cloned_storage = manager.clone_storage(storage, 'cloned-storage-test', 'fi-hel1')
57+
58+
Mock.mock_post("storage/01d3e9ad-8ff5-4a52-9fa2-48938e488e78/cancel", empty_content=True)
59+
res = manager.cancel_clone_storage(cloned_storage)
60+
assert res == {}
61+
3762
@responses.activate
3863
def test_storage_update(self, manager):
3964

@@ -61,7 +86,6 @@ def test_storage_delete(self, manager):
6186
res = manager.delete_storage("01d4fcd4-e446-433b-8a9c-551a1284952e")
6287
assert res == {}
6388

64-
6589
@responses.activate
6690
def test_storage_delete_oop(self, manager):
6791
data = Mock.mock_get("storage/01d4fcd4-e446-433b-8a9c-551a1284952e")

upcloud_api/cloud_manager/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class BaseAPI(object):
1212
"""
1313

1414
api = 'api.upcloud.com'
15-
api_v = '1.2'
15+
api_v = '1.3'
1616

1717
def __init__(self, token, timeout=None): # noqa
1818
self.token = token

upcloud_api/cloud_manager/storage_mixin.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ def delete_storage(self, UUID):
6666
"""
6767
return self.request('DELETE', '/storage/' + UUID)
6868

69+
def clone_storage(self, storage, title, zone, tier=None):
70+
"""
71+
Clones a Storage object. Returns an object based on the API's response,
72+
"""
73+
body = {'storage': {'title': title, 'zone': zone}}
74+
if tier:
75+
body['storage']['tier'] = tier
76+
res = self.request('POST', '/storage/{}/clone'.format(str(storage)), body)
77+
return Storage(cloud_manager=self, **res['storage'])
78+
79+
def cancel_clone_storage(self, storage):
80+
"""
81+
Cancels a running cloning operation and deletes the incomplete copy.
82+
"""
83+
return self.request('POST', '/storage/{}/cancel'.format(str(storage)))
84+
6985
def attach_storage(self, server, storage, storage_type, address):
7086
"""
7187
Attach a Storage object to a Server. Return a list of the server's storages.

0 commit comments

Comments
 (0)