-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathstorage_mixin.py
More file actions
252 lines (216 loc) · 8.9 KB
/
storage_mixin.py
File metadata and controls
252 lines (216 loc) · 8.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
from os import PathLike
from typing import BinaryIO
from upcloud_api.api import API
from upcloud_api.storage import BackupDeletionPolicy, Storage
from upcloud_api.storage_import import StorageImport
class StorageManager:
"""
Functions for managing Storage disks. Intended to be used as a mixin for CloudManager.
"""
api: API
def get_storages(self, storage_type='normal'):
"""
Return a list of Storage objects from the API.
Storage types: public, private, normal, backup, cdrom, template, favorite
"""
res = self.api.get_request('/storage/' + storage_type)
return Storage._create_storage_objs(res['storages'], cloud_manager=self)
def get_templates(self):
"""
Return a list of Storages that are templates in a dict with title as key and uuid as value.
"""
templates = []
res = self.api.get_request('/storage/template')
for item in res.get('storages').get('storage'):
templates.append({item.get('title'): item.get('uuid')})
return templates
def get_storage(self, storage: str) -> Storage:
"""
Return a Storage object from the API.
"""
res = self.api.get_request('/storage/' + str(storage))
return Storage(cloud_manager=self, **res['storage'])
def create_storage(
self,
zone: str,
size: int = 10,
tier: str = 'maxiops',
title: str = 'Storage disk',
encrypted: bool = False,
*,
backup_rule: dict | None = None,
) -> Storage:
"""
Create a Storage object. Returns an object based on the API's response.
"""
if backup_rule is None:
backup_rule = {}
encrypted_str = 'yes' if encrypted else 'no'
body = {
'storage': {
'size': size,
'tier': tier,
'title': title,
'zone': zone,
'backup_rule': backup_rule,
'encrypted': encrypted_str,
}
}
res = self.api.post_request('/storage', body)
return Storage(cloud_manager=self, **res['storage'])
def _modify_storage(self, storage, size, title, backup_rule: dict | None = None):
body = {'storage': {}}
if size:
body['storage']['size'] = size
if title:
body['storage']['title'] = title
if backup_rule:
body['storage']['backup_rule'] = backup_rule
return self.api.put_request('/storage/' + str(storage), body)
def modify_storage(
self, storage: str, size: int, title: str, backup_rule: dict | None = None
) -> Storage:
"""
Modify a Storage object. Returns an object based on the API's response.
"""
res = self._modify_storage(str(storage), size, title, backup_rule)
return Storage(cloud_manager=self, **res['storage'])
def delete_storage(self, uuid: str, backups: BackupDeletionPolicy = BackupDeletionPolicy.KEEP):
"""
Destroy a Storage object, and possibly some or all of its backups.
"""
return self.api.delete_request(f'/storage/{uuid}?backups={backups.value}')
def clone_storage(self, storage: Storage | str, title: str, zone: str, tier=None) -> Storage:
"""
Clones a Storage object. Returns an object based on the API's response.
"""
body = {'storage': {'title': title, 'zone': zone}}
if tier:
body['storage']['tier'] = tier
# TODO: `str(storage)` seems unsafe
res = self.api.post_request(f'/storage/{str(storage)}/clone', body)
return Storage(cloud_manager=self, **res['storage'])
def cancel_clone_storage(self, storage):
"""
Cancels a running cloning operation and deletes the incomplete copy.
"""
return self.api.post_request(f'/storage/{str(storage)}/cancel')
def attach_storage(self, server, storage, storage_type, address):
"""
Attach a Storage object to a Server. Return a list of the server's storages.
"""
body = {'storage_device': {}}
if storage:
body['storage_device']['storage'] = str(storage)
if storage_type:
body['storage_device']['type'] = storage_type
if address:
body['storage_device']['address'] = address
url = f'/server/{server}/storage/attach'
res = self.api.post_request(url, body)
return Storage._create_storage_objs(res['server']['storage_devices'], cloud_manager=self)
def detach_storage(self, server, address):
"""
Detach a Storage object to a Server. Return a list of the server's storages.
"""
body = {'storage_device': {'address': address}}
url = f'/server/{server}/storage/detach'
res = self.api.post_request(url, body)
return Storage._create_storage_objs(res['server']['storage_devices'], cloud_manager=self)
def load_cd_rom(self, server, address):
"""
Loads a storage as a CD-ROM in the CD-ROM device of a server.
"""
body = {'storage_device': {'storage': address}}
url = f'/server/{server}/cdrom/load'
res = self.api.post_request(url, body)
return Storage._create_storage_objs(res['server']['storage_devices'], cloud_manager=self)
def eject_cd_rom(self, server):
"""
Ejects the storage from the CD-ROM device of a server.
"""
url = f'/server/{server}/cdrom/eject'
res = self.api.post_request(url)
return Storage._create_storage_objs(res['server']['storage_devices'], cloud_manager=self)
def create_storage_backup(self, storage: str, title: str) -> Storage:
"""
Creates a point-in-time backup of a storage resource.
"""
url = f'/storage/{storage}/backup'
body = {'storage': {'title': title}}
res = self.api.post_request(url, body)
return Storage(cloud_manager=self, **res['storage'])
def restore_storage_backup(self, storage):
"""
Restores the origin storage with data from the specified backup storage.
"""
url = f'/storage/{storage}/restore'
return self.api.post_request(url)
def templatize_storage(self, storage: str, title: str) -> Storage:
"""
Creates an exact copy of an existing storage resource which can be used as a template for creating new servers.
"""
url = f'/storage/{storage}/templatize'
body = {'storage': {'title': title}}
res = self.api.post_request(url, body)
return Storage(cloud_manager=self, **res['storage'])
def create_storage_import(
self, storage: str, source: str, source_location=None
) -> StorageImport:
"""
Creates an import task to import data into an existing storage.
Source types: http_import or direct_upload.
"""
if source not in ("http_import", "direct_upload"):
raise Exception(f"invalid storage import source: {source}")
url = f'/storage/{storage}/import'
body = {'storage_import': {'source': source}}
if source_location:
body['storage_import']['source_location'] = source_location
res = self.api.post_request(url, body)
return StorageImport(**res['storage_import'])
def upload_file_for_storage_import(
self,
storage_import: StorageImport,
file: str | PathLike | BinaryIO,
timeout: int = 30,
content_type: str = 'application/octet-stream',
):
"""
Uploads a file directly to UpCloud's uploader session.
"""
import requests
# This is importing and using `requests` directly since there doesn't
# seem to be a point in adding a `.api.raw_request()` call to the `API` class.
# That could be changed if there starts to be more of these cases.
f = file
needs_closing = False
if not hasattr(file, 'read'):
f = open(file, 'rb')
needs_closing = True
try:
resp = requests.put(
url=storage_import.direct_upload_url,
data=f,
headers={'Content-type': content_type},
timeout=timeout,
)
resp.raise_for_status()
return resp.json()
finally:
if needs_closing:
f.close()
def get_storage_import_details(self, storage: str) -> StorageImport:
"""
Returns detailed information of an ongoing or finished import task.
"""
url = f'/storage/{storage}/import'
res = self.api.get_request(url)
return StorageImport(**res['storage_import'])
def cancel_storage_import(self, storage: str) -> StorageImport:
"""
Cancels an ongoing import task.
"""
url = f'/storage/{storage}/import/cancel'
res = self.api.post_request(url)
return StorageImport(**res['storage_import'])