Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

core: add support for input variables #1548

Merged
merged 53 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
3f98486
add variables to config
krantzinator May 27, 2021
1582729
more vars funcs for eventual refactoring
krantzinator May 27, 2021
3d753bd
add some var stuff to runner op and project
krantzinator May 27, 2021
aca5eb6
add var flags to the job
krantzinator May 27, 2021
2d7fc58
finish adding vars to the job
krantzinator May 27, 2021
e138aa8
testdata for variables_test.go
krantzinator May 27, 2021
e022a81
make format
krantzinator May 27, 2021
89926ac
move variables into separate package
krantzinator Jun 1, 2021
bcf37a4
fixup some comments; pt1 of CollectInputVars refactor
krantzinator Jun 2, 2021
0de16e7
move file parsing to func
krantzinator Jun 3, 2021
39affd2
fix test to actually test CollectInputValues
krantzinator Jun 3, 2021
29bbe16
more test cases
krantzinator Jun 3, 2021
80802eb
renamed func; added helper for file parse; some tests
krantzinator Jun 4, 2021
47cfa24
make Source a struct with precedence; update test; add sort
krantzinator Jun 8, 2021
4e2328c
comments and TODOs
krantzinator Jun 8, 2021
adf05f0
files are a slice
krantzinator Jun 8, 2021
12962a9
evalcontext take 1
krantzinator Jun 8, 2021
813ecee
initial refactor; add evalcontext for input vars
krantzinator Jun 10, 2021
1a938bb
fix temporary var name
krantzinator Jun 10, 2021
ada9ad1
update tests with cty null value
krantzinator Jun 10, 2021
87882d9
rename symbol
krantzinator Jun 10, 2021
20a941f
go mod tidy
krantzinator Jun 10, 2021
bc165d8
add new test file
krantzinator Jun 10, 2021
dbb258e
fix errant reordering of client.GetProject on runner
krantzinator Jun 21, 2021
845a47f
add vcs file parsing on runner
krantzinator Jun 24, 2021
95ce1a6
update tests per file loading changes
krantzinator Jun 24, 2021
73df665
add bool and int types to variables proto
krantzinator Jun 25, 2021
b581a75
num/bool support on protos
krantzinator Jun 25, 2021
d416828
rename vars
krantzinator Jun 28, 2021
c5607cc
tried to commit file/vcs combo separately; mildly successful
krantzinator Jun 28, 2021
fbc23a4
refactor struct relationships and precedence sorting
krantzinator Jun 28, 2021
bf25ed5
add missing error; remove old comment
krantzinator Jun 29, 2021
ec2c0b8
i think i forgot to commit this?
krantzinator Jun 29, 2021
05bbc5a
update tests for refactor
krantzinator Jun 30, 2021
08f7898
re-add support for hcl types
krantzinator Jun 30, 2021
59d4bc4
test cleanup; add docs; add test for vcs load
krantzinator Jun 30, 2021
5b84387
fmt
krantzinator Jun 30, 2021
82d9694
doc cleanup
krantzinator Jun 30, 2021
1f85a9a
remove requirement for default value
krantzinator Jun 30, 2021
e851aba
changelog
krantzinator Jun 30, 2021
6399c01
update doc comment
krantzinator Jun 30, 2021
de034eb
proto
krantzinator Jun 30, 2021
c8d9631
fix usage docs
krantzinator Jun 30, 2021
9722ea9
add slightly more info in doc comment
krantzinator Jun 30, 2021
c9115aa
more doc comment cleanup
krantzinator Jun 30, 2021
9bf40a2
typo
krantzinator Jun 30, 2021
f6a7878
first pass at PR comments
krantzinator Jul 1, 2021
c904b84
rename a func; move decode into variables pkg
krantzinator Jul 1, 2021
8a0ded9
fmt
krantzinator Jul 1, 2021
6b376da
fill in empty error msg
krantzinator Jul 1, 2021
c3863eb
add doc comments to protobuf; add hcl parsing to cli/env raw values
krantzinator Jul 1, 2021
be9e72e
remove unused code
krantzinator Jul 1, 2021
7afbaa1
fmt
krantzinator Jul 1, 2021
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
3 changes: 3 additions & 0 deletions .changelog/1548.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
core: Add support for input variables
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note that this is surely a candidate for bold feature (that we don't support automating yet). So no action you have to do @krantzinator but just for us to know when we cut a release and build the changelog.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10-4

```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/go-playground/validator v9.31.0+incompatible
github.com/gofrs/flock v0.8.0
github.com/golang/protobuf v1.5.2
github.com/google/go-cmp v0.5.5
github.com/google/go-jsonnet v0.17.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.1.2
Expand Down
36 changes: 36 additions & 0 deletions internal/cli/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
clientpkg "github.com/hashicorp/waypoint/internal/client"
"github.com/hashicorp/waypoint/internal/clierrors"
"github.com/hashicorp/waypoint/internal/config"
"github.com/hashicorp/waypoint/internal/config/variables"
"github.com/hashicorp/waypoint/internal/pkg/flag"
pb "github.com/hashicorp/waypoint/internal/server/gen"
"github.com/hashicorp/waypoint/internal/server/grpcmetadata"
Expand Down Expand Up @@ -65,6 +66,9 @@ type baseCommand struct {
refApp *pb.Ref_Application
refWorkspace *pb.Ref_Workspace

// variables hold the values set via flags and local env vars
variables []*pb.Variable

//---------------------------------------------------------------
// Internal fields that should not be accessed directly

Expand All @@ -74,6 +78,13 @@ type baseCommand struct {
// flagLabels are set via -label if flagSetOperation is set.
flagLabels map[string]string

// flagVars sets values for defined input variables
flagVars map[string]string

// flagVarFile is a HCL or JSON file setting one or more values
// for defined input variables
flagVarFile []string

// flagRemote is whether to execute using a remote runner or use
// a local runner.
flagRemote bool
Expand Down Expand Up @@ -239,6 +250,17 @@ func (c *baseCommand) Init(opts ...Option) error {
}
}

// Collect variable values from -var and -varfile flags,
// and env vars set with WP_VAR_* and set them on the job
vars, diags := variables.LoadVariableValues(c.flagVars, c.flagVarFile)
if diags.HasErrors() {
// we only return errors for file parsing, so we are specific
// in the error log here
c.logError(c.Log, "failed to load wpvars file", errors.New(diags.Error()))
return diags
}
c.variables = vars

// Create our client
if baseCfg.Client {
c.project, err = c.initClient(nil)
Expand Down Expand Up @@ -405,6 +427,20 @@ func (c *baseCommand) flagSet(bit flagSetBit, f func(*flag.Sets)) *flag.Sets {
"This is specified to the data source type being used in your configuration. " +
"This is used for example to set a specific Git ref to run against.",
})

f.StringMapVar(&flag.StringMapVar{
Name: "var",
Target: &c.flagVars,
Usage: "Variable value to set for this operation. Can be specified multiple times.",
})

f.StringSliceVar(&flag.StringSliceVar{
Name: "var-file",
Target: &c.flagVarFile,
Usage: "HCL or JSON file containing variable values to set for this " +
"operation. If any \"*.auto.wpvars\" or \"*.auto.wpvars.json\" " +
"files are present, they will be automatically loaded.",
})
}

if bit&flagSetConnection != 0 {
Expand Down
1 change: 1 addition & 0 deletions internal/cli/base_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (c *baseCommand) initClient(ctx context.Context) (*clientpkg.Project, error
clientpkg.WithClientConnect(connectOpts...),
clientpkg.WithProjectRef(c.refProject),
clientpkg.WithWorkspaceRef(c.refWorkspace),
clientpkg.WithVariables(c.variables),
clientpkg.WithLabels(c.flagLabels),
clientpkg.WithSourceOverrides(c.flagRemoteSource),
}
Expand Down
1 change: 1 addition & 0 deletions internal/client/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (c *Project) job() *pb.Job {
job := &pb.Job{
TargetRunner: c.runner,
Labels: c.labels,
Variables: c.variables,
Workspace: c.workspace,
Application: &pb.Ref_Application{
Project: c.project.Project,
Expand Down
9 changes: 9 additions & 0 deletions internal/client/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Project struct {
workspace *pb.Ref_Workspace
runner *pb.Ref_Runner
labels map[string]string
variables []*pb.Variable
dataSourceOverrides map[string]string
cleanupFunc func()
serverVersion *pb.VersionInfo
Expand Down Expand Up @@ -227,6 +228,14 @@ func WithClientConnect(opts ...serverclient.ConnectOption) Option {
}
}

// WithVariables sets variable values from flags and local env on any operations.
func WithVariables(m []*pb.Variable) Option {
return func(c *Project, cfg *config) error {
c.variables = m
return nil
}
}

// WithLabels sets the labels or any operations.
func WithLabels(m map[string]string) Option {
return func(c *Project, cfg *config) error {
Expand Down
40 changes: 29 additions & 11 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"path/filepath"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsimple"

"github.com/hashicorp/waypoint/internal/config/variables"
"github.com/hashicorp/waypoint/internal/pkg/defaults"
pb "github.com/hashicorp/waypoint/internal/server/gen"
)
Expand All @@ -18,16 +20,19 @@ type Config struct {
ctx *hcl.EvalContext
path string
pathData map[string]string

InputVariables map[string]*variables.Variable
}

type hclConfig struct {
Project string `hcl:"project,optional"`
Runner *Runner `hcl:"runner,block" default:"{}"`
Labels map[string]string `hcl:"labels,optional"`
Plugin []*Plugin `hcl:"plugin,block"`
Config *genericConfig `hcl:"config,block"`
Apps []*hclApp `hcl:"app,block"`
Body hcl.Body `hcl:",body"`
Project string `hcl:"project,optional"`
Runner *Runner `hcl:"runner,block" default:"{}"`
Labels map[string]string `hcl:"labels,optional"`
Variables []*variables.HclVariable `hcl:"variable,block"`
Plugin []*Plugin `hcl:"plugin,block"`
Config *genericConfig `hcl:"config,block"`
Apps []*hclApp `hcl:"app,block"`
Body hcl.Body `hcl:",body"`
}

// Runner is the configuration for supporting runners in this project.
Expand Down Expand Up @@ -117,6 +122,18 @@ func Load(path string, opts *LoadOptions) (*Config, error) {
if err := hclsimple.DecodeFile(path, finalizeContext(ctx), &cfg); err != nil {
return nil, err
}

// Decode variable blocks
schema, _ := gohcl.ImpliedBodySchema(&hclConfig{})
content, diags := cfg.Body.Content(schema)
if diags.HasErrors() {
return nil, diags
}
vs, diags := variables.DecodeVariableBlocks(content)
if diags.HasErrors() {
return nil, diags
}

if err := defaults.Set(&cfg); err != nil {
return nil, err
}
Expand All @@ -132,10 +149,11 @@ func Load(path string, opts *LoadOptions) (*Config, error) {
}

return &Config{
hclConfig: cfg,
ctx: ctx,
path: filepath.Dir(path),
pathData: pathData,
hclConfig: cfg,
ctx: ctx,
path: filepath.Dir(path),
pathData: pathData,
InputVariables: vs,
}, nil
}

Expand Down
41 changes: 41 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,44 @@ func TestLoad_compare(t *testing.T) {
})
}
}

func TestConfig_variableDecode(t *testing.T) {
cases := []struct {
file string
err string
}{
{
"valid.hcl",
"",
},
{
"invalid_type.hcl",
"Invalid type specification",
},

{
"invalid_def.hcl",
"Invalid default value for variable",
},
{
"duplicate_def.hcl",
"Duplicate variable",
},
}

for _, tt := range cases {
t.Run(tt.file, func(t *testing.T) {
require := require.New(t)

_, err := Load(filepath.Join("testdata", "validate", tt.file), nil)

if tt.err == "" {
require.NoError(err)
return
}

require.Error(err)
require.Contains(err.Error(), tt.err)
})
}
}
8 changes: 8 additions & 0 deletions internal/config/eval_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/zclconf/go-cty/cty/gocty"

"github.com/hashicorp/waypoint/internal/config/funcs"
"github.com/hashicorp/waypoint/internal/config/variables"
)

// EvalContext returns the common eval context to use for parsing all
Expand Down Expand Up @@ -95,6 +96,13 @@ func finalizeContext(ctx *hcl.EvalContext) *hcl.EvalContext {
return ctx
}

// AddVariables uses the final map of InputValues to add all input variables
// to the given hcl EvalContext.
func AddVariables(ctx *hcl.EvalContext, vs variables.Values) *hcl.EvalContext {
variables.AddInputVariables(ctx, vs)
return ctx
}

// hclContextContainer is an interface that config structs that have an HCL
// context may implement. We use this for certain things such as mapoperation()
// to set the proper context.
Expand Down
23 changes: 23 additions & 0 deletions internal/config/testdata/validate/duplicate_def.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
project = "foo"

app "web" {
config {
env = {
static = "hello"
}
}

build {}

deploy {}
}

variable "dinosaur" {
default = "longneck"
type = string
}

variable "dinosaur" {
default = "bigtooth"
type = string
}
18 changes: 18 additions & 0 deletions internal/config/testdata/validate/invalid_def.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
project = "foo"

app "web" {
config {
env = {
static = "hello"
}
}

build {}

deploy {}
}

variable "bees" {
default = "buzz"
type = bool
}
19 changes: 19 additions & 0 deletions internal/config/testdata/validate/invalid_type.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
project = "foo"

app "web" {
config {
env = {
static = "hello"
}
}

build {}

deploy {}
}

variable "bees" {
default = "buzz"
description = "This is my description"
type = notatype
}
6 changes: 6 additions & 0 deletions internal/config/testdata/validate/valid.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ app "web" {

deploy {}
}

variable "bees" {
default = "buzz"
description = "This is my description"
type = string
}
19 changes: 19 additions & 0 deletions internal/config/testdata/validate/variables.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
project = "foo"

app "web" {
config {
env = {
static = "hello"
}
}

build {}

deploy {}
}

variable "bees" {
default = "buzz"
description = "This is my description"
type = string
}
Loading