Coverage for src/extratools_cloud/aws/cloud_control.py: 0%

17 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-06-11 21:41 -0700

1import json 

2from collections.abc import Iterable 

3 

4import boto3 

5from botocore.client import BaseClient 

6from extratools_core.crudl import CRUDLDict 

7from extratools_core.json import JsonDict 

8 

9default_client: BaseClient = boto3.client("cloudcontrol") 

10 

11 

12def get_resource_dict( 

13 resource_type: str, 

14 *, 

15 client: BaseClient | None = None, 

16) -> CRUDLDict[str, JsonDict]: 

17 client = client or default_client 

18 

19 # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudcontrol.html 

20 

21 def read_func(identifier: str) -> JsonDict: 

22 return json.loads(client.get_resource( 

23 TypeName=resource_type, 

24 Identifier=identifier, 

25 )["ResourceDescription"]["Properties"]) 

26 

27 # Resource model may be treated as a special filter. 

28 # It must be specified for certain resource types, 

29 # yet must be unspecified for certain other resource types. 

30 # https://docs.aws.amazon.com/cloudcontrolapi/latest/userguide/resource-operations-list.html#resource-operations-list-containers 

31 def list_func(resource_model: JsonDict | None) -> Iterable[tuple[str, JsonDict]]: 

32 paginator = client.get_paginator("list_resources") 

33 for page in paginator.paginate( 

34 TypeName=resource_type, 

35 **({} if resource_model is None else dict( 

36 ResourceModel=json.dumps(resource_model), 

37 )), 

38 ): 

39 for resource in page.get("ResourceDescriptions", []): 

40 yield (resource["Identifier"], json.loads(resource["Properties"])) 

41 

42 return CRUDLDict[str, JsonDict]( 

43 create_func=lambda _, desired_state: client.create_resource( 

44 TypeName=resource_type, 

45 DesiredState=json.dumps(desired_state), 

46 ), 

47 read_func=read_func, 

48 update_func=lambda identifier, patch: client.update_resource( 

49 TypeName=resource_type, 

50 Identifier=identifier, 

51 PatchDocument=json.dumps(patch), 

52 ), 

53 delete_func=lambda identifier: client.delete_resource( 

54 TypeName=resource_type, 

55 Identifier=identifier, 

56 ), 

57 list_func=list_func, 

58 )