Skip to content

Commit 8f4b0bd

Browse files
committed
add settings resource
1 parent 912e8ae commit 8f4b0bd

6 files changed

+609
-793
lines changed

internal/provider/convert.go

+139-6
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,165 @@
11
package provider
22

33
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"github.com/hashicorp/terraform-plugin-framework/attr"
9+
"github.com/hashicorp/terraform-plugin-framework/diag"
410
"github.com/hashicorp/terraform-plugin-framework/types"
11+
"github.com/pomerium/enterprise-client-go/pb"
12+
"google.golang.org/protobuf/types/known/durationpb"
13+
"google.golang.org/protobuf/types/known/structpb"
514
)
615

7-
// OptionalString returns a pointer to the string value if not null, otherwise nil
8-
func OptionalString(v types.String) *string {
16+
// StringP returns a pointer to the string value if not null, otherwise nil
17+
func StringP(v types.String) *string {
918
if v.IsNull() {
1019
return nil
1120
}
1221
value := v.ValueString()
1322
return &value
1423
}
1524

16-
// OptionalBool returns a pointer to the bool value if not null, otherwise nil
17-
func OptionalBool(v types.Bool) *bool {
25+
// BoolP returns a pointer to the bool value if not null, otherwise nil
26+
func BoolP(v types.Bool) *bool {
1827
if v.IsNull() {
1928
return nil
2029
}
2130
value := v.ValueBool()
2231
return &value
2332
}
2433

25-
// OptionalFloat64 returns a pointer to the float64 value if not null, otherwise nil
26-
func OptionalFloat64(v types.Float64) *float64 {
34+
// Float64P returns a pointer to the float64 value if not null, otherwise nil
35+
func Float64P(v types.Float64) *float64 {
2736
if v.IsNull() {
2837
return nil
2938
}
3039
value := v.ValueFloat64()
3140
return &value
3241
}
42+
43+
func FromStringSlice(slice []string) types.List {
44+
if slice == nil {
45+
return types.ListNull(types.StringType)
46+
}
47+
fields := make([]attr.Value, 0)
48+
for _, v := range slice {
49+
fields = append(fields, types.StringValue(v))
50+
}
51+
return types.ListValueMust(types.StringType, fields)
52+
}
53+
54+
// FromStringList converts a Settings_StringList to a types.List
55+
func FromStringList(sl *pb.Settings_StringList) types.List {
56+
if sl == nil {
57+
return types.ListNull(types.StringType)
58+
}
59+
return FromStringSlice(sl.Values)
60+
}
61+
62+
// FromStringMap converts a map[string]string to a types.Map
63+
func FromStringMap(m map[string]string) types.Map {
64+
if m == nil {
65+
return types.MapNull(types.StringType)
66+
}
67+
elements := make(map[string]attr.Value)
68+
for k, v := range m {
69+
elements[k] = types.StringValue(v)
70+
}
71+
return types.MapValueMust(types.StringType, elements)
72+
}
73+
74+
// ToStringList converts a types.List to Settings_StringList and handles diagnostics internally
75+
func ToStringList(ctx context.Context, dst **pb.Settings_StringList, list types.List, diagnostics *diag.Diagnostics) {
76+
// Handle null list case first
77+
if list.IsNull() {
78+
*dst = nil
79+
return
80+
}
81+
82+
var values []string
83+
diagnostics.Append(list.ElementsAs(ctx, &values, false)...)
84+
if !diagnostics.HasError() {
85+
*dst = &pb.Settings_StringList{Values: values}
86+
}
87+
}
88+
89+
// ToStringMap converts a types.Map to map[string]string and handles diagnostics internally
90+
func ToStringMap(ctx context.Context, dst *map[string]string, m types.Map, diagnostics *diag.Diagnostics) {
91+
if m.IsNull() {
92+
*dst = nil
93+
return
94+
}
95+
96+
result := make(map[string]string)
97+
diagnostics.Append(m.ElementsAs(ctx, &result, false)...)
98+
if !diagnostics.HasError() {
99+
*dst = result
100+
}
101+
}
102+
103+
// ToStringSlice converts a types.List to string slice and handles diagnostics internally
104+
func ToStringSlice(ctx context.Context, dst *[]string, list types.List, diagnostics *diag.Diagnostics) {
105+
*dst = make([]string, 0)
106+
if !list.IsNull() {
107+
var values []string
108+
diagnostics.Append(list.ElementsAs(ctx, &values, false)...)
109+
if !diagnostics.HasError() {
110+
*dst = values
111+
}
112+
}
113+
}
114+
115+
// ToDurationP converts a types.String containing a duration to a durationpb.Duration and handles diagnostics internally
116+
func ToDurationP(dst **durationpb.Duration, src types.String, field string, diagnostics *diag.Diagnostics) {
117+
if src.IsNull() {
118+
*dst = nil
119+
return
120+
}
121+
122+
if d, err := time.ParseDuration(src.ValueString()); err == nil {
123+
*dst = durationpb.New(d)
124+
} else {
125+
diagnostics.AddError("invalid "+field, err.Error())
126+
}
127+
}
128+
129+
// FromDurationP converts a durationpb.Duration to a types.String
130+
func FromDurationP(d *durationpb.Duration) types.String {
131+
if d == nil {
132+
return types.StringNull()
133+
}
134+
return types.StringValue(d.AsDuration().String())
135+
}
136+
137+
// ToStruct converts a types.Map to a structpb.Struct and handles diagnostics internally
138+
func ToStruct(ctx context.Context, dst **structpb.Struct, m types.Map, field string, diagnostics *diag.Diagnostics) {
139+
if m.IsNull() {
140+
*dst = nil
141+
return
142+
}
143+
144+
var options map[string]interface{}
145+
diagnostics.Append(m.ElementsAs(ctx, &options, false)...)
146+
if !diagnostics.HasError() {
147+
if s, err := structpb.NewStruct(options); err == nil {
148+
*dst = s
149+
} else {
150+
diagnostics.AddError("invalid "+field, err.Error())
151+
}
152+
}
153+
}
154+
155+
// FromStruct converts a structpb.Struct to a types.Map
156+
func FromStruct(s *structpb.Struct) types.Map {
157+
if s == nil {
158+
return types.MapNull(types.StringType)
159+
}
160+
elements := make(map[string]attr.Value)
161+
for k, v := range s.AsMap() {
162+
elements[k] = types.StringValue(fmt.Sprint(v))
163+
}
164+
return types.MapValueMust(types.StringType, elements)
165+
}

0 commit comments

Comments
 (0)