Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tfbuilder/assertions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tfbuilder

import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
)

func CheckReapplyPlanEmpty(builder *Builder) resource.TestStep {
return resource.TestStep{
// Re-apply the same config and ensure no changes occur
Config: builder.Build(),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
}
}
5 changes: 4 additions & 1 deletion tfbuilder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ func TestBuilder_KongMeshWithPolicy(t *testing.T) {
WithLabels(map[string]string{
"kuma.io/mesh": "kong-mesh_mesh.default.name",
}).
WithSpecHCL(tfbuilder.AllowAllTrafficPermissionSpec),
WithSpec(tfbuilder.AllowAllTrafficPermissionSpec).
AddToSpec(`kind = "Mesh"`, `proxy_types = ["Sidecar"]`).
UpdateSpec(`kind = "Mesh"`, `kind = "MeshSubset"`).
RemoveFromSpec(`action = "Allow"`),
)

actual := builder.Build()
Expand Down
56 changes: 55 additions & 1 deletion tfbuilder/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,64 @@ module github.com/Kong/shared-speakeasy/tfbuilder

go 1.24.1

require github.com/stretchr/testify v1.10.0
require (
github.com/hashicorp/terraform-plugin-testing v1.12.0
github.com/stretchr/testify v1.10.0
)

require (
github.com/ProtonMail/go-crypto v1.1.3 // indirect
github.com/agext/levenshtein v1.2.2 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-cty v1.5.0 // indirect
github.com/hashicorp/go-hclog v1.6.3 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.6.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/hashicorp/hc-install v0.9.1 // indirect
github.com/hashicorp/hcl/v2 v2.23.0 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.22.0 // indirect
github.com/hashicorp/terraform-json v0.24.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.26.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.36.1 // indirect
github.com/hashicorp/terraform-registry-address v0.2.4 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/zclconf/go-cty v1.16.2 // indirect
golang.org/x/crypto v0.36.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.37.0 // indirect
golang.org/x/sync v0.12.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53 // indirect
google.golang.org/grpc v1.69.4 // indirect
google.golang.org/protobuf v1.36.3 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
221 changes: 220 additions & 1 deletion tfbuilder/go.sum

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions tfbuilder/mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ func (m *MeshBuilder) WithDependsOn(deps ...string) *MeshBuilder {
return m
}

func (m *MeshBuilder) AddToSpec(after, newLine string) *MeshBuilder {
m.Spec = addToSpec(m.Spec, after, newLine)
return m
}

func (m *MeshBuilder) RemoveFromSpec(match string) *MeshBuilder {
m.Spec = removeFromSpec(m.Spec, match)
return m
}

func (m *MeshBuilder) UpdateSpec(match, newValue string) *MeshBuilder {
m.Spec = updateSpec(m.Spec, match, newValue)
return m
}

func (m *MeshBuilder) Render(provider *Builder) string {
data := map[string]interface{}{
"Provider": provider.provider,
Expand Down
24 changes: 24 additions & 0 deletions tfbuilder/plan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tfbuilder

import (
"context"
"encoding/json"
"log"

"github.com/hashicorp/terraform-plugin-testing/plancheck"
)

type debugPlan struct{}

func (e debugPlan) CheckPlan(ctx context.Context, req plancheck.CheckPlanRequest, resp *plancheck.CheckPlanResponse) {
reqPlan, err := json.Marshal(req.Plan)
if err != nil {
log.Fatalf("error marshaling plan request: %s", err)
}
reqPlanStr := string(reqPlan)
log.Printf("req.Plan - %s\n", reqPlanStr)
}

func DebugPlan() plancheck.PlanCheck {
return debugPlan{}
}
23 changes: 19 additions & 4 deletions tfbuilder/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type PolicyBuilder struct {
Labels map[string]string
PolicyName string
Type string
SpecHCL string // literal HCL block
Spec string // literal HCL block
CPID string // Optional
}

Expand Down Expand Up @@ -57,8 +57,8 @@ func (p *PolicyBuilder) WithDependsOn(deps ...string) *PolicyBuilder {
return p
}

func (p *PolicyBuilder) WithSpecHCL(hcl string) *PolicyBuilder {
p.SpecHCL = hcl
func (p *PolicyBuilder) WithSpec(hcl string) *PolicyBuilder {
p.Spec = hcl
return p
}

Expand All @@ -67,6 +67,21 @@ func (p *PolicyBuilder) WithCPID(cpID string) *PolicyBuilder {
return p
}

func (p *PolicyBuilder) AddToSpec(after, newLine string) *PolicyBuilder {
p.Spec = addToSpec(p.Spec, after, newLine)
return p
}

func (p *PolicyBuilder) RemoveFromSpec(match string) *PolicyBuilder {
p.Spec = removeFromSpec(p.Spec, match)
return p
}

func (p *PolicyBuilder) UpdateSpec(match, newValue string) *PolicyBuilder {
p.Spec = updateSpec(p.Spec, match, newValue)
return p
}

func (p *PolicyBuilder) Render(provider *Builder) string {
tmplBytes, err := templatesFS.ReadFile("templates/policy.tmpl")
if err != nil {
Expand All @@ -89,7 +104,7 @@ func (p *PolicyBuilder) Render(provider *Builder) string {
"Labels": p.Labels,
"Name": p.PolicyName,
"Type": p.Type,
"Spec": p.SpecHCL,
"Spec": p.Spec,
"CPID": p.CPID,
}); err != nil {
panic(err)
Expand Down
56 changes: 56 additions & 0 deletions tfbuilder/spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package tfbuilder

import (
"log"
"strings"
)

func addToSpec(spec string, after, newLine string) string {
lines := strings.Split(spec, "\n")
var result []string
inserted := false

for i := 0; i < len(lines); i++ {
line := lines[i]
result = append(result, line)

if !inserted && strings.Contains(line, after) {
indent := getIndentation(line)
result = append(result, indent+newLine)
inserted = true
}
}

if !inserted {
log.Fatalf("AddToSpec failed: could not find line containing: %q", after)
}

return strings.Join(result, "\n")
}

func removeFromSpec(spec string, match string) string {
lines := strings.Split(spec, "\n")
var filtered []string
for _, l := range lines {
if !strings.Contains(l, match) {
filtered = append(filtered, l)
}
}
return strings.Join(filtered, "\n")
}

func updateSpec(spec, match, newValue string) string {
return strings.ReplaceAll(spec, match, newValue)
}

func getIndentation(line string) string {
indent := ""
for _, r := range line {
if r == ' ' || r == '\t' {
indent += string(r)
} else {
break
}
}
return indent
}
4 changes: 2 additions & 2 deletions tfbuilder/testdata/expected_kong_mesh_with_policy.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ resource "kong-mesh_mesh_traffic_permission" "allow_all" {
from = [
{
target_ref = {
kind = "Mesh"
kind = "MeshSubset"
proxy_types = ["Sidecar"]
}
default = {
action = "Allow"
}
}
]
Expand Down
Loading