Skip to content

Commit b5980a8

Browse files
authored
Merge pull request #96 from akx/types
Add some type annotations based on Monkeytype trace
2 parents 7cce91e + 0e274ab commit b5980a8

File tree

14 files changed

+97
-70
lines changed

14 files changed

+97
-70
lines changed

upcloud_api/cloud_manager/cloud_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class CloudManager(
3030
All other managers are mixed in so code can be organized in corresponding submanager classes.
3131
"""
3232

33-
def __init__(self, username, password, timeout=60):
33+
def __init__(self, username: str, password: str, timeout: int = 60) -> None:
3434
"""
3535
Initiates CloudManager that handles all HTTP conections with UpCloud's API.
3636

upcloud_api/cloud_manager/host_mixin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ def get_hosts(self):
1414
res = self.get_request(url)
1515
return [Host(**host) for host in res['hosts']['host']]
1616

17-
def get_host(self, id):
17+
def get_host(self, id: str) -> Host:
1818
"""
1919
Returns detailed information about a specific host.
2020
"""
2121
url = f'/host/{id}'
2222
res = self.get_request(url)
2323
return Host(**res['host'])
2424

25-
def modify_host(self, host, description):
25+
def modify_host(self, host: str, description: str) -> Host:
2626
"""
2727
Modifies description of a specific host.
2828
"""

upcloud_api/cloud_manager/ip_address_mixin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class IPManager:
66
Functions for managing IP-addresses. Intended to be used as a mixin for CloudManager.
77
"""
88

9-
def get_ip(self, address):
9+
def get_ip(self, address: str) -> IPAddress:
1010
"""
1111
Get an IPAddress object with the IP address (string) from the API.
1212
@@ -25,7 +25,7 @@ def get_ips(self, ignore_ips_without_server=False):
2525
)
2626
return IPs
2727

28-
def attach_ip(self, server, family='IPv4'):
28+
def attach_ip(self, server: str, family: str = 'IPv4') -> IPAddress:
2929
"""
3030
Attach a new (random) IPAddress to the given server (object or UUID).
3131
"""
@@ -34,7 +34,7 @@ def attach_ip(self, server, family='IPv4'):
3434
res = self.post_request('/ip_address', body)
3535
return IPAddress(cloud_manager=self, **res['ip_address'])
3636

37-
def modify_ip(self, ip_addr, ptr_record):
37+
def modify_ip(self, ip_addr: str, ptr_record: str) -> IPAddress:
3838
"""
3939
Modify an IP address' ptr-record (Reverse DNS).
4040

upcloud_api/cloud_manager/network_mixin.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def get_networks(self, zone=None):
2121
network.ip_networks = [IpNetwork(**n) for n in network.ip_networks.get('ip_network')]
2222
return networks
2323

24-
def get_network(self, uuid):
24+
def get_network(self, uuid: str) -> Network:
2525
"""
2626
Retrieves the details of a specific network.
2727
"""
@@ -198,15 +198,15 @@ def get_routers(self):
198198
res = self.get_request(url)
199199
return [Router(**router) for router in res['routers']['router']]
200200

201-
def get_router(self, uuid):
201+
def get_router(self, uuid: str) -> Router:
202202
"""
203203
Returns detailed information about a specific router.
204204
"""
205205
url = f'/router/{uuid}'
206206
res = self.get_request(url)
207207
return Router(**res['router'])
208208

209-
def create_router(self, name):
209+
def create_router(self, name: str) -> Router:
210210
"""
211211
Creates a new router.
212212
"""
@@ -215,7 +215,7 @@ def create_router(self, name):
215215
res = self.post_request(url, body)
216216
return Router(**res['router'])
217217

218-
def modify_router(self, router, name):
218+
def modify_router(self, router: str, name: str) -> Router:
219219
"""
220220
Modify an existing router.
221221
"""

upcloud_api/cloud_manager/object_storage_mixin.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ def get_object_storages(self):
2121
return object_storages
2222

2323
def create_object_storage(
24-
self, zone, access_key, secret_key, size, name=None, description=None
25-
):
24+
self,
25+
zone: str,
26+
access_key: str,
27+
secret_key: str,
28+
size: int,
29+
name: Optional[str] = None,
30+
description: Optional[str] = None,
31+
) -> ObjectStorage:
2632
"""
2733
Used to create a new Object Storage device with a given name, size and location.
2834
"""
@@ -42,7 +48,7 @@ def create_object_storage(
4248
res = self.post_request(url, body)
4349
return ObjectStorage(cloud_manager=self, **res['object_storage'])
4450

45-
def get_object_storage(self, uuid):
51+
def get_object_storage(self, uuid: str) -> ObjectStorage:
4652
"""
4753
A request to get details about a specific Object Storage device by the given uuid.
4854
"""
@@ -51,8 +57,13 @@ def get_object_storage(self, uuid):
5157
return ObjectStorage(cloud_manager=self, **res['object_storage'])
5258

5359
def modify_object_storage(
54-
self, object_storage, access_key=None, secret_key=None, description=None, size=None
55-
):
60+
self,
61+
object_storage: str,
62+
access_key: Optional[str] = None,
63+
secret_key: Optional[str] = None,
64+
description: Optional[str] = None,
65+
size: Optional[int] = None,
66+
) -> ObjectStorage:
5667
"""
5768
Modify requests can be used to update the details of an Object Storage including description, access_key and secret_key.
5869
"""

upcloud_api/cloud_manager/server_mixin.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def get_servers(self, populate=False, tags_has_one=None, tags_has_all=None):
4949

5050
return server_list
5151

52-
def get_server(self, UUID):
52+
def get_server(self, UUID: str) -> Server:
5353
"""
5454
Return a (populated) Server instance.
5555
"""
@@ -73,7 +73,7 @@ def get_server_by_ip(self, ip_address):
7373
UUID = data['ip_address']['server']
7474
return self.get_server(UUID)
7575

76-
def create_server(self, server):
76+
def create_server(self, server: Server) -> Server:
7777
"""
7878
Create a server and its storages based on a (locally created) Server object.
7979
@@ -117,7 +117,7 @@ def create_server(self, server):
117117
server_to_return._reset(res['server'], cloud_manager=self, populated=True)
118118
return server_to_return
119119

