Skip to content

feat(block): add exporting snapshot to s3 #537

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
May 30, 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
4 changes: 4 additions & 0 deletions scaleway-async/scaleway_async/block/v1alpha1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
from .types import CreateVolumeRequest
from .types import DeleteSnapshotRequest
from .types import DeleteVolumeRequest
from .types import ExportSnapshotToObjectStorageRequest
from .types import GetSnapshotRequest
from .types import GetVolumeRequest
from .types import ImportSnapshotFromObjectStorageRequest
from .types import ImportSnapshotFromS3Request
from .types import ListSnapshotsRequest
from .types import ListSnapshotsResponse
Expand Down Expand Up @@ -58,8 +60,10 @@
"CreateVolumeRequest",
"DeleteSnapshotRequest",
"DeleteVolumeRequest",
"ExportSnapshotToObjectStorageRequest",
"GetSnapshotRequest",
"GetVolumeRequest",
"ImportSnapshotFromObjectStorageRequest",
"ImportSnapshotFromS3Request",
"ListSnapshotsRequest",
"ListSnapshotsResponse",
Expand Down
109 changes: 109 additions & 0 deletions scaleway-async/scaleway_async/block/v1alpha1/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
CreateVolumeRequest,
CreateVolumeRequestFromEmpty,
CreateVolumeRequestFromSnapshot,
ExportSnapshotToObjectStorageRequest,
ImportSnapshotFromObjectStorageRequest,
ImportSnapshotFromS3Request,
ListSnapshotsResponse,
ListVolumeTypesResponse,
Expand All @@ -42,6 +44,8 @@
unmarshal_ListVolumesResponse,
marshal_CreateSnapshotRequest,
marshal_CreateVolumeRequest,
marshal_ExportSnapshotToObjectStorageRequest,
marshal_ImportSnapshotFromObjectStorageRequest,
marshal_ImportSnapshotFromS3Request,
marshal_UpdateSnapshotRequest,
marshal_UpdateVolumeRequest,
Expand Down Expand Up @@ -668,6 +672,7 @@ async def import_snapshot_from_s3(
:param tags: List of tags assigned to the snapshot.
:param size: Size of the snapshot.
:return: :class:`Snapshot <Snapshot>`
:deprecated

Usage:
::
Expand Down Expand Up @@ -701,6 +706,110 @@ async def import_snapshot_from_s3(
self._throw_on_error(res)
return unmarshal_Snapshot(res.json())

async def import_snapshot_from_object_storage(
self,
*,
bucket: str,
key: str,
name: str,
zone: Optional[Zone] = None,
project_id: Optional[str] = None,
tags: Optional[List[str]] = None,
size: Optional[int] = None,
) -> Snapshot:
"""
Import a snapshot from a Scaleway Object Storage bucket.
The bucket must contain a QCOW2 image.
The bucket can be imported into any Availability Zone as long as it is in the same region as the bucket.
:param bucket: Scaleway Object Storage bucket where the object is stored.
:param key: The object key inside the given bucket.
:param name: Name of the snapshot.
:param zone: Zone to target. If none is passed will use default zone from the config.
:param project_id: UUID of the Project to which the volume and the snapshot belong.
:param tags: List of tags assigned to the snapshot.
:param size: Size of the snapshot.
:return: :class:`Snapshot <Snapshot>`

Usage:
::

result = await api.import_snapshot_from_object_storage(
bucket="example",
key="example",
name="example",
)
"""

param_zone = validate_path_param("zone", zone or self.client.default_zone)

res = self._request(
"POST",
f"/block/v1alpha1/zones/{param_zone}/snapshots/import-from-object-storage",
body=marshal_ImportSnapshotFromObjectStorageRequest(
ImportSnapshotFromObjectStorageRequest(
bucket=bucket,
key=key,
name=name,
zone=zone,
project_id=project_id,
tags=tags,
size=size,
),
self.client,
),
)

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

async def export_snapshot_to_object_storage(
self,
*,
snapshot_id: str,
bucket: str,
key: str,
zone: Optional[Zone] = None,
) -> Snapshot:
"""
Export a snapshot to a Scaleway Object Storage bucket.
The snapshot is exported in QCOW2 format.
The snapshot must not be in transient state.
:param snapshot_id: UUID of the snapshot.
:param bucket: Scaleway Object Storage bucket where the object is stored.
:param key: The object key inside the given bucket.
:param zone: Zone to target. If none is passed will use default zone from the config.
:return: :class:`Snapshot <Snapshot>`

Usage:
::

result = await api.export_snapshot_to_object_storage(
snapshot_id="example",
bucket="example",
key="example",
)
"""

param_zone = validate_path_param("zone", zone or self.client.default_zone)
param_snapshot_id = validate_path_param("snapshot_id", snapshot_id)

res = self._request(
"POST",
f"/block/v1alpha1/zones/{param_zone}/snapshots/{param_snapshot_id}/export-to-object-storage",
body=marshal_ExportSnapshotToObjectStorageRequest(
ExportSnapshotToObjectStorageRequest(
snapshot_id=snapshot_id,
bucket=bucket,
key=key,
zone=zone,
),
self.client,
),
)

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

async def delete_snapshot(
self,
*,
Expand Down
1 change: 1 addition & 0 deletions scaleway-async/scaleway_async/block/v1alpha1/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
SNAPSHOT_TRANSIENT_STATUSES: List[SnapshotStatus] = [
SnapshotStatus.CREATING,
SnapshotStatus.DELETING,
SnapshotStatus.EXPORTING,
]
"""
Lists transient statutes of the enum :class:`SnapshotStatus <SnapshotStatus>`.
Expand Down
44 changes: 44 additions & 0 deletions scaleway-async/scaleway_async/block/v1alpha1/marshalling.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
CreateVolumeRequestFromEmpty,
CreateVolumeRequestFromSnapshot,
CreateVolumeRequest,
ExportSnapshotToObjectStorageRequest,
ImportSnapshotFromObjectStorageRequest,
ImportSnapshotFromS3Request,
UpdateSnapshotRequest,
UpdateVolumeRequest,
Expand Down Expand Up @@ -442,6 +444,48 @@ def marshal_CreateVolumeRequest(
return output


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

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

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

return output


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

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

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

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

if request.project_id is not None:
output["project_id"] = request.project_id or defaults.default_project_id

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

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

return output


def marshal_ImportSnapshotFromS3Request(
request: ImportSnapshotFromS3Request,
defaults: ProfileDefaults,
Expand Down
62 changes: 62 additions & 0 deletions scaleway-async/scaleway_async/block/v1alpha1/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class SnapshotStatus(str, Enum, metaclass=StrEnumMeta):
DELETED = "deleted"
IN_USE = "in_use"
LOCKED = "locked"
EXPORTING = "exporting"

def __str__(self) -> str:
return str(self.value)
Expand Down Expand Up @@ -433,6 +434,29 @@ class DeleteVolumeRequest:
"""


@dataclass
class ExportSnapshotToObjectStorageRequest:
snapshot_id: str
"""
UUID of the snapshot.
"""

bucket: str
"""
Scaleway Object Storage bucket where the object is stored.
"""

key: str
"""
The object key inside the given bucket.
"""

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


@dataclass
class GetSnapshotRequest:
snapshot_id: str
Expand All @@ -459,6 +483,44 @@ class GetVolumeRequest:
"""


@dataclass
class ImportSnapshotFromObjectStorageRequest:
bucket: str
"""
Scaleway Object Storage bucket where the object is stored.
"""

key: str
"""
The object key inside the given bucket.
"""

name: str
"""
Name of the snapshot.
"""

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

project_id: Optional[str]
"""
UUID of the Project to which the volume and the snapshot belong.
"""

tags: Optional[List[str]]
"""
List of tags assigned to the snapshot.
"""

size: Optional[int]
"""
Size of the snapshot.
"""


@dataclass
class ImportSnapshotFromS3Request:
bucket: str
Expand Down
4 changes: 4 additions & 0 deletions scaleway/scaleway/block/v1alpha1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
from .types import CreateVolumeRequest
from .types import DeleteSnapshotRequest
from .types import DeleteVolumeRequest
from .types import ExportSnapshotToObjectStorageRequest
from .types import GetSnapshotRequest
from .types import GetVolumeRequest
from .types import ImportSnapshotFromObjectStorageRequest
from .types import ImportSnapshotFromS3Request
from .types import ListSnapshotsRequest
from .types import ListSnapshotsResponse
Expand Down Expand Up @@ -58,8 +60,10 @@
"CreateVolumeRequest",
"DeleteSnapshotRequest",
"DeleteVolumeRequest",
"ExportSnapshotToObjectStorageRequest",
"GetSnapshotRequest",
"GetVolumeRequest",
"ImportSnapshotFromObjectStorageRequest",
"ImportSnapshotFromS3Request",
"ListSnapshotsRequest",
"ListSnapshotsResponse",
Expand Down
Loading