Skip to content

Commit

Permalink
Merge pull request #177 from hashicorp/f-waypoint-improvements
Browse files Browse the repository at this point in the history
Waypoint improvements
  • Loading branch information
paladin-devops authored Oct 21, 2024
2 parents dd4b1c4 + ec06f38 commit eaca7fc
Show file tree
Hide file tree
Showing 27 changed files with 641 additions and 197 deletions.
3 changes: 3 additions & 0 deletions .changelog/163.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
waypoint: Add flags to make execution mode configurable for Waypoint templates and add-on definitions.
```
3 changes: 3 additions & 0 deletions .changelog/170.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
waypoint: Add ability for variable options on an add-on definition to be configured with an HCL file.
```
3 changes: 3 additions & 0 deletions .changelog/172.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
waypoint: Support setting variables when creating apps.
```
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 .changelog/174.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:breaking-change
waypoint: Remove type field from variable options config file.
```
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type AddOnDefinitionOpts struct {
TerraformNoCodeModuleSource string
TerraformCloudProjectName string
TerraformCloudProjectID string
TerraformExecutionMode string
TerraformAgentPoolID string

VariableOptionsFile string

// testFunc is used for testing, so that the command can be tested without
// using the real API.
Expand Down
37 changes: 36 additions & 1 deletion internal/commands/waypoint/add-ons/definitions/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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 @@ -113,6 +114,27 @@ $ hcp waypoint add-ons definitions create -n=my-add-on-definition \
Value: flagvalue.Simple("", &opts.TerraformCloudProjectID),
Required: true,
},
{
Name: "variable-options-file",
DisplayValue: "VARIABLE_OPTIONS_FILE",
Description: "The file containing the HCL definition of Variable Options.",
Value: flagvalue.Simple("", &opts.VariableOptionsFile),
},
{
Name: "tf-execution-mode",
DisplayValue: "TF_EXECUTION_MODE",
Description: "The execution mode of the HCP Terraform " +
"workspaces for add-ons using this add-on definition.",
Value: flagvalue.Simple("remote", &opts.TerraformExecutionMode),
},
{
Name: "tf-agent-pool-id",
DisplayValue: "TF_AGENT_POOL_ID",
Description: "The ID of the Terraform agent pool to use for " +
"running Terraform operations. This is only applicable " +
"when the execution mode is set to 'agent'.",
Value: flagvalue.Simple("", &opts.TerraformAgentPoolID),
},
},
},
}
Expand All @@ -136,6 +158,18 @@ func addOnDefinitionCreate(opts *AddOnDefinitionOpts) error {
}
}

// read variable options file and parse hcl
var variables []*models.HashicorpCloudWaypointTFModuleVariable
if opts.VariableOptionsFile != "" {
variables, err = internal.ParseVariableOptionsFile(opts.VariableOptionsFile)
if err != nil {
return errors.Wrapf(err, "%s failed to read Variable Options hcl file %q",
opts.IO.ColorScheme().FailureIcon(),
opts.VariableOptionsFile,
)
}
}

