-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WAYP-2345] waypoint: Add create command for add-on definitions.
- Loading branch information
1 parent
a0875b5
commit a8cbf0e
Showing
4 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package addon | ||
|
||
import "github.com/hashicorp/hcp/internal/pkg/cmd" | ||
|
||
func NewCmdAddOn(ctx *cmd.Context) *cmd.Command { | ||
cmd := &cmd.Command{ | ||
Name: "add-ons", | ||
ShortHelp: "Manage HCP Waypoint add-ons and add-on definitions.", | ||
LongHelp: "Manage HCP Waypoint add-ons. Add-ons are units of infrastructure" + | ||
" that can be deployed alongside applications. They can be used to deploy" + | ||
" databases, caches, and other infrastructure alongside applications.", | ||
} | ||
|
||
cmd.AddChild(NewCmdAddOnDefinition(ctx)) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package addon | ||
|
||
import ( | ||
"github.com/hashicorp/hcp/internal/commands/waypoint/opts" | ||
"github.com/hashicorp/hcp/internal/pkg/cmd" | ||
) | ||
|
||
type AddOnDefinitionOpts struct { | ||
opts.WaypointOpts | ||
|
||
Name string | ||
Summary string | ||
Description string | ||
ReadmeMarkdownTemplateFile string | ||
Labels []string | ||
|
||
TerraformNoCodeModuleSource string | ||
TerraformNoCodeModuleVersion string | ||
TerraformCloudProjectName string | ||
TerraformCloudProjectID string | ||
|
||
// testFunc is used for testing, so that the command can be tested without | ||
// using the real API. | ||
testFunc func(c *cmd.Command, args []string) error | ||
} | ||
|
||
func NewCmdAddOnDefinition(ctx *cmd.Context) *cmd.Command { | ||
opts := &AddOnDefinitionOpts{ | ||
WaypointOpts: opts.New(ctx), | ||
} | ||
|
||
cmd := &cmd.Command{ | ||
Name: "definitions", | ||
ShortHelp: "Manage HCP Waypoint add-on definitions.", | ||
LongHelp: "Manage HCP Waypoint add-on definitions. Add-on definitions " + | ||
"are reusable configurations for creating add-ons.", | ||
} | ||
|
||
cmd.AddChild(NewCmdAddOnDefinitionCreate(ctx, opts)) | ||
|
||
return cmd | ||
} |
152 changes: 152 additions & 0 deletions
152
internal/commands/waypoint/add-on/add_on_definition_create.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package addon | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"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/pkg/cmd" | ||
"github.com/hashicorp/hcp/internal/pkg/flagvalue" | ||
"github.com/hashicorp/hcp/internal/pkg/heredoc" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func NewCmdAddOnDefinitionCreate(ctx *cmd.Context, opts *AddOnDefinitionOpts) *cmd.Command { | ||
cmd := &cmd.Command{ | ||
Name: "create", | ||
ShortHelp: "Create a new add-on definition.", | ||
LongHelp: "Create a new add-on definition.", | ||
Examples: []cmd.Example{ | ||
// TODO: Add example | ||
}, | ||
RunF: func(c *cmd.Command, args []string) error { | ||
if opts.testFunc != nil { | ||
return opts.testFunc(c, args) | ||
} | ||
return addOnDefinitionCreate(opts) | ||
}, | ||
PersistentPreRun: func(c *cmd.Command, args []string) error { | ||
return cmd.RequireOrgAndProject(ctx) | ||
}, | ||
Flags: cmd.Flags{ | ||
Local: []*cmd.Flag{ | ||
{ | ||
Name: "name", | ||
Shorthand: "n", | ||
DisplayValue: "NAME", | ||
Description: "The name of the add-on definition.", | ||
Value: flagvalue.Simple("", &opts.Name), | ||
}, | ||
{ | ||
Name: "summary", | ||
Shorthand: "s", | ||
DisplayValue: "SUMMARY", | ||
Description: "The summary of the add-on definition.", | ||
Value: flagvalue.Simple("", &opts.Summary), | ||
}, | ||
{ | ||
Name: "description", | ||
Shorthand: "d", | ||
DisplayValue: "DESCRIPTION", | ||
Description: "The description of the template.", | ||
Value: flagvalue.Simple("", &opts.Description), | ||
}, | ||
{ | ||
Name: "readme-markdown-template-file", | ||
DisplayValue: "README_MARKDOWN_TEMPLATE_FILE_PATH", | ||
Description: "The file containing the README markdown template.", | ||
Value: flagvalue.Simple("", &opts.ReadmeMarkdownTemplateFile), | ||
}, | ||
{ | ||
Name: "label", | ||
Shorthand: "l", | ||
DisplayValue: "LABEL", | ||
Description: "A label to apply to the template.", | ||
Repeatable: true, | ||
Value: flagvalue.SimpleSlice(nil, &opts.Labels), | ||
}, | ||
{ | ||
Name: "tfc-no-code-module-source", | ||
DisplayValue: "TFC_NO_CODE_MODULE_SOURCE", | ||
Description: heredoc.New(ctx.IO).Must(` | ||
The source of the Terraform no-code module. | ||
The expected format is "NAMESPACE/NAME/PROVIDER". An | ||
optional "HOSTNAME/" can be added at the beginning for | ||
a private registry. | ||
`), | ||
Value: flagvalue.Simple("", &opts.TerraformNoCodeModuleSource), | ||
Required: true, | ||
}, | ||
{ | ||
Name: "tfc-no-code-module-version", | ||
DisplayValue: "TFC_NO_CODE_MODULE_VERSION", | ||
Description: "The version of the Terraform no-code module.", | ||
Value: flagvalue.Simple("", &opts.TerraformNoCodeModuleVersion), | ||
Required: true, | ||
}, | ||
{ | ||
Name: "tfc-project-name", | ||
DisplayValue: "TFC_PROJECT_NAME", | ||
Description: "The name of the Terraform Cloud project where" + | ||
" applications using this template will be created.", | ||
Value: flagvalue.Simple("", &opts.TerraformCloudProjectName), | ||
Required: true, | ||
}, | ||
{ | ||
Name: "tfc-project-id", | ||
DisplayValue: "TFC_PROJECT_ID", | ||
Description: "The ID of the Terraform Cloud project where" + | ||
" applications using this template will be created.", | ||
Value: flagvalue.Simple("", &opts.TerraformCloudProjectID), | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func addOnDefinitionCreate(opts *AddOnDefinitionOpts) error { | ||
ns, err := opts.Namespace() | ||
if err != nil { | ||
return errors.Wrapf(err, "Unable to access HCP project") | ||
} | ||
|
||
var readmeTpl []byte | ||
if opts.ReadmeMarkdownTemplateFile != "" { | ||
readmeTpl, err = os.ReadFile(opts.ReadmeMarkdownTemplateFile) | ||
if err != nil { | ||
return fmt.Errorf("failed to read README markdown template file: %w", err) | ||
} | ||
} | ||
|
||
_, err = opts.WS.WaypointServiceCreateAddOnDefinition( | ||
&waypoint_service.WaypointServiceCreateAddOnDefinitionParams{ | ||
NamespaceID: ns.ID, | ||
Body: &models.HashicorpCloudWaypointWaypointServiceCreateAddOnDefinitionBody{ | ||
Name: opts.Name, | ||
Summary: opts.Summary, | ||
Description: opts.Description, | ||
ReadmeMarkdownTemplate: readmeTpl, | ||
Labels: opts.Labels, | ||
TerraformNocodeModule: &models.HashicorpCloudWaypointTerraformNocodeModule{ | ||
Source: opts.TerraformNoCodeModuleSource, | ||
Version: opts.TerraformNoCodeModuleVersion, | ||
}, | ||
TerraformCloudWorkspaceDetails: &models.HashicorpCloudWaypointTerraformCloudWorkspaceDetails{ | ||
Name: opts.TerraformCloudProjectName, | ||
ProjectID: opts.TerraformCloudProjectID, | ||
}, | ||
}, | ||
Context: opts.Ctx, | ||
}, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed to create add-on definition %q: %w", opts.Name, err) | ||
} | ||
|
||
fmt.Fprintf(opts.IO.Err(), "Template %q created.", opts.Name) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters