|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 12 | +) |
| 13 | + |
| 14 | +type cwFilterDataSource struct{} |
| 15 | + |
| 16 | +func NewCloudWatchFilterDataSource() datasource.DataSource { return &cwFilterDataSource{} } |
| 17 | + |
| 18 | +type cwFilterModel struct { |
| 19 | + Struct types.String `tfsdk:"struct"` |
| 20 | + Event types.String `tfsdk:"event"` |
| 21 | + Predicates map[string][]string `tfsdk:"predicates"` |
| 22 | + |
| 23 | + Pattern types.String `tfsdk:"pattern"` |
| 24 | +} |
| 25 | + |
| 26 | +func (d *cwFilterDataSource) Metadata(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 27 | + resp.TypeName = "logstruct_cloudwatch_filter" |
| 28 | +} |
| 29 | + |
| 30 | +func (d *cwFilterDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 31 | + resp.Schema = schema.Schema{ |
| 32 | + Attributes: map[string]schema.Attribute{ |
| 33 | + "struct": schema.StringAttribute{Required: true, Description: "LogStruct struct name e.g. ActionMailer"}, |
| 34 | + "event": schema.StringAttribute{Required: true, Description: "Event value (serialized), validated against struct"}, |
| 35 | + "predicates": schema.MapAttribute{ |
| 36 | + Optional: true, |
| 37 | + ElementType: types.ListType{ElemType: types.StringType}, |
| 38 | + Description: "Additional equality predicates map[field] = [values...]", |
| 39 | + }, |
| 40 | + "pattern": schema.StringAttribute{Computed: true, Description: "Generated CloudWatch Logs filter pattern"}, |
| 41 | + }, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func (d *cwFilterDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 46 | + var data cwFilterModel |
| 47 | + diags := req.Config.Get(ctx, &data) |
| 48 | + resp.Diagnostics.Append(diags...) |
| 49 | + if resp.Diagnostics.HasError() { return } |
| 50 | + |
| 51 | + client, ok := req.ProviderData.(*MetadataClient) |
| 52 | + if !ok || client == nil { |
| 53 | + resp.Diagnostics.AddError("Provider not configured", "Missing metadata client") |
| 54 | + return |
| 55 | + } |
| 56 | + structName := data.Struct.ValueString() |
| 57 | + event := data.Event.ValueString() |
| 58 | + allowed, _, err := client.AllowedEventsForStruct(structName) |
| 59 | + if err != nil { resp.Diagnostics.AddError("Lookup error", err.Error()); return } |
| 60 | + if !contains(allowed, event) { |
| 61 | + resp.Diagnostics.AddError("Invalid event for struct", fmt.Sprintf("event %q not allowed for %s (allowed: %v)", event, structName, allowed)) |
| 62 | + return |
| 63 | + } |
| 64 | + var parts []string |
| 65 | + // add evt |
| 66 | + evtKey, ok := client.Keys["evt"] |
| 67 | + if !ok { resp.Diagnostics.AddError("Missing key", "evt key missing from exports"); return } |
| 68 | + parts = append(parts, fmt.Sprintf("$.%s = \"%s\"", evtKey, event)) |
| 69 | + // add source |
| 70 | + if src, fixed, err := client.FixedSourceForStruct(structName); err != nil { |
| 71 | + resp.Diagnostics.AddError("Lookup error", err.Error()); return |
| 72 | + } else if fixed { |
| 73 | + srcKey, ok := client.Keys["src"]; if !ok { resp.Diagnostics.AddError("Missing key", "src key missing from exports"); return } |
| 74 | + parts = append(parts, fmt.Sprintf("$.%s = \"%s\"", srcKey, src)) |
| 75 | + } |
| 76 | + // add extra predicates |
| 77 | + if data.Predicates != nil { |
| 78 | + keys := make([]string, 0, len(data.Predicates)) |
| 79 | + for k := range data.Predicates { keys = append(keys, k) } |
| 80 | + sort.Strings(keys) |
| 81 | + for _, k := range keys { |
| 82 | + values := data.Predicates[k] |
| 83 | + if len(values) == 0 { continue } |
| 84 | + keyPath := fmt.Sprintf("$.%s", k) |
| 85 | + if len(values) == 1 { |
| 86 | + parts = append(parts, fmt.Sprintf("%s = \"%s\"", keyPath, escape(values[0]))) |
| 87 | + } else { |
| 88 | + ors := make([]string, 0, len(values)) |
| 89 | + for _, v := range values { ors = append(ors, fmt.Sprintf("%s = \"%s\"", keyPath, escape(v))) } |
| 90 | + parts = append(parts, fmt.Sprintf("(%s)", strings.Join(ors, " || "))) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + pattern := fmt.Sprintf("{ %s }", strings.Join(parts, " && ")) |
| 95 | + data.Pattern = types.StringValue(pattern) |
| 96 | + |
| 97 | + diags = resp.State.Set(ctx, &data) |
| 98 | + resp.Diagnostics.Append(diags...) |
| 99 | +} |
| 100 | + |
| 101 | +func contains(arr []string, s string) bool { for _, v := range arr { if v == s { return true } }; return false } |
| 102 | +func escape(s string) string { return strings.ReplaceAll(s, "\"", "\\\"") } |
| 103 | + |
0 commit comments