Skip to content

Commit 1df8c54

Browse files
committed
typing: Add hints to api module
Most of this module should eventually be removed in favour of SDK, but that is a job for another day. Change-Id: I71cfb1f786a483a0715984dfb7dda451d4b33dac Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
1 parent 0b3301a commit 1df8c54

8 files changed

Lines changed: 166 additions & 149 deletions

File tree

openstackclient/api/api.py

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
"""Base API Library"""
1515

16+
from typing import Any
17+
1618
from keystoneauth1 import exceptions as ks_exceptions
1719
from keystoneauth1 import session as ks_session
1820
from osc_lib import exceptions
@@ -30,7 +32,9 @@ class KeystoneSession:
3032
3133
"""
3234

33-
def __init__(self, session=None, endpoint=None, **kwargs):
35+
def __init__(
36+
self, session: Any = None, endpoint: str | None = None, **kwargs: Any
37+
) -> None:
3438
"""Base object that contains some common API objects and methods
3539
3640
:param Session session:
@@ -46,7 +50,9 @@ def __init__(self, session=None, endpoint=None, **kwargs):
4650
self.session = session
4751
self.endpoint = endpoint
4852

49-
def _request(self, method, url, session=None, **kwargs):
53+
def _request(
54+
self, method: str, url: str, session: Any = None, **kwargs: Any
55+
) -> Any:
5056
"""Perform call into session
5157
5258
All API calls are funneled through this method to provide a common
@@ -82,8 +88,12 @@ class BaseAPI(KeystoneSession):
8288
"""Base API"""
8389

8490
def __init__(
85-
self, session=None, service_type=None, endpoint=None, **kwargs
86-
):
91+
self,
92+
session: Any = None,
93+
service_type: str | None = None,
94+
endpoint: str | None = None,
95+
**kwargs: Any,
96+
) -> None:
8797
"""Base object that contains some common API objects and methods
8898
8999
:param Session session:
@@ -101,7 +111,13 @@ def __init__(
101111

102112
# The basic action methods all take a Session and return dict/lists
103113

104-
def create(self, url, session=None, method=None, **params):
114+
def create(
115+
self,
116+
url: str,
117+
session: Any = None,
118+
method: str | None = None,
119+
**params: Any,
120+
) -> Any:
105121
"""Create a new resource
106122
107123
:param string url:
@@ -121,7 +137,7 @@ def create(self, url, session=None, method=None, **params):
121137
except requests.JSONDecodeError:
122138
return ret
123139

124-
def delete(self, url, session=None, **params):
140+
def delete(self, url: str, session: Any = None, **params: Any) -> Any:
125141
"""Delete a resource
126142
127143
:param string url:
@@ -132,7 +148,14 @@ def delete(self, url, session=None, **params):
132148

133149
return self._request('DELETE', url, **params)
134150

135-
def list(self, path, session=None, body=None, detailed=False, **params):
151+
def list(
152+
self,
153+
path: str,
154+
session: Any = None,
155+
body: Any = None,
156+
detailed: bool = False,
157+
**params: Any,
158+
) -> Any:
136159
"""Return a list of resources
137160
138161
GET ${ENDPOINT}/${PATH}?${PARAMS}
@@ -177,11 +200,11 @@ def list(self, path, session=None, body=None, detailed=False, **params):
177200

178201
def find_attr(
179202
self,
180-
path,
181-
value=None,
182-
attr=None,
183-
resource=None,
184-
):
203+
path: str,
204+
value: str | None = None,
205+
attr: str | None = None,
206+
resource: str | None = None,
207+
) -> Any:
185208
"""Find a resource via attribute or ID
186209
187210
Most APIs return a list wrapped by a dict with the resource
@@ -211,15 +234,15 @@ def find_attr(
211234
if resource is None:
212235
resource = path
213236

214-
def getlist(kw):
237+
def getlist(kw: dict[str, Any]) -> Any:
215238
"""Do list call, unwrap resource dict if present"""
216239
ret = self.list(path, **kw)
217240
if isinstance(ret, dict) and resource in ret:
218241
ret = ret[resource]
219242
return ret
220243

221244
# Search by attribute
222-
kwargs = {attr: value}
245+
kwargs: dict[str, Any] = {attr: value}
223246
data = getlist(kwargs)
224247
if isinstance(data, dict):
225248
return data
@@ -241,7 +264,7 @@ def getlist(kw):
241264
msg % {'resource': resource, 'attr': attr, 'value': value}
242265
)
243266

244-
def find_bulk(self, path, **kwargs):
267+
def find_bulk(self, path: str, **kwargs: Any) -> Any:
245268
"""Bulk load and filter locally
246269
247270
:param string path:
@@ -267,7 +290,7 @@ def find_bulk(self, path, **kwargs):
267290

268291
return ret
269292

270-
def find_one(self, path, **kwargs):
293+
def find_one(self, path: str, **kwargs: Any) -> Any:
271294
"""Find a resource by name or ID
272295
273296
:param string path:
@@ -286,12 +309,7 @@ def find_one(self, path, **kwargs):
286309
raise RuntimeError(msg)
287310
return bulk_list[0]
288311

289-
def find(
290-
self,
291-
path,
292-
value=None,
293-
attr=None,
294-
):
312+
def find(self, path: str, value: str, attr: str) -> Any:
295313
"""Find a single resource by name or ID
296314
297315
:param string path:

openstackclient/api/compute_v2.py

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"""
1818

1919
import http
20+
from typing import Any
2021

2122
from openstack import exceptions as sdk_exceptions
2223
from osc_lib import exceptions
@@ -25,7 +26,11 @@
2526
# security groups
2627

2728

28-
def create_security_group(compute_client, name=None, description=None):
29+
def create_security_group(
30+
compute_client: Any,
31+
name: str | None = None,
32+
description: str | None = None,
33+
) -> Any:
2934
"""Create a new security group
3035
3136
https://docs.openstack.org/api-ref/compute/#create-security-group
@@ -46,7 +51,9 @@ def create_security_group(compute_client, name=None, description=None):
4651
return response.json()['security_group']
4752

4853

49-
def list_security_groups(compute_client, all_projects=None):
54+
def list_security_groups(
55+
compute_client: Any, all_projects: bool | None = None
56+
) -> Any:
5057
"""Get all security groups
5158
5259
https://docs.openstack.org/api-ref/compute/#list-security-groups
@@ -63,7 +70,7 @@ def list_security_groups(compute_client, all_projects=None):
6370
return response.json()['security_groups']
6471

6572

66-
def find_security_group(compute_client, name_or_id):
73+
def find_security_group(compute_client: Any, name_or_id: str) -> Any:
6774
"""Find the security group for a given name or ID
6875
6976
https://docs.openstack.org/api-ref/compute/#show-security-group-details
@@ -101,8 +108,11 @@ def find_security_group(compute_client, name_or_id):
101108

102109

103110
def update_security_group(
104-
compute_client, security_group_id, name=None, description=None
105-
):
111+
compute_client: Any,
112+
security_group_id: str,
113+
name: str | None = None,
114+
description: str | None = None,
115+
) -> Any:
106116
"""Update an existing security group
107117
108118
https://docs.openstack.org/api-ref/compute/#update-security-group
@@ -127,7 +137,9 @@ def update_security_group(
127137
return response.json()['security_group']
128138

129139

130-
def delete_security_group(compute_client, security_group_id=None):
140+
def delete_security_group(
141+
compute_client: Any, security_group_id: str | None = None
142+
) -> None:
131143
"""Delete a security group
132144
133145
https://docs.openstack.org/api-ref/compute/#delete-security-group
@@ -146,14 +158,14 @@ def delete_security_group(compute_client, security_group_id=None):
146158

147159

148160
def create_security_group_rule(
149-
compute_client,
150-
security_group_id=None,
151-
ip_protocol=None,
152-
from_port=None,
153-
to_port=None,
154-
remote_ip=None,
155-
remote_group=None,
156-
):
161+
compute_client: Any,
162+
security_group_id: str | None = None,
163+
ip_protocol: str | None = None,
164+
from_port: int | None = None,
165+
to_port: int | None = None,
166+
remote_ip: str | None = None,
167+
remote_group: str | None = None,
168+
) -> Any:
157169
"""Create a new security group rule
158170
159171
https://docs.openstack.org/api-ref/compute/#create-security-group-rule
@@ -182,7 +194,9 @@ def create_security_group_rule(
182194
return response.json()['security_group_rule']
183195

184196

185-
def delete_security_group_rule(compute_client, security_group_rule_id=None):
197+
def delete_security_group_rule(
198+
compute_client: Any, security_group_rule_id: str | None = None
199+
) -> None:
186200
"""Delete a security group rule
187201
188202
https://docs.openstack.org/api-ref/compute/#delete-security-group-rule
@@ -201,7 +215,12 @@ def delete_security_group_rule(compute_client, security_group_rule_id=None):
201215
# networks
202216

203217

204-
def create_network(compute_client, name, subnet, share_subnet=None):
218+
def create_network(
219+
compute_client: Any,
220+
name: str,
221+
subnet: str,
222+
share_subnet: bool | None = None,
223+
) -> Any:
205224
"""Create a new network
206225
207226
https://docs.openstack.org/api-ref/compute/#create-network
@@ -212,7 +231,7 @@ def create_network(compute_client, name, subnet, share_subnet=None):
212231
:param bool share_subnet: Shared subnet between projects
213232
:returns: A network object
214233
"""
215-
data = {
234+
data: dict[str, Any] = {
216235
'label': name,
217236
'cidr': subnet,
218237
}
@@ -226,7 +245,7 @@ def create_network(compute_client, name, subnet, share_subnet=None):
226245
return response.json()['network']
227246

228247

229-
def list_networks(compute_client):
248+
def list_networks(compute_client: Any) -> Any:
230249
"""Get all networks
231250
232251
https://docs.openstack.org/api-ref/compute/#list-networks
@@ -239,7 +258,7 @@ def list_networks(compute_client):
239258
return response.json()['networks']
240259

241260

242-
def find_network(compute_client, name_or_id):
261+
def find_network(compute_client: Any, name_or_id: str) -> Any:
243262
"""Find the network for a given name or ID
244263
245264
https://docs.openstack.org/api-ref/compute/#show-network-details
@@ -276,7 +295,7 @@ def find_network(compute_client, name_or_id):
276295
return found
277296

278297

279-
def delete_network(compute_client, network_id):
298+
def delete_network(compute_client: Any, network_id: str) -> None:
280299
"""Delete a network
281300
282301
https://docs.openstack.org/api-ref/compute/#delete-network
@@ -294,7 +313,7 @@ def delete_network(compute_client, network_id):
294313
# floating ips
295314

296315

297-
def create_floating_ip(compute_client, network):
316+
def create_floating_ip(compute_client: Any, network: str) -> Any:
298317
"""Create a new floating ip
299318
300319
https://docs.openstack.org/api-ref/compute/#create-allocate-floating-ip-address
@@ -309,7 +328,7 @@ def create_floating_ip(compute_client, network):
309328
return response.json()['floating_ip']
310329

311330

312-
def list_floating_ips(compute_client):
331+
def list_floating_ips(compute_client: Any) -> Any:
313332
"""Get all floating IPs
314333
315334
https://docs.openstack.org/api-ref/compute/#list-floating-ip-addresses
@@ -321,7 +340,7 @@ def list_floating_ips(compute_client):
321340
return response.json()['floating_ips']
322341

323342

324-
def get_floating_ip(compute_client, floating_ip_id):
343+
def get_floating_ip(compute_client: Any, floating_ip_id: str) -> Any:
325344
"""Get a floating IP
326345
327346
https://docs.openstack.org/api-ref/compute/#show-floating-ip-address-details
@@ -336,7 +355,7 @@ def get_floating_ip(compute_client, floating_ip_id):
336355
return response.json()['floating_ip']
337356

338357

339-
def delete_floating_ip(compute_client, floating_ip_id):
358+
def delete_floating_ip(compute_client: Any, floating_ip_id: str) -> None:
340359
"""Delete a floating IP
341360
342361
https://docs.openstack.org/api-ref/compute/#delete-deallocate-floating-ip-address
@@ -353,7 +372,7 @@ def delete_floating_ip(compute_client, floating_ip_id):
353372
# floating ip pools
354373

355374

356-
def list_floating_ip_pools(compute_client):
375+
def list_floating_ip_pools(compute_client: Any) -> Any:
357376
"""Get all floating IP pools
358377
359378
https://docs.openstack.org/api-ref/compute/#list-floating-ip-pools

0 commit comments

Comments
 (0)