_, err = opts.WS.WaypointServiceCreateAddOnDefinition(
&waypoint_service.WaypointServiceCreateAddOnDefinitionParams{
NamespaceID: ns.ID,
Expand All @@ -149,7 +183,8 @@ func addOnDefinitionCreate(opts *AddOnDefinitionOpts) error {
Name: opts.TerraformCloudProjectName,
ProjectID: opts.TerraformCloudProjectID,
},
ModuleSource: opts.TerraformNoCodeModuleSource,
ModuleSource: opts.TerraformNoCodeModuleSource,
VariableOptions: variables,
},
Context: opts.Ctx,
}, nil)
Expand Down
7 changes: 7 additions & 0 deletions internal/commands/waypoint/add-ons/definitions/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func TestCmdAddOnDefinitionCreate(t *testing.T) {
"-l", "cli",
"-d", "An add-on definition created with the CLI.",
"--readme-markdown-template-file", "readme_test.txt",
"--tf-execution-mode", "agent",
"--tf-agent-pool-id", "pool-abc123",
"--variable-options-file", "vars.hcl",
},
Expect: &AddOnDefinitionOpts{
Name: "cli-test",
Expand All @@ -63,6 +66,9 @@ func TestCmdAddOnDefinitionCreate(t *testing.T) {
TerraformNoCodeModuleSource: "private/waypoint/waypoint-nocode-module/null",
Labels: []string{"cli"},
ReadmeMarkdownTemplateFile: "readme_test.txt",
TerraformExecutionMode: "agent",
TerraformAgentPoolID: "pool-abc123",
VariableOptionsFile: "vars.hcl",
},
},
}
Expand Down Expand Up @@ -104,6 +110,7 @@ func TestCmdAddOnDefinitionCreate(t *testing.T) {
r.Equal(c.Expect.TerraformNoCodeModuleSource, aodOpts.TerraformNoCodeModuleSource)
r.Equal(c.Expect.ReadmeMarkdownTemplateFile, aodOpts.ReadmeMarkdownTemplateFile)
r.Equal(c.Expect.Labels, aodOpts.Labels)
r.Equal(c.Expect.VariableOptionsFile, aodOpts.VariableOptionsFile)
}
})
}
Expand Down
29 changes: 28 additions & 1 deletion internal/commands/waypoint/add-ons/definitions/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package definitions

import (
"strings"

"github.com/hashicorp/hcp-sdk-go/clients/cloud-waypoint-service/preview/2023-08-18/client/waypoint_service"
"github.com/hashicorp/hcp/internal/pkg/cmd"
"github.com/hashicorp/hcp/internal/pkg/flagvalue"
Expand Down Expand Up @@ -74,6 +76,31 @@ func addOnDefinitionRead(opts *AddOnDefinitionOpts) error {
}

getRespPayload := getResp.GetPayload()
if getRespPayload.AddOnDefinition == nil {
return errors.Errorf(
"%s empty add-on definition returned for name %q",
opts.IO.ColorScheme().FailureIcon(),
opts.Name,
)
}
addOnDef := getRespPayload.AddOnDefinition
var optionNames []string
for _, option := range addOnDef.VariableOptions {
optionNames = append(optionNames, option.Name)
}
optionNamesStr := strings.Join(optionNames, ", ")
fields := []format.Field{
format.NewField("ID", "{{ .ID }}"),
format.NewField("Name", "{{ .Name }}"),
format.NewField("Summary", "{{ .Summary }}"),
format.NewField("Description", "{{ .Description }}"),
format.NewField("Labels", "{{ .Labels }}"),
format.NewField("Terraform Cloud Workspace Details", "{{ .TerraformCloudWorkspaceDetails }}"),
format.NewField("Module Source", "{{ .ModuleSource }}"),
format.NewField("Execution Mode", "{{ .TfExecutionMode }}"),
format.NewField("Agent Pool ID", "{{ .TfAgentPoolID }}"),
format.NewField("Variable Options", optionNamesStr),
}

return opts.Output.Show(getRespPayload.AddOnDefinition, format.Pretty)
return opts.Output.Display(format.NewDisplayer(addOnDef, format.Pretty, fields))
}
38 changes: 38 additions & 0 deletions internal/commands/waypoint/add-ons/definitions/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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 @@ -91,12 +92,33 @@ $ hcp waypoint add-ons definitions update -n=my-add-on-definition \
Description: "The Terraform Cloud project ID.",
Value: flagvalue.Simple("", &opts.TerraformCloudProjectID),
},
{
Name: "variable-options-file",
DisplayValue: "VARIABLE_OPTIONS_FILE",
Description: "The file containing the HCL definition of Variable Options.",
Value: flagvalue.Simple("", &opts.VariableOptionsFile),
},
{
Name: "tfc-project-name",
DisplayValue: "TFC_PROJECT_NAME",
Description: "The Terraform Cloud project name.",
Value: flagvalue.Simple("", &opts.TerraformCloudProjectName),
},
{
Name: "tf-execution-mode",
DisplayValue: "TF_EXECUTION_MODE",
Description: "The execution mode of the HCP Terraform " +
"workspaces for add-ons using this add-on definition.",
Value: flagvalue.Simple("remote", &opts.TerraformExecutionMode),
},
{
Name: "tf-agent-pool-id",
DisplayValue: "TF_AGENT_POOL_ID",
Description: "The ID of the Terraform agent pool to use for " +
"running Terraform operations. This is only applicable " +
"when the execution mode is set to 'agent'.",
Value: flagvalue.Simple("", &opts.TerraformAgentPoolID),
},
},
},
}
Expand All @@ -121,6 +143,19 @@ func addOnDefinitionUpdate(opts *AddOnDefinitionOpts) error {
}
}

