Skip to content

Commit a706536

Browse files
committed
Provider: use embedded Go catalog; implement DataSource Configure; fix provider signatures and attr types; build succeeds
1 parent c358c6f commit a706536

File tree

7 files changed

+233
-94
lines changed

7 files changed

+233
-94
lines changed

go.mod

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,29 @@ module github.com/DocSpring/terraform-provider-logstruct
22

33
go 1.22.0
44

5+
require github.com/hashicorp/terraform-plugin-framework v1.11.0
6+
57
require (
6-
github.com/hashicorp/terraform-plugin-framework v1.11.0
7-
github.com/hashicorp/terraform-plugin-log/tflog v0.9.0
8+
github.com/fatih/color v1.13.0 // indirect
9+
github.com/golang/protobuf v1.5.4 // indirect
10+
github.com/hashicorp/go-hclog v1.5.0 // indirect
11+
github.com/hashicorp/go-plugin v1.6.0 // indirect
12+
github.com/hashicorp/go-uuid v1.0.3 // indirect
13+
github.com/hashicorp/terraform-plugin-go v0.23.0 // indirect
14+
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
15+
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
16+
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
17+
github.com/hashicorp/yamux v0.1.1 // indirect
18+
github.com/mattn/go-colorable v0.1.12 // indirect
19+
github.com/mattn/go-isatty v0.0.14 // indirect
20+
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
21+
github.com/oklog/run v1.0.0 // indirect
22+
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
23+
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
24+
golang.org/x/net v0.23.0 // indirect
25+
golang.org/x/sys v0.18.0 // indirect
26+
golang.org/x/text v0.14.0 // indirect
27+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
28+
google.golang.org/grpc v1.63.2 // indirect
29+
google.golang.org/protobuf v1.34.0 // indirect
830
)

go.sum

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
2+
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
3+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6+
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
7+
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
8+
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
9+
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
10+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
11+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
12+
github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+13c=
13+
github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
14+
github.com/hashicorp/go-plugin v1.6.0 h1:wgd4KxHJTVGGqWBq4QPB1i5BZNEx9BR8+OFmHDmTk8A=
15+
github.com/hashicorp/go-plugin v1.6.0/go.mod h1:lBS5MtSSBZk0SHc66KACcjjlU6WzEVP/8pwz68aMkCI=
16+
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
17+
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
18+
github.com/hashicorp/terraform-plugin-framework v1.11.0 h1:M7+9zBArexHFXDx/pKTxjE6n/2UCXY6b8FIq9ZYhwfE=
19+
github.com/hashicorp/terraform-plugin-framework v1.11.0/go.mod h1:qBXLDn69kM97NNVi/MQ9qgd1uWWsVftGSnygYG1tImM=
20+
github.com/hashicorp/terraform-plugin-go v0.23.0 h1:AALVuU1gD1kPb48aPQUjug9Ir/125t+AAurhqphJ2Co=
21+
github.com/hashicorp/terraform-plugin-go v0.23.0/go.mod h1:1E3Cr9h2vMlahWMbsSEcNrOCxovCZhOOIXjFHbjc/lQ=
22+
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
23+
github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow=
24+
github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI=
25+
github.com/hashicorp/terraform-registry-address v0.2.3/go.mod h1:lFHA76T8jfQteVfT7caREqguFrW3c4MFSPhZB7HHgUM=
26+
github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ=
27+
github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc=
28+
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
29+
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
30+
github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c=
31+
github.com/jhump/protoreflect v1.15.1/go.mod h1:jD/2GMKKE6OqX8qTjhADU1e6DShO+gavG9e0Q693nKo=
32+
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
33+
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
34+
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
35+
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
36+
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
37+
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
38+
github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU=
39+
github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8=
40+
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
41+
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
42+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
43+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
44+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
45+
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
46+
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
47+
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
48+
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
49+
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
50+
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
51+
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
52+
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
53+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
54+
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
55+
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
56+
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
57+
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
58+
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
59+
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
60+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
61+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
62+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de h1:cZGRis4/ot9uVm639a+rHCUaG0JJHEsdyzSQTMX+suY=
63+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:H4O17MA/PE9BsGx3w+a+W2VOLLD1Qf7oJneAoU6WktY=
64+
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
65+
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
66+
google.golang.org/protobuf v1.34.0 h1:Qo/qEd2RZPCf2nKuorzksSknv0d3ERwp1vFG38gSmH4=
67+
google.golang.org/protobuf v1.34.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
68+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
69+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
70+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/data/catalog_gen.go

