Skip to content

Commit be278e8

Browse files
committed
added tests and ci workflow
1 parent a11d96a commit be278e8

File tree

5 files changed

+89
-15
lines changed

5 files changed

+89
-15
lines changed

.github/workflows/test.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
with:
15+
fetch-depth: 0
16+
- uses: actions/setup-go@v5
17+
with:
18+
go-version: '1.22'
19+
- name: Build
20+
run: go build ./...
21+
- name: Vet
22+
run: go vet ./...
23+
- name: Test
24+
run: go test ./...
25+

pkg/provider/datasource_cw_filter.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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, "\"", "\\\"") }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package provider
2+
3+
import (
4+
"strings"
5+
"testing"
6+
"github.com/hashicorp/terraform-plugin-framework/types"
7+
)
8+
9+
// minimal harness to exercise Read logic without full TF runtime
10+
func TestCloudWatchFilter_BuildsPattern(t *testing.T) {
11+
client, err := NewMetadataClient()
12+
if err != nil { t.Fatalf("client: %v", err) }
13+
// Build inputs directly and call helper
14+
cfg := cwFilterModel{Struct: types.StringValue("ActionMailer"), Event: types.StringValue("delivered"), Predicates: map[string][]string{"mailer_class": {"UserMailer"}}}
15+
p, err := generatePattern(client, cfg)
16+
if err != nil { t.Fatalf("generatePattern error: %v", err) }
17+
if p == "" { t.Fatalf("expected pattern to be set") }
18+
if !strings.Contains(p, "$.evt = \"delivered\"") { t.Fatalf("missing evt in pattern: %s", p) }
19+
}

pkg/provider/metadata_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package provider
2+
3+
import "testing"
4+
5+
func TestMetadataClient_Basics(t *testing.T) {
6+
c, err := NewMetadataClient()
7+
if err != nil { t.Fatalf("client: %v", err) }
8+
if c.Keys == nil || len(c.Keys) == 0 { t.Fatalf("expected keys exported") }
9+
10+
// Known struct from generated catalog
11+
events, fixed, err := c.AllowedEventsForStruct("ActionMailer")
12+
if err != nil { t.Fatalf("allowed: %v", err) }
13+
if len(events) == 0 { t.Fatalf("expected events for ActionMailer") }
14+
15+
// If source is fixed, ensure FixedSourceForStruct returns it
16+
if fixed {
17+
src, isFixed, err := c.FixedSourceForStruct("ActionMailer")
18+
if err != nil { t.Fatalf("fixed source: %v", err) }
19+
if !isFixed || src == "" { t.Fatalf("expected fixed source for ActionMailer") }
20+
}
21+
}
22+

terraform-provider-logstruct

20.1 MB
Binary file not shown.

0 commit comments

Comments
 (0)