// read variable options file and parse hcl
var variables []*models.HashicorpCloudWaypointTFModuleVariable
if opts.VariableOptionsFile != "" {
vars, err := internal.ParseVariableOptionsFile(opts.VariableOptionsFile)
if err != nil {
return errors.Wrapf(err, "%s failed to read Variable Options hcl file %q",
opts.IO.ColorScheme().FailureIcon(),
opts.VariableOptionsFile,
)
}
variables = vars
}

_, err = opts.WS.WaypointServiceUpdateAddOnDefinition2(
&waypoint_service.WaypointServiceUpdateAddOnDefinition2Params{
NamespaceID: ns.ID,
Expand All @@ -135,6 +170,9 @@ func addOnDefinitionUpdate(opts *AddOnDefinitionOpts) error {
ProjectID: opts.TerraformCloudProjectID,
Name: opts.TerraformCloudProjectName,
},
TfExecutionMode: opts.TerraformExecutionMode,
TfAgentPoolID: opts.TerraformAgentPoolID,
VariableOptions: variables,
},
}, nil,
)
Expand Down
10 changes: 9 additions & 1 deletion internal/commands/waypoint/add-ons/definitions/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func TestCmdAddOnDefinitionUpdate(t *testing.T) {
"-l", "cli",
"-d", "An add-on definition created with the CLI.",
"--readme-markdown-template-file", "readme_test.txt",
"--tf-execution-mode", "agent",
"--tf-agent-pool-id", "pool-abc123",
"--variable-options-file", "vars.hcl",
},
Expect: &AddOnDefinitionOpts{
Name: "cli-test",
Expand All @@ -60,6 +63,9 @@ func TestCmdAddOnDefinitionUpdate(t *testing.T) {
TerraformCloudProjectName: "test",
Labels: []string{"cli"},
ReadmeMarkdownTemplateFile: "readme_test.txt",
TerraformExecutionMode: "agent",
TerraformAgentPoolID: "pool-abc123",
VariableOptionsFile: "vars.hcl",
},
},
}
Expand Down Expand Up @@ -99,8 +105,10 @@ func TestCmdAddOnDefinitionUpdate(t *testing.T) {
r.Equal(c.Expect.TerraformCloudProjectName, aodOpts.TerraformCloudProjectName)
r.Equal(c.Expect.Labels, aodOpts.Labels)
r.Equal(c.Expect.ReadmeMarkdownTemplateFile, aodOpts.ReadmeMarkdownTemplateFile)
r.Equal(c.Expect.TerraformExecutionMode, aodOpts.TerraformExecutionMode)
r.Equal(c.Expect.TerraformAgentPoolID, aodOpts.TerraformAgentPoolID)
r.Equal(c.Expect.VariableOptionsFile, aodOpts.VariableOptionsFile)
}
})

}
}
Loading

0 comments on commit eaca7fc

Please sign in to comment.