Skip to content

Commit

Permalink
feat: Support setting variables on add-ons.
Browse files Browse the repository at this point in the history
  • Loading branch information
paladin-devops committed Oct 16, 2024
1 parent f8982d7 commit 15c65ee
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/173.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
waypoint: Support setting variables when installing add-ons.
```
3 changes: 3 additions & 0 deletions internal/commands/waypoint/add-ons/add_on.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
62 changes: 61 additions & 1 deletion internal/commands/waypoint/add-ons/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
},
},
},
}
Expand All @@ -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,
Expand All @@ -88,7 +147,8 @@ func addOnCreate(opts *AddOnOpts) error {
Definition: &models.HashicorpCloudWaypointRefAddOnDefinition{
Name: opts.AddOnDefinitionName,
},
Name: opts.Name,
Name: opts.Name,
Variables: vars,
},
}, nil)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions internal/commands/waypoint/add-ons/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
}
Expand Down Expand Up @@ -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)
}
})
}
Expand Down

0 comments on commit 15c65ee

Please sign in to comment.