Skip to content

Commit 81c5cd5

Browse files
authored
add idp settings (#14)
1 parent f14de36 commit 81c5cd5

File tree

8 files changed

+669
-21
lines changed

8 files changed

+669
-21
lines changed

example/main.tf

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ resource "pomerium_namespace" "test_namespace" {
2222

2323
resource "pomerium_settings" "settings" {
2424
installation_id = "localhost-dev"
25+
identity_provider_okta = {
26+
api_key = "key"
27+
url = "http://localhost"
28+
}
2529
}
2630

2731
resource "pomerium_policy" "test_policy" {

go.mod

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ go 1.23.0
44

55
require (
66
github.com/go-jose/go-jose/v3 v3.0.3
7-
github.com/hashicorp/terraform-plugin-framework v1.11.0
8-
github.com/hashicorp/terraform-plugin-go v0.23.0
7+
github.com/google/go-cmp v0.6.0
8+
github.com/hashicorp/terraform-plugin-framework v1.13.0
9+
github.com/hashicorp/terraform-plugin-framework-validators v0.16.0
10+
github.com/hashicorp/terraform-plugin-go v0.25.0
911
github.com/hashicorp/terraform-plugin-log v0.9.0
12+
github.com/iancoleman/strcase v0.3.0
1013
github.com/pomerium/enterprise-client-go v0.18.1-0.20241202185750-aab20a674922
1114
github.com/pomerium/pomerium v0.28.0
1215
github.com/rs/zerolog v1.33.0
@@ -31,7 +34,7 @@ require (
3134
github.com/hashicorp/errwrap v1.1.0 // indirect
3235
github.com/hashicorp/go-hclog v1.5.0 // indirect
3336
github.com/hashicorp/go-multierror v1.1.1 // indirect
34-
github.com/hashicorp/go-plugin v1.6.0 // indirect
37+
github.com/hashicorp/go-plugin v1.6.2 // indirect
3538
github.com/hashicorp/go-set/v3 v3.0.0 // indirect
3639
github.com/hashicorp/go-uuid v1.0.3 // indirect
3740
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect

go.sum

+10-6
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+
122122
github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
123123
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
124124
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
125-
github.com/hashicorp/go-plugin v1.6.0 h1:wgd4KxHJTVGGqWBq4QPB1i5BZNEx9BR8+OFmHDmTk8A=
126-
github.com/hashicorp/go-plugin v1.6.0/go.mod h1:lBS5MtSSBZk0SHc66KACcjjlU6WzEVP/8pwz68aMkCI=
125+
github.com/hashicorp/go-plugin v1.6.2 h1:zdGAEd0V1lCaU0u+MxWQhtSDQmahpkwOun8U8EiRVog=
126+
github.com/hashicorp/go-plugin v1.6.2/go.mod h1:CkgLQ5CZqNmdL9U9JzM532t8ZiYQ35+pj3b1FD37R0Q=
127127
github.com/hashicorp/go-set/v3 v3.0.0 h1:CaJBQvQCOWoftrBcDt7Nwgo0kdpmrKxar/x2o6pV9JA=
128128
github.com/hashicorp/go-set/v3 v3.0.0/go.mod h1:IEghM2MpE5IaNvL+D7X480dfNtxjRXZ6VMpK3C8s2ok=
129129
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
@@ -132,10 +132,12 @@ github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs
132132
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
133133
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
134134
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
135-
github.com/hashicorp/terraform-plugin-framework v1.11.0 h1:M7+9zBArexHFXDx/pKTxjE6n/2UCXY6b8FIq9ZYhwfE=
136-
github.com/hashicorp/terraform-plugin-framework v1.11.0/go.mod h1:qBXLDn69kM97NNVi/MQ9qgd1uWWsVftGSnygYG1tImM=
137-
github.com/hashicorp/terraform-plugin-go v0.23.0 h1:AALVuU1gD1kPb48aPQUjug9Ir/125t+AAurhqphJ2Co=
138-
github.com/hashicorp/terraform-plugin-go v0.23.0/go.mod h1:1E3Cr9h2vMlahWMbsSEcNrOCxovCZhOOIXjFHbjc/lQ=
135+
github.com/hashicorp/terraform-plugin-framework v1.13.0 h1:8OTG4+oZUfKgnfTdPTJwZ532Bh2BobF4H+yBiYJ/scw=
136+
github.com/hashicorp/terraform-plugin-framework v1.13.0/go.mod h1:j64rwMGpgM3NYXTKuxrCnyubQb/4VKldEKlcG8cvmjU=
137+
github.com/hashicorp/terraform-plugin-framework-validators v0.16.0 h1:O9QqGoYDzQT7lwTXUsZEtgabeWW96zUBh47Smn2lkFA=
138+
github.com/hashicorp/terraform-plugin-framework-validators v0.16.0/go.mod h1:Bh89/hNmqsEWug4/XWKYBwtnw3tbz5BAy1L1OgvbIaY=
139+
github.com/hashicorp/terraform-plugin-go v0.25.0 h1:oi13cx7xXA6QciMcpcFi/rwA974rdTxjqEhXJjbAyks=
140+
github.com/hashicorp/terraform-plugin-go v0.25.0/go.mod h1:+SYagMYadJP86Kvn+TGeV+ofr/R3g4/If0O5sO96MVw=
139141
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
140142
github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow=
141143
github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI=
@@ -144,6 +146,8 @@ github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S
144146
github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc=
145147
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
146148
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
149+
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
150+
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
147151
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
148152
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
149153
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=

internal/provider/convert.go

+117
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ package provider
22

33
import (
44
"context"
5+
"fmt"
6+
"reflect"
57
"time"
68

79
"github.com/hashicorp/terraform-plugin-framework/attr"
810
"github.com/hashicorp/terraform-plugin-framework/diag"
11+
"github.com/hashicorp/terraform-plugin-framework/path"
912
"github.com/hashicorp/terraform-plugin-framework/types"
13+
"github.com/iancoleman/strcase"
1014
"github.com/pomerium/enterprise-client-go/pb"
1115
"google.golang.org/protobuf/types/known/durationpb"
16+
17+
"google.golang.org/protobuf/types/known/structpb"
1218
)
1319

1420
func FromStringSlice(slice []string) types.List {
@@ -104,3 +110,114 @@ func FromDuration(d *durationpb.Duration) types.String {
104110
}
105111
return types.StringValue(d.AsDuration().String())
106112
}
113+
114+
// GoStructToPB converts a Go struct to a protobuf Struct.
115+
// It only supports protobuf types.String field types
116+
// Field names are converted to snake_case.
117+
func GoStructToPB(input interface{}) (*structpb.Struct, error) {
118+
if input == nil {
119+
return nil, nil
120+
}
121+
122+
val := reflect.ValueOf(input)
123+
if val.Kind() != reflect.Struct {
124+
return nil, fmt.Errorf("input must be a struct, got %v", val.Kind())
125+
}
126+
127+
fields := make(map[string]*structpb.Value)
128+
typ := val.Type()
129+
130+
typeString := reflect.TypeOf(types.String{})
131+
for i := 0; i < typ.NumField(); i++ {
132+
field := typ.Field(i)
133+
fieldValue := val.Field(i)
134+
fieldName := strcase.ToSnake(field.Name)
135+
136+
if fieldValue.Type() != typeString {
137+
return nil, fmt.Errorf("unsupported field type %s for field %s", fieldValue.Type(), fieldName)
138+
}
139+
protoValue, ok := fieldValue.Interface().(types.String)
140+
if !ok {
141+
return nil, fmt.Errorf("unexpected type assertion for field %s", fieldName)
142+
}
143+
if !protoValue.IsNull() {
144+
fields[fieldName] = structpb.NewStringValue(protoValue.ValueString())
145+
}
146+
}
147+
148+
return &structpb.Struct{Fields: fields}, nil
149+
}
150+
151+
// PBStructToTF converts a protobuf Struct to a types.Object,
152+
// by enumerating the `tfsdk` tags on the struct fields.
153+
// only supports string fields
154+
func PBStructToTF[T any](
155+
dst *types.Object,
156+
src *structpb.Struct,
157+
diags *diag.Diagnostics,
158+
) {
159+
attrTypes, err := GetTFObjectTypes[T]()
160+
if err != nil {
161+
diags.AddError("failed to get object types", err.Error())
162+
return
163+
}
164+
165+
if src == nil {
166+
*dst = types.ObjectNull(attrTypes)
167+
return
168+
}
169+
170+
attrs := make(map[string]attr.Value)
171+
for k, v := range src.Fields {
172+
_, ok := attrTypes[k]
173+
if !ok {
174+
diags.AddAttributeWarning(
175+
path.Root(k),
176+
"unexpected field",
177+
fmt.Sprintf("unexpected field %s", k),
178+
)
179+
continue
180+
}
181+
str, ok := v.GetKind().(*structpb.Value_StringValue)
182+
if !ok {
183+
diags.AddAttributeError(
184+
path.Root(k),
185+
"unsupported field type",
186+
fmt.Sprintf("%T for field %s", v, k))
187+
return
188+
}
189+
attrs[k] = types.StringValue(str.StringValue)
190+
}
191+
192+
for k := range attrTypes {
193+
if _, ok := src.Fields[k]; ok {
194+
continue
195+
}
196+
attrs[k] = types.StringNull()
197+
}
198+
199+
v, d := types.ObjectValue(attrTypes, attrs)
200+
diags.Append(d...)
201+
if !diags.HasError() {
202+
*dst = v
203+
}
204+
}
205+
206+
func GetTFObjectTypes[T any]() (map[string]attr.Type, error) {
207+
tm := make(map[string]attr.Type)
208+
var v T
209+
typ := reflect.TypeOf(v)
210+
typeString := reflect.TypeOf(types.String{})
211+
for i := 0; i < typ.NumField(); i++ {
212+
field := typ.Field(i)
213+
if field.Type != typeString {
214+
return nil, fmt.Errorf("unsupported field type %s for field %s", field.Type, field.Name)
215+
}
216+
tfsdkTag := field.Tag.Get("tfsdk")
217+
if tfsdkTag == "" {
218+
return nil, fmt.Errorf("missing tfsdk tag for field %s", field.Name)
219+
}
220+
tm[tfsdkTag] = types.StringType
221+
}
222+
return tm, nil
223+
}

0 commit comments

Comments
 (0)