Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default values on resources if set on struct annotation #29

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hclconfig
import (
"fmt"

"github.com/creasty/defaults"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"

Expand Down Expand Up @@ -267,6 +268,9 @@ func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Di
ul := getContextLock(ctx)
defer ul()

// if there are defaults defined on the resource set them
defaults.Set(r)

diag := gohcl.DecodeBody(bdy, ctx, r)
if diag.HasErrors() {
pe := &errors.ParserError{}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/aws/aws-sdk-go v1.55.5 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/creasty/defaults v1.8.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWH
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/creasty/defaults v1.8.0 h1:z27FJxCAa0JKt3utc0sCImAEb+spPucmKoOdLHvHYKk=
github.com/creasty/defaults v1.8.0/go.mod h1:iGzKe6pbEHnpMPtfDXZEr0NVxWnPTjb1bbDy08fPzYM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
20 changes: 20 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,26 @@ func TestParseResolvesArrayReferences(t *testing.T) {
require.Equal(t, float64(12), out.Value.([]interface{})[2].(float64))
}

func TestParseSetsDefaultValues(t *testing.T) {
absoluteFolderPath, err := filepath.Abs("./test_fixtures/defaults/container.hcl")
if err != nil {
t.Fatal(err)
}

p := setupParser(t)

c, err := p.ParseFile(absoluteFolderPath)
require.NoError(t, err)

r, err := c.FindResource("resource.container.default")
require.NoError(t, err)
require.NotNil(t, r)

// check default values have been set
cont := r.(*structs.Container)
require.Equal(t, "hello world", cont.Default)
}

func TestLoadsVariableFilesInOptionsOverridingVariableDefaults(t *testing.T) {
absoluteFolderPath, err := filepath.Abs("./test_fixtures/simple")
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions test_fixtures/defaults/container.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resource "container" "default" {
}
2 changes: 2 additions & 0 deletions test_fixtures/structs/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type Container struct {
// embedded type holding name, etc
types.ResourceBase `hcl:"rm,remain"`

Default string `hcl:"default,optional" json:"default,omitempty" default:"hello world"` // A default value

Networks []NetworkAttachment `hcl:"network,block" json:"networks,omitempty"` // Attach to the correct network // only when Image is specified
NetworkObj Network `hcl:"networkobj,optional" json:"networkobj,omitempty"` // Reference to an object

Expand Down
Loading