1+ """
2+ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+ SPDX-License-Identifier: MIT-0
4+ """
5+
6+ from cfn_policy_validator import client
7+ from cfn_policy_validator .application_error import ApplicationError
8+
9+ def get_dashboard_created_time (region , resource_name , resource = None ):
10+ return get_dashboard_attribute (region , resource_name , 'CreatedTimestamp' )
11+
12+ def get_dashboard_status (region , resource_name , resource = None ):
13+ return get_dashboard_attribute (region , resource_name , 'Status' )
14+
15+ def get_dashboard_type (region , resource_name , resource = None ):
16+ return get_dashboard_attribute (region , resource_name , 'Type' )
17+
18+ def get_dashboard_updated_time (region , resource_name , resource = None ):
19+ return get_dashboard_attribute (region , resource_name , 'UpdatedTimestamp' )
20+
21+ def get_eventdatastore_arn (arn_pattern , resource_name , resource , visited_nodes , region ):
22+ return get_eventdatastore_arn_from_client (region , resource_name )
23+
24+ def get_eventdatastore_created_time (region , resource_name , resource = None ):
25+ return get_eventdatastore_attribute (region , resource_name , 'CreatedTimestamp' )
26+
27+ def get_eventdatastore_status (region , resource_name , resource = None ):
28+ return get_eventdatastore_attribute (region , resource_name , 'Status' )
29+
30+ def get_eventdatastore_updated_time (region , resource_name , resource = None ):
31+ return get_eventdatastore_attribute (region , resource_name , 'UpdatedTimestamp' )
32+
33+
34+ def get_dashboard_attribute (region , resource_name , attribute ):
35+ supported_attributes = ['Type' , 'CreatedTimestamp' , 'Status' , 'UpdatedTimestamp' ]
36+ cloudtrail_client = client .build ('cloudtrail' , region )
37+ try :
38+ if attribute not in supported_attributes :
39+ raise ApplicationError (f"Attribute { attribute } is not supported. Supported attributes are { supported_attributes } " )
40+ response = cloudtrail_client .get_dashboard (
41+ DashboardId = resource_name
42+ )
43+ return response [attribute ]
44+ except Exception as e :
45+ raise ApplicationError (f"Error: { e } " )
46+
47+ def get_eventdatastore_arn_from_client (region , resource_name ):
48+ cloudtrail_client = client .build ('cloudtrail' , region )
49+ next_token = None
50+ client_config = {
51+ 'MaxResults' : 25
52+ }
53+
54+ while True :
55+ if next_token :
56+ client_config ['NextToken' ] = nextToken
57+ response = cloudtrail_client .list_event_data_stores (** client_config )
58+ for eventdatastore in response ['EventDataStores' ]:
59+ if eventdatastore ['Name' ] == resource_name :
60+ return eventdatastore ['EventDataStoreArn' ]
61+ nextToken = response .get ('NextToken' )
62+ if not nextToken :
63+ break
64+ raise ApplicationError (f"CloudTrail Event Data Store { resource_name } not found" )
65+
66+
67+ def get_eventdatastore_attribute (region , resource_name , attribute ):
68+ cloudtrail_client = client .build ('cloudtrail' , region )
69+ supported_attributes = ['CreatedTimestamp' , 'Status' , 'UpdatedTimestamp' ]
70+
71+ if attribute not in supported_attributes :
72+ raise ApplicationError (f"Attribute { attribute } is not supported. Supported attributes are { supported_attributes } " )
73+
74+ arn = get_eventdatastore_arn_from_client (region , resource_name )
75+ event_data_store_response = cloudtrail_client .get_event_data_store (EventDataStore = arn )
76+ ret = event_data_store_response [attribute ]
77+ return ret
78+
79+
0 commit comments