Skip to content

feat(ipam): add support for custom resource Attach/Detach/Move IP #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions scaleway-async/scaleway_async/ipam/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
from .types import Resource
from .types import Reverse
from .types import Source
from .types import CustomResource
from .types import IP
from .types import AttachIPRequest
from .types import BookIPRequest
from .types import DetachIPRequest
from .types import GetIPRequest
from .types import ListIPsRequest
from .types import ListIPsResponse
from .types import MoveIPRequest
from .types import ReleaseIPRequest
from .types import ReleaseIPSetRequest
from .types import UpdateIPRequest
Expand All @@ -21,11 +25,15 @@
"Resource",
"Reverse",
"Source",
"CustomResource",
"IP",
"AttachIPRequest",
"BookIPRequest",
"DetachIPRequest",
"GetIPRequest",
"ListIPsRequest",
"ListIPsResponse",
"MoveIPRequest",
"ReleaseIPRequest",
"ReleaseIPSetRequest",
"UpdateIPRequest",
Expand Down
132 changes: 132 additions & 0 deletions scaleway-async/scaleway_async/ipam/v1/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
from .types import (
ListIPsRequestOrderBy,
ResourceType,
AttachIPRequest,
BookIPRequest,
CustomResource,
IP,
ListIPsResponse,
MoveIPRequest,
ReleaseIPSetRequest,
Reverse,
Source,
Expand All @@ -27,7 +30,9 @@
from .marshalling import (
unmarshal_IP,
unmarshal_ListIPsResponse,
marshal_AttachIPRequest,
marshal_BookIPRequest,
marshal_MoveIPRequest,
marshal_ReleaseIPSetRequest,
marshal_UpdateIPRequest,
)
Expand All @@ -47,6 +52,7 @@ async def book_ip(
project_id: Optional[str] = None,
address: Optional[str] = None,
tags: Optional[List[str]] = None,
resource: Optional[CustomResource] = None,
) -> IP:
"""
Book a new IP.
Expand All @@ -57,6 +63,7 @@ async def book_ip(
:param project_id: When creating an IP in a Private Network, the Project must match the Private Network's Project.
: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.
:param tags: Tags for the IP.
: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.
:return: :class:`IP <IP>`

Usage:
Expand All @@ -83,6 +90,7 @@ async def book_ip(
project_id=project_id,
address=address,
tags=tags,
resource=resource,
),
self.client,
),
Expand Down Expand Up @@ -408,3 +416,127 @@ async def list_i_ps_all(
"subnet_id": subnet_id,
},
)

async def attach_ip(
self,
*,
ip_id: str,
resource: CustomResource,
region: Optional[Region] = None,
) -> IP:
"""
Attach existing IP to custom resource.
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.
:param ip_id: IP ID.
:param resource: Custom resource to be attached to the IP.
:param region: Region to target. If none is passed will use default region from the config.
:return: :class:`IP <IP>`

Usage:
::

result = await api.attach_ip(
ip_id="example",
resource=CustomResource(),
)
"""

param_region = validate_path_param(
"region", region or self.client.default_region
)
param_ip_id = validate_path_param("ip_id", ip_id)

res = self._request(
"POST",
f"/ipam/v1/regions/{param_region}/ips/{param_ip_id}/attach",
body=marshal_AttachIPRequest(
AttachIPRequest(
ip_id=ip_id,
resource=resource,
region=region,
),
self.client,
),
)

self._throw_on_error(res)
return unmarshal_IP(res.json())

async def detach_ip(
self,
*,
ip_id: str,
region: Optional[Region] = None,
) -> IP:
"""
Detach existing IP from a custom resource.
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.
:param ip_id: IP ID.
:param region: Region to target. If none is passed will use default region from the config.
:return: :class:`IP <IP>`

Usage:
::

result = await api.detach_ip(
ip_id="example",
)
"""

param_region = validate_path_param(
"region", region or self.client.default_region
)
param_ip_id = validate_path_param("ip_id", ip_id)

res = self._request(
"POST",
f"/ipam/v1/regions/{param_region}/ips/{param_ip_id}/detach",
body={},
)

self._throw_on_error(res)
return unmarshal_IP(res.json())

async def move_ip(
self,
*,
ip_id: str,
region: Optional[Region] = None,
resource: Optional[CustomResource] = None,
) -> IP:
"""
Move existing IP to a custom resource.
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.
:param ip_id: IP ID.
:param region: Region to target. If none is passed will use default region from the config.
:param resource: Custom resource to be attached to the IP.
:return: :class:`IP <IP>`

Usage:
::

result = await api.move_ip(
ip_id="example",
)
"""

param_region = validate_path_param(
"region", region or self.client.default_region
)
param_ip_id = validate_path_param("ip_id", ip_id)

res = self._request(
"POST",
f"/ipam/v1/regions/{param_region}/ips/{param_ip_id}/move",
body=marshal_MoveIPRequest(
MoveIPRequest(
ip_id=ip_id,
region=region,
resource=resource,
),
self.client,
),
)

self._throw_on_error(res)
return unmarshal_IP(res.json())
45 changes: 45 additions & 0 deletions scaleway-async/scaleway_async/ipam/v1/marshalling.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
Source,
IP,
ListIPsResponse,
CustomResource,
AttachIPRequest,
BookIPRequest,
MoveIPRequest,
ReleaseIPSetRequest,
UpdateIPRequest,
)
Expand Down Expand Up @@ -192,6 +195,33 @@ def unmarshal_ListIPsResponse(data: Any) -> ListIPsResponse:
return ListIPsResponse(**args)


def marshal_CustomResource(
request: CustomResource,
defaults: ProfileDefaults,
) -> Dict[str, Any]:
output: Dict[str, Any] = {}

if request.mac_address is not None:
output["mac_address"] = request.mac_address

if request.name is not None:
output["name"] = request.name

return output


def marshal_AttachIPRequest(
request: AttachIPRequest,
defaults: ProfileDefaults,
) -> Dict[str, Any]:
output: Dict[str, Any] = {}

if request.resource is not None:
output["resource"] = marshal_CustomResource(request.resource, defaults)

return output


def marshal_Source(
request: Source,
defaults: ProfileDefaults,
Expand Down Expand Up @@ -231,6 +261,21 @@ def marshal_BookIPRequest(
if request.tags is not None:
output["tags"] = request.tags

if request.resource is not None:
output["resource"] = marshal_CustomResource(request.resource, defaults)

return output


def marshal_MoveIPRequest(
request: MoveIPRequest,
defaults: ProfileDefaults,
) -> Dict[str, Any]:
output: Dict[str, Any] = {}

if request.resource is not None:
output["resource"] = marshal_CustomResource(request.resource, defaults)

return output


Expand Down
68 changes: 68 additions & 0 deletions scaleway-async/scaleway_async/ipam/v1/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __str__(self) -> str:

class ResourceType(str, Enum, metaclass=StrEnumMeta):
UNKNOWN_TYPE = "unknown_type"
CUSTOM = "custom"
INSTANCE_SERVER = "instance_server"
INSTANCE_IP = "instance_ip"
INSTANCE_PRIVATE_NIC = "instance_private_nic"
Expand Down Expand Up @@ -94,6 +95,19 @@ class Source:
subnet_id: Optional[str]


@dataclass
class CustomResource:
mac_address: str
"""
MAC address of the custom resource.
"""

name: Optional[str]
"""
When the resource is in a Private Network, a DNS record is available to resolve the resource name.
"""


@dataclass
class IP:
id: str
Expand Down Expand Up @@ -157,6 +171,24 @@ class IP:
"""


@dataclass
class AttachIPRequest:
ip_id: str
"""
IP ID.
"""

resource: CustomResource
"""
Custom resource to be attached to the IP.
"""

region: Optional[Region]
"""
Region to target. If none is passed will use default region from the config.
"""


@dataclass
class BookIPRequest:
source: Source
Expand Down Expand Up @@ -189,6 +221,24 @@ class BookIPRequest:
Tags for the IP.
"""

resource: Optional[CustomResource]
"""
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.
"""


@dataclass
class DetachIPRequest:
ip_id: str
"""
IP ID.
"""

region: Optional[Region]
"""
Region to target. If none is passed will use default region from the config.
"""


@dataclass
class GetIPRequest:
Expand Down Expand Up @@ -294,6 +344,24 @@ class ListIPsResponse:
ips: List[IP]


@dataclass
class MoveIPRequest:
ip_id: str
"""
IP ID.
"""

region: Optional[Region]
"""
Region to target. If none is passed will use default region from the config.
"""

resource: Optional[CustomResource]
"""
Custom resource to be attached to the IP.
"""


@dataclass
class ReleaseIPRequest:
ip_id: str
Expand Down
Loading