Skip to content

Commit 8e4fba2

Browse files
authored
feat(key_manager): add key scheduled deletion and restoration (#1029)
1 parent 7841bde commit 8e4fba2

File tree

8 files changed

+154
-4
lines changed

8 files changed

+154
-4
lines changed

scaleway-async/scaleway_async/key_manager/v1alpha1/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from .types import ListKeysResponse
3030
from .types import ProtectKeyRequest
3131
from .types import PublicKey
32+
from .types import RestoreKeyRequest
3233
from .types import RotateKeyRequest
3334
from .types import SignRequest
3435
from .types import SignResponse
@@ -68,6 +69,7 @@
6869
"ListKeysResponse",
6970
"ProtectKeyRequest",
7071
"PublicKey",
72+
"RestoreKeyRequest",
7173
"RotateKeyRequest",
7274
"SignRequest",
7375
"SignResponse",

scaleway-async/scaleway_async/key_manager/v1alpha1/api.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ async def disable_key(
452452
async def list_keys(
453453
self,
454454
*,
455+
scheduled_for_deletion: bool,
455456
region: Optional[ScwRegion] = None,
456457
organization_id: Optional[str] = None,
457458
project_id: Optional[str] = None,
@@ -465,6 +466,7 @@ async def list_keys(
465466
"""
466467
List keys.
467468
Retrieve a list of keys across all Projects in an Organization or within a specific Project. You must specify the `region`, and either the `organization_id` or the `project_id`.
469+
:param scheduled_for_deletion: Filter keys based on their deletion status. By default, only keys not scheduled for deletion are returned in the output.
468470
:param region: Region to target. If none is passed will use default region from the config.
469471
:param organization_id: (Optional) Filter by Organization ID.
470472
:param project_id: (Optional) Filter by Project ID.
@@ -479,7 +481,9 @@ async def list_keys(
479481
Usage:
480482
::
481483
482-
result = await api.list_keys()
484+
result = await api.list_keys(
485+
scheduled_for_deletion=False,
486+
)
483487
"""
484488

485489
param_region = validate_path_param(
@@ -497,6 +501,7 @@ async def list_keys(
497501
"page": page,
498502
"page_size": page_size or self.client.default_page_size,
499503
"project_id": project_id or self.client.default_project_id,
504+
"scheduled_for_deletion": scheduled_for_deletion,
500505
"tags": tags,
501506
"usage": usage,
502507
},
@@ -508,6 +513,7 @@ async def list_keys(
508513
async def list_keys_all(
509514
self,
510515
*,
516+
scheduled_for_deletion: bool,
511517
region: Optional[ScwRegion] = None,
512518
organization_id: Optional[str] = None,
513519
project_id: Optional[str] = None,
@@ -521,6 +527,7 @@ async def list_keys_all(
521527
"""
522528
List keys.
523529
Retrieve a list of keys across all Projects in an Organization or within a specific Project. You must specify the `region`, and either the `organization_id` or the `project_id`.
530+
:param scheduled_for_deletion: Filter keys based on their deletion status. By default, only keys not scheduled for deletion are returned in the output.
524531
:param region: Region to target. If none is passed will use default region from the config.
525532
:param organization_id: (Optional) Filter by Organization ID.
526533
:param project_id: (Optional) Filter by Project ID.
@@ -535,14 +542,17 @@ async def list_keys_all(
535542
Usage:
536543
::
537544
538-
result = await api.list_keys_all()
545+
result = await api.list_keys_all(
546+
scheduled_for_deletion=False,
547+
)
539548
"""
540549

541550
return await fetch_all_pages_async(
542551
type=ListKeysResponse,
543552
key="keys",
544553
fetcher=self.list_keys,
545554
args={
555+
"scheduled_for_deletion": scheduled_for_deletion,
546556
"region": region,
547557
"organization_id": organization_id,
548558
"project_id": project_id,
@@ -876,3 +886,38 @@ async def delete_key_material(
876886
)
877887

878888
self._throw_on_error(res)
889+
890+
async def restore_key(
891+
self,
892+
*,
893+
key_id: str,
894+
region: Optional[ScwRegion] = None,
895+
) -> Key:
896+
"""
897+
Restore a key.
898+
Restore a key and all its rotations scheduled for deletion specified by the `region` and `key_id` parameters.
899+
:param key_id:
900+
:param region: Region to target. If none is passed will use default region from the config.
901+
:return: :class:`Key <Key>`
902+
903+
Usage:
904+
::
905+
906+
result = await api.restore_key(
907+
key_id="example",
908+
)
909+
"""
910+
911+
param_region = validate_path_param(
912+
"region", region or self.client.default_region
913+
)
914+
param_key_id = validate_path_param("key_id", key_id)
915+
916+
res = self._request(
917+
"POST",
918+
f"/key-manager/v1alpha1/regions/{param_region}/keys/{param_key_id}/restore",
919+
body={},
920+
)
921+
922+
self._throw_on_error(res)
923+
return unmarshal_Key(res.json())

scaleway-async/scaleway_async/key_manager/v1alpha1/marshalling.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ def unmarshal_Key(data: Any) -> Key:
169169
else:
170170
args["rotation_policy"] = None
171171

172+
field = data.get("deletion_requested_at", None)
173+
if field is not None:
174+
args["deletion_requested_at"] = (
175+
parser.isoparse(field) if isinstance(field, str) else field
176+
)
177+
else:
178+
args["deletion_requested_at"] = None
179+
172180
return Key(**args)
173181

174182

scaleway-async/scaleway_async/key_manager/v1alpha1/types.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ class Key:
201201
Key rotation policy.
202202
"""
203203

204+
deletion_requested_at: Optional[datetime]
205+
"""
206+
Returns the time at which deletion was requested.
207+
"""
208+
204209

205210
@dataclass
206211
class CreateKeyRequest:
@@ -482,6 +487,11 @@ class ImportKeyMaterialRequest:
482487

483488
@dataclass
484489
class ListKeysRequest:
490+
scheduled_for_deletion: bool
491+
"""
492+
Filter keys based on their deletion status. By default, only keys not scheduled for deletion are returned in the output.
493+
"""
494+
485495
region: Optional[ScwRegion]
486496
"""
487497
Region to target. If none is passed will use default region from the config.
@@ -550,6 +560,16 @@ class PublicKey:
550560
pem: str
551561

552562

563+
@dataclass
564+
class RestoreKeyRequest:
565+
key_id: str
566+
567+
region: Optional[ScwRegion]
568+
"""
569+
Region to target. If none is passed will use default region from the config.
570+
"""
571+
572+
553573
@dataclass
554574
class RotateKeyRequest:
555575
key_id: str

scaleway/scaleway/key_manager/v1alpha1/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from .types import ListKeysResponse
3030
from .types import ProtectKeyRequest
3131
from .types import PublicKey
32+
from .types import RestoreKeyRequest
3233
from .types import RotateKeyRequest
3334
from .types import SignRequest
3435
from .types import SignResponse
@@ -68,6 +69,7 @@
6869
"ListKeysResponse",
6970
"ProtectKeyRequest",
7071
"PublicKey",
72+
"RestoreKeyRequest",
7173
"RotateKeyRequest",
7274
"SignRequest",
7375
"SignResponse",

scaleway/scaleway/key_manager/v1alpha1/api.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ def disable_key(
452452
def list_keys(
453453
self,
454454
*,
455+
scheduled_for_deletion: bool,
455456
region: Optional[ScwRegion] = None,
456457
organization_id: Optional[str] = None,
457458
project_id: Optional[str] = None,
@@ -465,6 +466,7 @@ def list_keys(
465466
"""
466467
List keys.
467468
Retrieve a list of keys across all Projects in an Organization or within a specific Project. You must specify the `region`, and either the `organization_id` or the `project_id`.
469+
:param scheduled_for_deletion: Filter keys based on their deletion status. By default, only keys not scheduled for deletion are returned in the output.
468470
:param region: Region to target. If none is passed will use default region from the config.
469471
:param organization_id: (Optional) Filter by Organization ID.
470472
:param project_id: (Optional) Filter by Project ID.
@@ -479,7 +481,9 @@ def list_keys(
479481
Usage:
480482
::
481483
482-
result = api.list_keys()
484+
result = api.list_keys(
485+
scheduled_for_deletion=False,
486+
)
483487
"""
484488

485489
param_region = validate_path_param(
@@ -497,6 +501,7 @@ def list_keys(
497501
"page": page,
498502
"page_size": page_size or self.client.default_page_size,
499503
"project_id": project_id or self.client.default_project_id,
504+
"scheduled_for_deletion": scheduled_for_deletion,
500505
"tags": tags,
501506
"usage": usage,
502507
},
@@ -508,6 +513,7 @@ def list_keys(
508513
def list_keys_all(
509514
self,
510515
*,
516+
scheduled_for_deletion: bool,
511517
region: Optional[ScwRegion] = None,
512518
organization_id: Optional[str] = None,
513519
project_id: Optional[str] = None,
@@ -521,6 +527,7 @@ def list_keys_all(
521527
"""
522528
List keys.
523529
Retrieve a list of keys across all Projects in an Organization or within a specific Project. You must specify the `region`, and either the `organization_id` or the `project_id`.
530+
:param scheduled_for_deletion: Filter keys based on their deletion status. By default, only keys not scheduled for deletion are returned in the output.
524531
:param region: Region to target. If none is passed will use default region from the config.
525532
:param organization_id: (Optional) Filter by Organization ID.
526533
:param project_id: (Optional) Filter by Project ID.
@@ -535,14 +542,17 @@ def list_keys_all(
535542
Usage:
536543
::
537544
538-
result = api.list_keys_all()
545+
result = api.list_keys_all(
546+
scheduled_for_deletion=False,
547+
)
539548
"""
540549

541550
return fetch_all_pages(
542551
type=ListKeysResponse,
543552
key="keys",
544553
fetcher=self.list_keys,
545554
args={
555+
"scheduled_for_deletion": scheduled_for_deletion,
546556
"region": region,
547557
"organization_id": organization_id,
548558
"project_id": project_id,
@@ -876,3 +886,38 @@ def delete_key_material(
876886
)
877887

878888
self._throw_on_error(res)
889+
890+
def restore_key(
891+
self,
892+
*,
893+
key_id: str,
894+
region: Optional[ScwRegion] = None,
895+
) -> Key:
896+
"""
897+
Restore a key.
898+
Restore a key and all its rotations scheduled for deletion specified by the `region` and `key_id` parameters.
899+
:param key_id:
900+
:param region: Region to target. If none is passed will use default region from the config.
901+
:return: :class:`Key <Key>`
902+
903+
Usage:
904+
::
905+
906+
result = api.restore_key(
907+
key_id="example",
908+
)
909+
"""
910+
911+
param_region = validate_path_param(
912+
"region", region or self.client.default_region
913+
)
914+
param_key_id = validate_path_param("key_id", key_id)
915+
916+
res = self._request(
917+
"POST",
918+
f"/key-manager/v1alpha1/regions/{param_region}/keys/{param_key_id}/restore",
919+
body={},
920+
)
921+
922+
self._throw_on_error(res)
923+
return unmarshal_Key(res.json())

scaleway/scaleway/key_manager/v1alpha1/marshalling.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ def unmarshal_Key(data: Any) -> Key:
169169
else:
170170
args["rotation_policy"] = None
171171

172+
field = data.get("deletion_requested_at", None)
173+
if field is not None:
174+
args["deletion_requested_at"] = (
175+
parser.isoparse(field) if isinstance(field, str) else field
176+
)
177+
else:
178+
args["deletion_requested_at"] = None
179+
172180
return Key(**args)
173181

174182

scaleway/scaleway/key_manager/v1alpha1/types.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ class Key:
201201
Key rotation policy.
202202
"""
203203

204+
deletion_requested_at: Optional[datetime]
205+
"""
206+
Returns the time at which deletion was requested.
207+
"""
208+
204209

205210
@dataclass
206211
class CreateKeyRequest:
@@ -482,6 +487,11 @@ class ImportKeyMaterialRequest:
482487

483488
@dataclass
484489
class ListKeysRequest:
490+
scheduled_for_deletion: bool
491+
"""
492+
Filter keys based on their deletion status. By default, only keys not scheduled for deletion are returned in the output.
493+
"""
494+
485495
region: Optional[ScwRegion]
486496
"""
487497
Region to target. If none is passed will use default region from the config.
@@ -550,6 +560,16 @@ class PublicKey:
550560
pem: str
551561

552562

563+
@dataclass
564+
class RestoreKeyRequest:
565+
key_id: str
566+
567+
region: Optional[ScwRegion]
568+
"""
569+
Region to target. If none is passed will use default region from the config.
570+
"""
571+
572+
553573
@dataclass
554574
class RotateKeyRequest:
555575
key_id: str

0 commit comments

Comments
 (0)