120-
def modify_server(self, UUID, **kwargs):
120+
def modify_server(self, uuid: str, **kwargs) -> Server:
121121
"""
122122
modify_server allows updating the server's updateable_fields.
123123
@@ -130,7 +130,7 @@ def modify_server(self, UUID, **kwargs):
130130
Exception(f'{arg} is not an updateable field')
131131
body['server'][arg] = kwargs[arg]
132132

133-
res = self.put_request(f'/server/{UUID}', body)
133+
res = self.put_request(f'/server/{uuid}', body)
134134
server = res['server']
135135

136136
# Populate subobjects
@@ -148,15 +148,15 @@ def modify_server(self, UUID, **kwargs):
148148
cloud_manager=self,
149149
)
150150

151-
def delete_server(self, UUID):
151+
def delete_server(self, uuid):
152152
"""
153153
DELETE '/server/UUID'. Permanently destroys the virtual machine.
154154
155155
DOES NOT remove the storage disks.
156156
157157
Returns an empty object.
158158
"""
159-
return self.delete_request(f'/server/{UUID}')
159+
return self.delete_request(f'/server/{uuid}')
160160

161161
def get_server_data(self, UUID):
162162
"""

upcloud_api/cloud_manager/storage_mixin.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Optional, Union
22

33
from upcloud_api.storage import Storage
44
from upcloud_api.storage_import import StorageImport
@@ -29,7 +29,7 @@ def get_templates(self):
2929
templates.append({item.get('title'): item.get('uuid')})
3030
return templates
3131

32-
def get_storage(self, storage):
32+
def get_storage(self, storage: str) -> Storage:
3333
"""
3434
Return a Storage object from the API.
3535
"""
@@ -38,12 +38,12 @@ def get_storage(self, storage):
3838