Lines changed: 100 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/provider/datasource_cw_filter.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework/types"
1212
)
1313

14-
type cwFilterDataSource struct{}
14+
type cwFilterDataSource struct{ client *MetadataClient }
1515

1616
func NewCloudWatchFilterDataSource() datasource.DataSource { return &cwFilterDataSource{} }
1717

@@ -42,14 +42,19 @@ func (d *cwFilterDataSource) Schema(_ context.Context, _ datasource.SchemaReques
4242
}
4343
}
4444

45+
func (d *cwFilterDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
46+
if req.ProviderData == nil { return }
47+
if c, ok := req.ProviderData.(*MetadataClient); ok { d.client = c }
48+
}
49+
4550
func (d *cwFilterDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
4651
var data cwFilterModel
4752
diags := req.Config.Get(ctx, &data)
4853
resp.Diagnostics.Append(diags...)
4954
if resp.Diagnostics.HasError() { return }
5055

51-
client, ok := req.ProviderData.(*MetadataClient)
52-
if !ok || client == nil {
56+
client := d.client
57+
if client == nil {
5358
resp.Diagnostics.AddError("Provider not configured", "Missing metadata client")
5459
return
5560
}
@@ -100,4 +105,3 @@ func (d *cwFilterDataSource) Read(ctx context.Context, req datasource.ReadReques
100105

101106
func contains(arr []string, s string) bool { for _, v := range arr { if v == s { return true } }; return false }
102107
func escape(s string) string { return strings.ReplaceAll(s, "\"", "\\\"") }
103-

pkg/provider/datasource_struct.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
"github.com/hashicorp/terraform-plugin-framework/datasource"
77
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
88
"github.com/hashicorp/terraform-plugin-framework/types"
9+
"github.com/hashicorp/terraform-plugin-framework/attr"
910
)
1011

11-
type structDataSource struct{}
12+
type structDataSource struct{ client *MetadataClient }
1213

1314
func NewStructDataSource() datasource.DataSource { return &structDataSource{} }
1415

@@ -34,14 +35,19 @@ func (d *structDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
3435
}
3536
}
3637

38+
func (d *structDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
39+
if req.ProviderData == nil { return }
40+
if c, ok := req.ProviderData.(*MetadataClient); ok { d.client = c }
41+
}
42+
3743
func (d *structDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
3844
var data structDataModel
3945
diags := req.Config.Get(ctx, &data)
4046
resp.Diagnostics.Append(diags...)
4147
if resp.Diagnostics.HasError() { return }
4248

43-
client, ok := req.ProviderData.(*MetadataClient)
44-
if !ok || client == nil {
49+
client := d.client
50+
if client == nil {
4551
resp.Diagnostics.AddError("Provider not configured", "Missing metadata client")
4652
return
4753
}
@@ -59,7 +65,7 @@ func (d *structDataSource) Read(ctx context.Context, req datasource.ReadRequest,
5965
for _, e := range allowed { data.AllowedEvents = append(data.AllowedEvents, types.StringValue(e)) }
6066
_ = single
6167
// keys map
62-
kv := map[string]types.Value{}
68+
kv := map[string]attr.Value{}
6369
for k, v := range client.Keys { kv[k] = types.StringValue(v) }
6470
m, md := types.MapValue(types.StringType, kv)
6571
resp.Diagnostics.Append(md...)
@@ -68,4 +74,3 @@ func (d *structDataSource) Read(ctx context.Context, req datasource.ReadRequest,
6874
diags = resp.State.Set(ctx, &data)
6975
resp.Diagnostics.Append(diags...)
7076
}
71-

pkg/provider/metadata.go

Lines changed: 10 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,32 @@
11
package provider
22

33
import (
4-
"encoding/json"
54
"fmt"
6-
"os"
7-
"path/filepath"
5+
"github.com/DocSpring/terraform-provider-logstruct/pkg/data"
86
)
97

108
type MetadataClient struct {
11-
Keys map[string]string
12-
Enums map[string][]EnumValue
13-
Structs map[string]StructInfo
9+
Keys map[string]string
10+
Structs map[string]StructCatalog
1411
}
1512

16-
type EnumValue struct {
17-
Name string `json:"name"`
18-
Value string `json:"value"`
19-
}
20-
21-
type FieldInfo struct {
22-
Optional bool `json:"optional"`
23-
Type string `json:"type"`
24-
BaseEnum string `json:"base_enum,omitempty"`
25-
EnumValue string `json:"enum_value,omitempty"`
26-
// for enum_union
27-
EnumValues []string `json:"enum_values,omitempty"`
28-
}
29-
30-
type StructInfo struct {
31-
Name string `json:"name"`
32-
Fields map[string]FieldInfo `json:"fields"`
33-
}
13+
type StructCatalog = data.StructCatalog
3414

35-
func NewMetadataClient(dir string) (*MetadataClient, error) {
36-
c := &MetadataClient{}
37-
38-
// keys
39-
keysPath := filepath.Join(dir, "log-keys.json")
40-
b, err := os.ReadFile(keysPath)
41-
if err != nil { return nil, fmt.Errorf("read log-keys.json: %w", err) }
42-
if err := json.Unmarshal(b, &c.Keys); err != nil { return nil, fmt.Errorf("parse log-keys.json: %w", err) }
43-
44-
// enums
45-
enumsPath := filepath.Join(dir, "sorbet-enums.json")
46-
b, err = os.ReadFile(enumsPath)
47-
if err != nil { return nil, fmt.Errorf("read sorbet-enums.json: %w", err) }
48-
if err := json.Unmarshal(b, &c.Enums); err != nil { return nil, fmt.Errorf("parse sorbet-enums.json: %w", err) }
49-
50-
// structs
51-
structsPath := filepath.Join(dir, "sorbet-log-structs.json")
52-
b, err = os.ReadFile(structsPath)
53-
if err != nil { return nil, fmt.Errorf("read sorbet-log-structs.json: %w", err) }
54-
if err := json.Unmarshal(b, &c.Structs); err != nil { return nil, fmt.Errorf("parse sorbet-log-structs.json: %w", err) }
55-
56-
return c, nil
15+
func NewMetadataClient() (*MetadataClient, error) {
16+
return &MetadataClient{Keys: data.CatalogData.Keys, Structs: data.CatalogData.Structs}, nil
5717
}
5818

5919
func (c *MetadataClient) AllowedEventsForStruct(structName string) ([]string, bool, error) {
6020
si, ok := c.Structs[structName]
6121
if !ok { return nil, false, fmt.Errorf("unknown struct: %s", structName) }
62-
f, ok := si.Fields["event"]
63-
if !ok { return nil, false, fmt.Errorf("struct %s missing event field", structName) }
64-
switch f.Type {
65-
case "enum_single":
66-
if f.EnumValue == "" { return nil, false, fmt.Errorf("struct %s event enum_single missing value", structName) }
67-
return []string{f.EnumValue}, true, nil
68-
case "enum_union":
69-
if len(f.EnumValues) == 0 { return nil, false, fmt.Errorf("struct %s event enum_union empty", structName) }
70-
return f.EnumValues, false, nil
71-
default:
72-
return nil, false, fmt.Errorf("struct %s event field not enum: %s", structName, f.Type)
73-
}
22+
if len(si.AllowedEvents) == 1 { return si.AllowedEvents, true, nil }
23+
if len(si.AllowedEvents) > 1 { return si.AllowedEvents, false, nil }
24+
return nil, false, fmt.Errorf("struct %s has no allowed events", structName)
7425
}
7526

7627
func (c *MetadataClient) FixedSourceForStruct(structName string) (string, bool, error) {
7728
si, ok := c.Structs[structName]
7829
if !ok { return "", false, fmt.Errorf("unknown struct: %s", structName) }
79-
f, ok := si.Fields["source"]
80-
if !ok { return "", false, fmt.Errorf("struct %s missing source field", structName) }
81-
if f.Type == "enum_single" && f.EnumValue != "" {
82-
return f.EnumValue, true, nil
83-
}
30+
if si.FixedSource != nil { return *si.FixedSource, true, nil }
8431
return "", false, nil
8532
}
86-

0 commit comments

Comments
 (0)