Coverage for src/extratools_cloud/aws/cloudcontrol.py: 0%
17 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-11 19:22 -0700
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-11 19:22 -0700
1import json
2from collections.abc import Iterable
4import boto3
5from botocore.client import BaseClient
6from extratools_core.crudl import CRUDLDict
7from extratools_core.json import JsonDict
9default_client: BaseClient = boto3.client("cloudcontrol")
12def get_resource_dict(
13 resource_type: str,
14 client: BaseClient | None = None,
15) -> CRUDLDict[str, JsonDict]:
16 client = client or default_client
18 def read_func(identifier: str) -> JsonDict:
19 return json.loads(client.get_resource(
20 TypeName=resource_type,
21 Identifier=identifier,
22 )["ResourceDescription"]["Properties"])
24 def list_func() -> Iterable[tuple[str, JsonDict]]:
25 paginator = client.get_paginator("list_resources")
26 for page in paginator.paginate(
27 TypeName=resource_type,
28 ):
29 for resource in page.get("ResourceDescriptions", []):
30 yield (resource["Identifier"], json.loads(resource["Properties"]))
32 return CRUDLDict[str, JsonDict](
33 create_func=lambda _, desired_state: client.create_resource(
34 TypeName=resource_type,
35 DesiredState=json.dumps(desired_state),
36 ),
37 read_func=read_func,
38 update_func=lambda identifier, patch: client.update_resource(
39 TypeName=resource_type,
40 Identifier=identifier,
41 PatchDocument=json.dumps(patch),
42 ),
43 delete_func=lambda identifier: client.delete_resource(
44 TypeName=resource_type,
45 Identifier=identifier,
46 ),
47 list_func=list_func,
48 )