3939
def create_storage(
4040
self,
41-
size=10,
42-
tier='maxiops',
43-
title='Storage disk',
44-
zone='fi-hel1',
41+
size: int = 10,
42+
tier: str = 'maxiops',
43+
title: str = 'Storage disk',
44+
zone: str = 'fi-hel1',
4545
backup_rule: Optional[dict] = None,
46-
):
46+
) -> Storage:
4747
"""
4848
Create a Storage object. Returns an object based on the API's response.
4949
"""
@@ -71,7 +71,9 @@ def _modify_storage(self, storage, size, title, backup_rule: Optional[dict] = No
7171
body['storage']['backup_rule'] = backup_rule
7272
return self.put_request('/storage/' + str(storage), body)
7373

74-
def modify_storage(self, storage, size, title, backup_rule: Optional[dict] = None):
74+
def modify_storage(
75+
self, storage: str, size: int, title: str, backup_rule: Optional[dict] = None
76+
) -> Storage:
7577
"""
7678
Modify a Storage object. Returns an object based on the API's response.
7779
"""
@@ -84,13 +86,16 @@ def delete_storage(self, UUID):
8486
"""
8587
return self.delete_request('/storage/' + UUID)
8688

87-
def clone_storage(self, storage, title, zone, tier=None):
89+
def clone_storage(
90+
self, storage: Union[Storage, str], title: str, zone: str, tier=None
91+
) -> Storage:
8892
"""
8993
Clones a Storage object. Returns an object based on the API's response.
9094
"""
9195
body = {'storage': {'title': title, 'zone': zone}}
9296
if tier:
9397
body['storage']['tier'] = tier
98+
# TODO: `str(storage)` seems unsafe
9499
res = self.post_request(f'/storage/{str(storage)}/clone', body)
95100
return Storage(cloud_manager=self, **res['storage'])
96101

@@ -144,7 +149,7 @@ def eject_cd_rom(self, server):
144149
res = self.post_request(url)
145150
return Storage._create_storage_objs(res['server']['storage_devices'], cloud_manager=self)
146151

147-
def create_storage_backup(self, storage, title):
152+
def create_storage_backup(self, storage: str, title: str) -> Storage:
148153
"""
149154
Creates a point-in-time backup of a storage resource.
150155
"""
@@ -160,7 +165,7 @@ def restore_storage_backup(self, storage):
160165
url = f'/storage/{storage}/restore'
161166
return self.post_request(url)
162167

163-
def templatize_storage(self, storage, title):
168+
def templatize_storage(self, storage: str, title: str) -> Storage:
164169
"""
165170
Creates an exact copy of an existing storage resource which can be used as a template for creating new servers.
166171
"""
@@ -169,7 +174,9 @@ def templatize_storage(self, storage, title):
169174
res = self.post_request(url, body)
170175
return Storage(cloud_manager=self, **res['storage'])
171176

172-
def create_storage_import(self, storage, source, source_location=None):
177+
def create_storage_import(
178+
self, storage: str, source: str, source_location=None
179+
) -> StorageImport:
173180
"""
174181
Creates an import task to import data into an existing storage.
175182
Source types: http_import or direct_upload.
@@ -190,15 +197,15 @@ def upload_file_for_storage_import(self, storage_import, file):
190197
body = {'data': data}
191198
return self.put_request(url, body, timeout=600, request_to_api=False)
192199

193-
def get_storage_import_details(self, storage):
200+
def get_storage_import_details(self, storage: str) -> StorageImport:
194201
"""
195202
Returns detailed information of an ongoing or finished import task.
196203
"""
197204
url = f'/storage/{storage}/import'
198205
res = self.get_request(url)
199206
return StorageImport(**res['storage_import'])
200207

201-
def cancel_storage_import(self, storage):
208+
def cancel_storage_import(self, storage: str) -> StorageImport:
202209
"""
203210
Cancels an ongoing import task.
204211
"""

upcloud_api/cloud_manager/tag_mixin.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ def get_tags(self):
1515
res = self.get_request('/tag')
1616
return [Tag(cloud_manager=self, **tag) for tag in res['tags']['tag']]
1717

18-
def get_tag(self, name):
18+
def get_tag(self, name: str) -> Tag:
1919
"""Return the tag as Tag object."""
2020
res = self.get_request('/tag/' + name)
2121
return Tag(cloud_manager=self, **res['tag'])
2222

23-
def create_tag(self, name, description=None, servers: Optional[list] = None):
23+
def create_tag(
24+
self, name: str, description: Optional[str] = None, servers: Optional[list] = None
25+
) -> Tag:
2426
"""
2527
Create a new Tag. Only name is mandatory.
2628

upcloud_api/ip_address.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class IPAddress(UpCloudResource):
2828
'server': None,
2929
}
3030

31-
def save(self):
31+
def save(self) -> None:
3232
"""
3333
IPAddress can only change its PTR record. Saves the current state, PUT /ip_address/uuid.
3434
"""

0 commit comments

Comments
 (0)