@@ -58,24 +58,39 @@ func (d *cwFilterDataSource) Read(ctx context.Context, req datasource.ReadReques
5858 resp .Diagnostics .AddError ("Provider not configured" , "Missing metadata client" )
5959 return
6060 }
61+ pattern , err := generatePattern (client , data )
62+ if err != nil {
63+ resp .Diagnostics .AddError ("Pattern generation failed" , err .Error ())
64+ return
65+ }
66+ data .Pattern = types .StringValue (pattern )
67+
68+ diags = resp .State .Set (ctx , & data )
69+ resp .Diagnostics .Append (diags ... )
70+ }
71+
72+ func contains (arr []string , s string ) bool { for _ , v := range arr { if v == s { return true } }; return false }
73+ func escape (s string ) string { return strings .ReplaceAll (s , "\" " , "\\ \" " ) }
74+
75+ // generatePattern builds a CloudWatch filter pattern for the given model using the catalog client.
76+ func generatePattern (client * MetadataClient , data cwFilterModel ) (string , error ) {
6177 structName := data .Struct .ValueString ()
6278 event := data .Event .ValueString ()
6379 allowed , _ , err := client .AllowedEventsForStruct (structName )
64- if err != nil { resp . Diagnostics . AddError ( "Lookup error" , err . Error ()); return }
80+ if err != nil { return "" , err }
6581 if ! contains (allowed , event ) {
66- resp .Diagnostics .AddError ("Invalid event for struct" , fmt .Sprintf ("event %q not allowed for %s (allowed: %v)" , event , structName , allowed ))
67- return
82+ return "" , fmt .Errorf ("event %q not allowed for %s (allowed: %v)" , event , structName , allowed )
6883 }
6984 var parts []string
7085 // add evt
71- evtKey , ok := client .Keys ["evt " ]
72- if ! ok { resp . Diagnostics . AddError ( "Missing key" , "evt key missing from exports" ); return }
86+ evtKey , ok := client .Keys ["event " ]
87+ if ! ok { return "" , fmt . Errorf ( "evt key missing from exports" ) }
7388 parts = append (parts , fmt .Sprintf ("$.%s = \" %s\" " , evtKey , event ))
7489 // add source
7590 if src , fixed , err := client .FixedSourceForStruct (structName ); err != nil {
76- resp . Diagnostics . AddError ( "Lookup error" , err . Error ()); return
91+ return "" , err
7792 } else if fixed {
78- srcKey , ok := client .Keys ["src " ]; if ! ok { resp . Diagnostics . AddError ( "Missing key" , "src key missing from exports" ); return }
93+ srcKey , ok := client .Keys ["source " ]; if ! ok { return "" , fmt . Errorf ( "src key missing from exports" ) }
7994 parts = append (parts , fmt .Sprintf ("$.%s = \" %s\" " , srcKey , src ))
8095 }
8196 // add extra predicates
@@ -96,12 +111,5 @@ func (d *cwFilterDataSource) Read(ctx context.Context, req datasource.ReadReques
96111 }
97112 }
98113 }
99- pattern := fmt .Sprintf ("{ %s }" , strings .Join (parts , " && " ))
100- data .Pattern = types .StringValue (pattern )
101-
102- diags = resp .State .Set (ctx , & data )
103- resp .Diagnostics .Append (diags ... )
114+ return fmt .Sprintf ("{ %s }" , strings .Join (parts , " && " )), nil
104115}
105-
106- func contains (arr []string , s string ) bool { for _ , v := range arr { if v == s { return true } }; return false }
107- func escape (s string ) string { return strings .ReplaceAll (s , "\" " , "\\ \" " ) }
0 commit comments