Skip to content

Commit 00eb7a6

Browse files
authored
resources: use sets for properties where order has no significance (#35)
Updates data model to use `Set` instead of a `List` where order of elements has no significance. https://linear.app/pomerium/issue/ENG-1950/use-sets-where-appropriate-for-order-insensitive-fields
1 parent a85a0e5 commit 00eb7a6

12 files changed

+530
-68
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
run: go mod download
2424

2525
- name: Run tests
26-
run: go test -v -cover ./internal/provider/...
26+
run: make test
2727

2828
- name: Run acceptance tests
2929
run: |

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ build:
1212

1313
.PHONY: docs
1414
docs:
15+
@echo "@==> $@"
1516
go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs@latest generate --provider-dir . -provider-name pomerium
17+
18+
.PHONY: test
19+
test:
20+
@echo "@==> $@"
21+
@go test ./internal/provider/...

example/main.tf

+19-1
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,30 @@ resource "pomerium_policy" "test_policy" {
7676
})
7777
}
7878

79+
resource "pomerium_policy" "test_policy_group" {
80+
name = "group test policy"
81+
namespace_id = pomerium_namespace.test_namespace.id
82+
ppl = yamlencode({
83+
allow = {
84+
and = [
85+
{
86+
groups = {
87+
has = "gid-123456"
88+
}
89+
}
90+
]
91+
}
92+
})
93+
}
7994
resource "pomerium_route" "test_route" {
8095
name = "test-route"
8196
namespace_id = pomerium_namespace.test_namespace.id
8297
from = "https://verify-tf.localhost.pomerium.io"
8398
to = ["https://verify.pomerium.com"]
84-
policies = [pomerium_policy.test_policy.id]
99+
policies = [
100+
pomerium_policy.test_policy_group.id,
101+
pomerium_policy.test_policy.id,
102+
]
85103
}
86104

87105
resource "pomerium_key_pair" "test_key_pair" {

internal/provider/convert.go

+32-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,18 @@ import (
1717
"google.golang.org/protobuf/types/known/structpb"
1818
)
1919

20-
func FromStringSlice(slice []string) types.List {
20+
func FromStringSliceToSet(slice []string) types.Set {
21+
if slice == nil {
22+
return types.SetNull(types.StringType)
23+
}
24+
fields := make([]attr.Value, 0)
25+
for _, v := range slice {
26+
fields = append(fields, types.StringValue(v))
27+
}
28+
return types.SetValueMust(types.StringType, fields)
29+
}
30+
31+
func FromStringSliceToList(slice []string) types.List {
2132
if slice == nil {
2233
return types.ListNull(types.StringType)
2334
}
@@ -28,12 +39,12 @@ func FromStringSlice(slice []string) types.List {
2839
return types.ListValueMust(types.StringType, fields)
2940
}
3041

31-
// FromStringList converts a Settings_StringList to a types.List
32-
func FromStringList(sl *pb.Settings_StringList) types.List {
42+
// FromStringListToSet converts a Settings_StringList to a types.List
43+
func FromStringListToSet(sl *pb.Settings_StringList) types.Set {
3344
if sl == nil {
34-
return types.ListNull(types.StringType)
45+
return types.SetNull(types.StringType)
3546
}
36-
return FromStringSlice(sl.Values)
47+
return FromStringSliceToSet(sl.Values)
3748
}
3849

3950
// FromStringMap converts a map[string]string to a types.Map
@@ -49,14 +60,14 @@ func FromStringMap(m map[string]string) types.Map {
4960
}
5061

5162
// ToStringList converts a types.List to Settings_StringList and handles diagnostics internally
52-
func ToStringList(ctx context.Context, dst **pb.Settings_StringList, list types.List, diagnostics *diag.Diagnostics) {
53-
if list.IsNull() {
63+
func ToStringListFromSet(ctx context.Context, dst **pb.Settings_StringList, set types.Set, diagnostics *diag.Diagnostics) {
64+
if set.IsNull() {
5465
*dst = nil
5566
return
5667
}
5768

5869
var values []string
59-
diagnostics.Append(list.ElementsAs(ctx, &values, false)...)
70+
diagnostics.Append(set.ElementsAs(ctx, &values, false)...)
6071
if !diagnostics.HasError() {
6172
*dst = &pb.Settings_StringList{Values: values}
6273
}
@@ -76,8 +87,19 @@ func ToStringMap(ctx context.Context, dst *map[string]string, m types.Map, diagn
7687
}
7788
}
7889

79-
// ToStringSlice converts a types.List to string slice and handles diagnostics internally
80-
func ToStringSlice(ctx context.Context, dst *[]string, list types.List, diagnostics *diag.Diagnostics) {
90+
func ToStringSliceFromSet(ctx context.Context, dst *[]string, set types.Set, diagnostics *diag.Diagnostics) {
91+
*dst = make([]string, 0)
92+
if !set.IsNull() {
93+
var values []string
94+
diagnostics.Append(set.ElementsAs(ctx, &values, false)...)
95+
if !diagnostics.HasError() {
96+
*dst = values
97+
}
98+
}
99+
}
100+
101+
// ToStringSliceFromList converts a types.List to string slice and handles diagnostics internally
102+
func ToStringSliceFromList(ctx context.Context, dst *[]string, list types.List, diagnostics *diag.Diagnostics) {
81103
*dst = make([]string, 0)
82104
if !list.IsNull() {
83105
var values []string

0 commit comments

Comments
 (0)