Skip to content

Commit 0374d8d

Browse files
authored
feat(ipam): add support for custom resource Attach/Detach/Move IP (#632)
1 parent df2382a commit 0374d8d

File tree

8 files changed

+506
-0
lines changed

8 files changed

+506
-0
lines changed

scaleway-async/scaleway_async/ipam/v1/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
from .types import Resource
66
from .types import Reverse
77
from .types import Source
8+
from .types import CustomResource
89
from .types import IP
10+
from .types import AttachIPRequest
911
from .types import BookIPRequest
12+
from .types import DetachIPRequest
1013
from .types import GetIPRequest
1114
from .types import ListIPsRequest
1215
from .types import ListIPsResponse
16+
from .types import MoveIPRequest
1317
from .types import ReleaseIPRequest
1418
from .types import ReleaseIPSetRequest
1519
from .types import UpdateIPRequest
@@ -21,11 +25,15 @@
2125
"Resource",
2226
"Reverse",
2327
"Source",
28+
"CustomResource",
2429
"IP",
30+
"AttachIPRequest",
2531
"BookIPRequest",
32+
"DetachIPRequest",
2633
"GetIPRequest",
2734
"ListIPsRequest",
2835
"ListIPsResponse",
36+
"MoveIPRequest",
2937
"ReleaseIPRequest",
3038
"ReleaseIPSetRequest",
3139
"UpdateIPRequest",

scaleway-async/scaleway_async/ipam/v1/api.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
from .types import (
1717
ListIPsRequestOrderBy,
1818
ResourceType,
19+
AttachIPRequest,
1920
BookIPRequest,
21+
CustomResource,
2022
IP,
2123
ListIPsResponse,
24+
MoveIPRequest,
2225
ReleaseIPSetRequest,
2326
Reverse,
2427
Source,
@@ -27,7 +30,9 @@
2730
from .marshalling import (
2831
unmarshal_IP,
2932
unmarshal_ListIPsResponse,
33+
marshal_AttachIPRequest,
3034
marshal_BookIPRequest,
35+
marshal_MoveIPRequest,
3136
marshal_ReleaseIPSetRequest,
3237
marshal_UpdateIPRequest,
3338
)
@@ -47,6 +52,7 @@ async def book_ip(
4752
project_id: Optional[str] = None,
4853
address: Optional[str] = None,
4954
tags: Optional[List[str]] = None,
55+
resource: Optional[CustomResource] = None,
5056
) -> IP:
5157
"""
5258
Book a new IP.
@@ -57,6 +63,7 @@ async def book_ip(
5763
:param project_id: When creating an IP in a Private Network, the Project must match the Private Network's Project.
5864
:param address: The requested address should not include the subnet mask (/suffix). Note that only the Private Network source allows you to pick a specific IP. If the requested IP is already booked, then the call will fail.
5965
:param tags: Tags for the IP.
66+
:param resource: Custom resource to attach to the IP being booked. An example of a custom resource is a virtual machine hosted on an Elastic Metal server, or an additional user network interface on an Instance. Do not use this for attaching IP addresses to standard Scaleway resources, as it will fail - instead, see the relevant product API for an equivalent method.
6067
:return: :class:`IP <IP>`
6168
6269
Usage:
@@ -83,6 +90,7 @@ async def book_ip(
8390
project_id=project_id,
8491
address=address,
8592
tags=tags,
93+
resource=resource,
8694
),
8795
self.client,
8896
),
@@ -408,3 +416,127 @@ async def list_i_ps_all(
408416
"subnet_id": subnet_id,
409417
},
410418
)
419+
420+
async def attach_ip(
421+
self,
422+
*,
423+
ip_id: str,
424+
resource: CustomResource,
425+
region: Optional[Region] = None,
426+
) -> IP:
427+
"""
428+
Attach existing IP to custom resource.
429+
Attach an existing IP from a Private Network subnet to a custom, named resource via its MAC address. An example of a custom resource is a virtual machine hosted on an Elastic Metal server, or an additional user network interface on an Instance. Do not use this method for attaching IP addresses to standard Scaleway resources as it will fail - see the relevant product API for an equivalent method.
430+
:param ip_id: IP ID.
431+
:param resource: Custom resource to be attached to the IP.
432+
:param region: Region to target. If none is passed will use default region from the config.
433+
:return: :class:`IP <IP>`
434+
435+
Usage:
436+
::
437+
438+
result = await api.attach_ip(
439+
ip_id="example",
440+
resource=CustomResource(),
441+
)
442+
"""
443+
444+
param_region = validate_path_param(
445+
"region", region or self.client.default_region
446+
)
447+
param_ip_id = validate_path_param("ip_id", ip_id)
448+
449+
res = self._request(
450+
"POST",
451+
f"/ipam/v1/regions/{param_region}/ips/{param_ip_id}/attach",
452+
body=marshal_AttachIPRequest(
453+
AttachIPRequest(
454+
ip_id=ip_id,
455+
resource=resource,
456+
region=region,
457+
),
458+
self.client,
459+
),
460+
)
461+
462+
self._throw_on_error(res)
463+
return unmarshal_IP(res.json())
464+
465+
async def detach_ip(
466+
self,
467+
*,
468+
ip_id: str,
469+
region: Optional[Region] = None,
470+
) -> IP:
471+
"""
472+
Detach existing IP from a custom resource.
473+
Detach a private IP from a custom resource. An example of a custom resource is a virtual machine hosted on an Elastic Metal server. Do not use this method for attaching IP addresses to standard Scaleway resources (e.g. Instances, Load Balancers) as it will fail - see the relevant product API for an equivalent method.
474+
:param ip_id: IP ID.
475+
:param region: Region to target. If none is passed will use default region from the config.
476+
:return: :class:`IP <IP>`
477+
478+
Usage:
479+
::
480+
481+
result = await api.detach_ip(
482+
ip_id="example",
483+
)
484+
"""
485+
486+
param_region = validate_path_param(
487+
"region", region or self.client.default_region
488+
)
489+
param_ip_id = validate_path_param("ip_id", ip_id)
490+
491+
res = self._request(
492+
"POST",
493+
f"/ipam/v1/regions/{param_region}/ips/{param_ip_id}/detach",
494+
body={},
495+
)
496+
497+
self._throw_on_error(res)
498+
return unmarshal_IP(res.json())
499+
500+
async def move_ip(
501+
self,
502+
*,
503+
ip_id: str,
504+
region: Optional[Region] = None,
505+
resource: Optional[CustomResource] = None,
506+
) -> IP:
507+
"""
508+
Move existing IP to a custom resource.
509+
Move an existing private IP from one custom resource (e.g. a virtual machine hosted on an Elastic Metal server) to another custom resource. This will detach it from the first resource, and attach it to the second. Do not use this method for moving IP addresses between standard Scaleway resources (e.g. Instances, Load Balancers) as it will fail - see the relevant product API for an equivalent method.
510+
:param ip_id: IP ID.
511+
:param region: Region to target. If none is passed will use default region from the config.
512+
:param resource: Custom resource to be attached to the IP.
513+
:return: :class:`IP <IP>`
514+
515+
Usage:
516+
::
517+
518+
result = await api.move_ip(
519+
ip_id="example",
520+
)
521+
"""
522+
523+
param_region = validate_path_param(
524+
"region", region or self.client.default_region
525+
)
526+
param_ip_id = validate_path_param("ip_id", ip_id)
527+
528+
res = self._request(
529+
"POST",
530+
f"/ipam/v1/regions/{param_region}/ips/{param_ip_id}/move",
531+
body=marshal_MoveIPRequest(
532+
MoveIPRequest(
533+
ip_id=ip_id,
534+
region=region,
535+
resource=resource,
536+
),
537+
self.client,
538+
),
539+
)
540+
541+
self._throw_on_error(res)
542+
return unmarshal_IP(res.json())

scaleway-async/scaleway_async/ipam/v1/marshalling.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
Source,
1616
IP,
1717
ListIPsResponse,
18+
CustomResource,
19+
AttachIPRequest,
1820
BookIPRequest,
21+
MoveIPRequest,
1922
ReleaseIPSetRequest,
2023
UpdateIPRequest,
2124
)
@@ -192,6 +195,33 @@ def unmarshal_ListIPsResponse(data: Any) -> ListIPsResponse:
192195
return ListIPsResponse(**args)
193196

194197

198+
def marshal_CustomResource(
199+
request: CustomResource,
200+
defaults: ProfileDefaults,
201+
) -> Dict[str, Any]:
202+
output: Dict[str, Any] = {}
203+
204+
if request.mac_address is not None:
205+
output["mac_address"] = request.mac_address
206+
207+
if request.name is not None:
208+
output["name"] = request.name
209+
210+
return output
211+
212+
213+
def marshal_AttachIPRequest(
214+
request: AttachIPRequest,
215+
defaults: ProfileDefaults,
216+
) -> Dict[str, Any]:
217+
output: Dict[str, Any] = {}
218+
219+
if request.resource is not None:
220+
output["resource"] = marshal_CustomResource(request.resource, defaults)
221+
222+
return output
223+
224+
195225
def marshal_Source(
196226
request: Source,
197227
defaults: ProfileDefaults,
@@ -231,6 +261,21 @@ def marshal_BookIPRequest(
231261
if request.tags is not None:
232262
output["tags"] = request.tags
233263

264+
if request.resource is not None:
265+
output["resource"] = marshal_CustomResource(request.resource, defaults)
266+
267+
return output
268+
269+
270+
def marshal_MoveIPRequest(
271+
request: MoveIPRequest,
272+
defaults: ProfileDefaults,
273+
) -> Dict[str, Any]:
274+
output: Dict[str, Any] = {}
275+
276+
if request.resource is not None:
277+
output["resource"] = marshal_CustomResource(request.resource, defaults)
278+
234279
return output
235280

236281

scaleway-async/scaleway_async/ipam/v1/types.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def __str__(self) -> str:
3030

3131
class ResourceType(str, Enum, metaclass=StrEnumMeta):
3232
UNKNOWN_TYPE = "unknown_type"
33+
CUSTOM = "custom"
3334
INSTANCE_SERVER = "instance_server"
3435
INSTANCE_IP = "instance_ip"
3536
INSTANCE_PRIVATE_NIC = "instance_private_nic"
@@ -94,6 +95,19 @@ class Source:
9495
subnet_id: Optional[str]
9596

9697

98+
@dataclass
99+
class CustomResource:
100+
mac_address: str
101+
"""
102+
MAC address of the custom resource.
103+
"""
104+
105+
name: Optional[str]
106+
"""
107+
When the resource is in a Private Network, a DNS record is available to resolve the resource name.
108+
"""
109+
110+
97111
@dataclass
98112
class IP:
99113
id: str
@@ -157,6 +171,24 @@ class IP:
157171
"""
158172

159173

174+
@dataclass
175+
class AttachIPRequest:
176+
ip_id: str
177+
"""
178+
IP ID.
179+
"""
180+
181+
resource: CustomResource
182+
"""
183+
Custom resource to be attached to the IP.
184+
"""
185+
186+
region: Optional[Region]
187+
"""
188+
Region to target. If none is passed will use default region from the config.
189+
"""
190+
191+
160192
@dataclass
161193
class BookIPRequest:
162194
source: Source
@@ -189,6 +221,24 @@ class BookIPRequest:
189221
Tags for the IP.
190222
"""
191223

224+
resource: Optional[CustomResource]
225+
"""
226+
Custom resource to attach to the IP being booked. An example of a custom resource is a virtual machine hosted on an Elastic Metal server, or an additional user network interface on an Instance. Do not use this for attaching IP addresses to standard Scaleway resources, as it will fail - instead, see the relevant product API for an equivalent method.
227+
"""
228+
229+
230+
@dataclass
231+
class DetachIPRequest:
232+
ip_id: str
233+
"""
234+
IP ID.
235+
"""
236+
237+
region: Optional[Region]
238+
"""
239+
Region to target. If none is passed will use default region from the config.
240+
"""
241+
192242

193243
@dataclass
194244
class GetIPRequest:
@@ -294,6 +344,24 @@ class ListIPsResponse:
294344
ips: List[IP]
295345

296346

347+
@dataclass
348+
class MoveIPRequest:
349+
ip_id: str
350+
"""
351+
IP ID.
352+
"""
353+
354+
region: Optional[Region]
355+
"""
356+
Region to target. If none is passed will use default region from the config.
357+
"""
358+
359+
resource: Optional[CustomResource]
360+
"""
361+
Custom resource to be attached to the IP.
362+
"""
363+
364+
297365
@dataclass
298366
class ReleaseIPRequest:
299367
ip_id: str

0 commit comments

Comments
 (0)