@@ -8,8 +8,10 @@ import (
88 "github.com/aws/aws-sdk-go-v2/service/cloudtrail"
99 "github.com/aws/aws-sdk-go-v2/service/cloudtrail/types"
1010 "github.com/cloudquery/cloudquery/plugins/source/aws/client"
11+ "github.com/cloudquery/cloudquery/plugins/source/aws/client/tableoptions"
1112 "github.com/cloudquery/plugin-sdk/v2/schema"
1213 "github.com/cloudquery/plugin-sdk/v2/transformers"
14+ "github.com/mitchellh/hashstructure/v2"
1315)
1416
1517const tableName = "aws_cloudtrail_events"
@@ -45,51 +47,71 @@ func Events() *schema.Table {
4547func fetchCloudtrailEvents (ctx context.Context , meta schema.ClientMeta , parent * schema.Resource , res chan <- any ) error {
4648 cl := meta .(* client.Client )
4749 svc := cl .Services ().Cloudtrail
48- le := & cloudtrail.LookupEventsInput {}
50+
51+ allConfigs := []tableoptions.CustomLookupEventsOpts {{}}
52+ noTableConfig := true
4953 if cl .Spec .TableOptions .CloudTrailEvents != nil {
50- le = & cl .Spec .TableOptions .CloudTrailEvents .LookupEventsOpts .LookupEventsInput
54+ allConfigs = cl .Spec .TableOptions .CloudTrailEvents .LookupEventsOpts
55+ noTableConfig = false
5156 }
57+ for _ , w := range allConfigs {
58+ le := w .LookupEventsInput
5259
53- if cl .Backend != nil {
54- value , err := cl .Backend .Get (ctx , tableName , cl .ID ())
55- if err != nil {
56- return fmt .Errorf ("failed to retrieve state from backend: %w" , err )
57- }
58-
59- if value != "" {
60- date , err := time .Parse (time .RFC3339Nano , value )
60+ var backendKey string
61+ if cl .Backend != nil {
62+ // Retrieve the last event time from the backend for this table option config.
63+ // We use a hash of the config as the key, so changing the config will cause a full refresh.
64+ hash , err := hashstructure .Hash (le , hashstructure .FormatV2 , nil )
6165 if err != nil {
62- return fmt .Errorf ("retrieved invalid state value: %q %w" , value , err )
66+ return err
67+ }
68+ backendKey = fmt .Sprintf ("%s-%d" , cl .ID (), hash )
69+ if noTableConfig {
70+ // for backwards-compatibility, default to client id if there is no table config
71+ backendKey = cl .ID ()
72+ }
73+ value , err := cl .Backend .Get (ctx , tableName , backendKey )
74+ if err != nil {
75+ return fmt .Errorf ("failed to retrieve state from backend: %w" , err )
76+ }
77+
78+ if value != "" {
79+ date , err := time .Parse (time .RFC3339Nano , value )
80+ if err != nil {
81+ return fmt .Errorf ("retrieved invalid state value: %q %w" , value , err )
82+ }
83+ le .StartTime = & date
6384 }
64- le .StartTime = & date
65- }
66- }
67- var lastEventTime * time.Time
68- // var err error
69- paginator := cloudtrail .NewLookupEventsPaginator (svc , le )
70- for paginator .HasMorePages () {
71- page , err := paginator .NextPage (ctx , func (options * cloudtrail.Options ) {
72- options .Region = cl .Region
73- })
74- if err != nil {
75- return err
7685 }
77- res <- page .Events
7886
79- // Retrieve the timestamp from the latest event
80- for _ , event := range page .Events {
81- if lastEventTime == nil {
82- lastEventTime = event .EventTime
83- continue
87+ var lastEventTime * time.Time
88+ // var err error
89+ paginator := cloudtrail .NewLookupEventsPaginator (svc , & le )
90+ for paginator .HasMorePages () {
91+ page , err := paginator .NextPage (ctx , func (options * cloudtrail.Options ) {
92+ options .Region = cl .Region
93+ })
94+ if err != nil {
95+ return err
8496 }
85- if event .EventTime .After (* lastEventTime ) {
86- lastEventTime = event .EventTime
97+ res <- page .Events
98+
99+ // Retrieve the timestamp from the latest event
100+ for _ , event := range page .Events {
101+ if lastEventTime == nil {
102+ lastEventTime = event .EventTime
103+ continue
104+ }
105+ if event .EventTime .After (* lastEventTime ) {
106+ lastEventTime = event .EventTime
107+ }
87108 }
88109 }
89- }
90110
91- if cl .Backend != nil && lastEventTime != nil {
92- return cl .Backend .Set (ctx , tableName , cl .ID (), lastEventTime .Format (time .RFC3339Nano ))
111+ if cl .Backend != nil && lastEventTime != nil {
112+ return cl .Backend .Set (ctx , tableName , backendKey , lastEventTime .Format (time .RFC3339Nano ))
113+ }
93114 }
115+
94116 return nil
95117}
0 commit comments