diff --git a/.changelog/173.txt b/.changelog/173.txt new file mode 100644 index 00000000..22f7ce3f --- /dev/null +++ b/.changelog/173.txt @@ -0,0 +1,3 @@ +```release-note:feature +waypoint: Support setting variables when installing add-ons. +``` diff --git a/internal/commands/waypoint/add-ons/add_on.go b/internal/commands/waypoint/add-ons/add_on.go index f2f7f35e..f23f844b 100644 --- a/internal/commands/waypoint/add-ons/add_on.go +++ b/internal/commands/waypoint/add-ons/add_on.go @@ -17,6 +17,9 @@ type AddOnOpts struct { AddOnDefinitionName string ApplicationName string + Variables map[string]string + VariablesFile string + testFunc func(c *cmd.Command, args []string) error } diff --git a/internal/commands/waypoint/add-ons/create.go b/internal/commands/waypoint/add-ons/create.go index 55e53fa0..8bf3da8f 100644 --- a/internal/commands/waypoint/add-ons/create.go +++ b/internal/commands/waypoint/add-ons/create.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/hcp-sdk-go/clients/cloud-waypoint-service/preview/2023-08-18/client/waypoint_service" "github.com/hashicorp/hcp-sdk-go/clients/cloud-waypoint-service/preview/2023-08-18/models" + "github.com/hashicorp/hcp/internal/commands/waypoint/internal" "github.com/hashicorp/hcp/internal/pkg/cmd" "github.com/hashicorp/hcp/internal/pkg/flagvalue" "github.com/hashicorp/hcp/internal/pkg/heredoc" @@ -64,6 +65,27 @@ $ hcp waypoint add-ons create -n=my-addon -a=my-application -d=my-addon-definiti Value: flagvalue.Simple("", &opts.ApplicationName), Required: true, }, + { + Name: "var", + DisplayValue: "KEY=VALUE", + Description: "A variable to be used in the application. The" + + " flag can be repeated to specify multiple variables. " + + "Variables specified with the flag will override " + + "variables specified in a file.", + Value: flagvalue.SimpleMap(nil, &opts.Variables), + Required: false, + Repeatable: true, + }, + { + Name: "var-file", + DisplayValue: "FILE", + Description: "A file containing variables to be used in the " + + "application. The file should be in HCL format Variables" + + " in the file will be overridden by variables specified" + + " with the --var flag.", + Value: flagvalue.Simple("", &opts.VariablesFile), + Required: false, + }, }, }, } @@ -77,6 +99,43 @@ func addOnCreate(opts *AddOnOpts) error { return err } + // Variable Processing + + // a map is used with the key being the variable name, so that + // flags can override file values. + ivs := make(map[string]*models.HashicorpCloudWaypointInputVariable) + if opts.VariablesFile != "" { + variables, err := internal.ParseInputVariablesFile(opts.VariablesFile) + if err != nil { + return errors.Wrapf(err, "%s failed to parse input variables file %q", + opts.IO.ColorScheme().FailureIcon(), + opts.VariablesFile, + ) + } + for _, v := range variables { + ivs[v.Name] = &models.HashicorpCloudWaypointInputVariable{ + Name: v.Name, + Value: v.Value, + } + } + } + + // Flags are processed second, so that they can override file values. + // Flags take precedence over file values. + for k, v := range opts.Variables { + ivs[k] = &models.HashicorpCloudWaypointInputVariable{ + Name: k, + Value: v, + } + } + + var vars []*models.HashicorpCloudWaypointInputVariable + for _, v := range ivs { + vars = append(vars, v) + } + + // End Variable Processing + _, err = opts.WS.WaypointServiceCreateAddOn( &waypoint_service.WaypointServiceCreateAddOnParams{ NamespaceID: ns.ID, diff --git a/internal/commands/waypoint/add-ons/create_test.go b/internal/commands/waypoint/add-ons/create_test.go index 3aca76de..faa3ff56 100644 --- a/internal/commands/waypoint/add-ons/create_test.go +++ b/internal/commands/waypoint/add-ons/create_test.go @@ -48,11 +48,19 @@ func TestNewCmdCreate(t *testing.T) { "-n=cli-test", "--app=testApp", "--add-on-definition-name=testAddOnDefinition", + "--var-file", "vars.hcl", + "--var", "key=value", + "--var", "key2=value2", }, Expect: &AddOnOpts{ Name: "cli-test", ApplicationName: "testApp", AddOnDefinitionName: "testAddOnDefinition", + Variables: map[string]string{ + "key": "value", + "key2": "value2", + }, + VariablesFile: "vars.hcl", }, }, } @@ -86,6 +94,8 @@ func TestNewCmdCreate(t *testing.T) { r.Equal(c.Expect.Name, addOnOpts.Name) r.Equal(c.Expect.ApplicationName, addOnOpts.ApplicationName) r.Equal(c.Expect.AddOnDefinitionName, addOnOpts.AddOnDefinitionName) + r.Equal(c.Expect.Variables, addOnOpts.Variables) + r.Equal(c.Expect.VariablesFile, addOnOpts.VariablesFile) } }) }