-
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.
Merge pull request #52 from hashicorp/WAYP-2339-add-ons
`hcp waypoint add-ons` CLI
- Loading branch information
Showing
24 changed files
with
741 additions
and
45 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,3 @@ | ||
```release-note:feature | ||
waypoint: Add `hcp waypoint add-ons` CLI for managing HCP Waypoint add-ons. | ||
``` |
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
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,100 @@ | ||
package addons | ||
|
||
import ( | ||
"fmt" | ||
|
||
"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 NewCmdCreate(ctx *cmd.Context, opts *AddOnOpts) *cmd.Command { | ||
cmd := &cmd.Command{ | ||
Name: "create", | ||
ShortHelp: "Create a new HCP Waypoint add-on.", | ||
LongHelp: heredoc.New(ctx.IO).Must(` | ||
The {{ template "mdCodeOrBold" "hcp waypoint add-ons create" }} command creates a new HCP Waypoint add-on. | ||
`), | ||
Examples: []cmd.Example{ | ||
{ | ||
Preamble: "Create a new HCP Waypoint add-on:", | ||
Command: heredoc.New(ctx.IO, heredoc.WithPreserveNewlines()).Must(` | ||
$ hcp waypoint add-ons create -n=my-addon -a=my-application -d=my-addon-definition | ||
`), | ||
}, | ||
}, | ||
AdditionalDocs: nil, | ||
PersistentPreRun: func(c *cmd.Command, args []string) error { | ||
return cmd.RequireOrgAndProject(ctx) | ||
}, | ||
RunF: func(c *cmd.Command, args []string) error { | ||
if opts.testFunc != nil { | ||
return opts.testFunc(c, args) | ||
} | ||
return addOnCreate(opts) | ||
}, | ||
Flags: cmd.Flags{ | ||
Local: []*cmd.Flag{ | ||
{ | ||
Name: "name", | ||
Shorthand: "n", | ||
DisplayValue: "NAME", | ||
Description: "The name of the add-on. If no name is provided," + | ||
" a name will be generated.", | ||
Value: flagvalue.Simple("", &opts.Name), | ||
Required: false, | ||
}, | ||
{ | ||
Name: "add-on-definition-name", | ||
DisplayValue: "NAME", | ||
Description: "The name of the add-on definition to use.", | ||
Value: flagvalue.Simple("", &opts.AddOnDefinitionName), | ||
Required: true, | ||
}, | ||
{ | ||
Name: "application-name", | ||
DisplayValue: "NAME", | ||
Description: "The name of the application to which the add-on will be added.", | ||
Value: flagvalue.Simple("", &opts.ApplicationName), | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func addOnCreate(opts *AddOnOpts) error { | ||
ns, err := opts.Namespace() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = opts.WS.WaypointServiceCreateAddOn( | ||
&waypoint_service.WaypointServiceCreateAddOnParams{ | ||
NamespaceID: ns.ID, | ||
Context: opts.Ctx, | ||
Body: &models.HashicorpCloudWaypointWaypointServiceCreateAddOnBody{ | ||
Application: &models.HashicorpCloudWaypointRefApplication{ | ||
Name: opts.ApplicationName, | ||
}, | ||
Definition: &models.HashicorpCloudWaypointRefAddOnDefinition{ | ||
Name: opts.AddOnDefinitionName, | ||
}, | ||
Name: opts.Name, | ||
}, | ||
}, nil) | ||
if err != nil { | ||
return errors.Wrapf(err, "%s failed to create add-on %q", | ||
opts.IO.ColorScheme().FailureIcon(), | ||
opts.Name) | ||
} | ||
|
||
fmt.Fprintf(opts.IO.Err(), "%s Add-on %q created!\n", opts.IO.ColorScheme().SuccessIcon(), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package addons | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/go-openapi/runtime/client" | ||
"github.com/hashicorp/hcp/internal/pkg/cmd" | ||
"github.com/hashicorp/hcp/internal/pkg/format" | ||
"github.com/hashicorp/hcp/internal/pkg/iostreams" | ||
"github.com/hashicorp/hcp/internal/pkg/profile" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewCmdCreate(t *testing.T) { | ||
t.Parallel() | ||
|
||
cases := []struct { | ||
Name string | ||
Args []string | ||
Profile func(t *testing.T) *profile.Profile | ||
Error string | ||
Expect *AddOnOpts | ||
}{ | ||
{ | ||
Name: "No Org", | ||
Profile: profile.TestProfile, | ||
Args: []string{}, | ||
Error: "Organization ID must be configured", | ||
}, | ||
{ | ||
Name: "no args", | ||
Profile: func(t *testing.T) *profile.Profile { | ||
return profile.TestProfile(t).SetOrgID("123") | ||
}, | ||
Args: []string{}, | ||
Error: "accepts 1 arg(s), received 0", | ||
}, | ||
{ | ||
Name: "happy", | ||
Profile: func(t *testing.T) *profile.Profile { | ||
return profile.TestProfile(t).SetOrgID("123") | ||
}, | ||
Args: []string{ | ||
"-n=cli-test", | ||
"--application-name=testApp", | ||
"--add-on-definition-name=testAddOnDefinition", | ||
}, | ||
Expect: &AddOnOpts{ | ||
Name: "cli-test", | ||
ApplicationName: "testApp", | ||
AddOnDefinitionName: "testAddOnDefinition", | ||
}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
c := c | ||
t.Run(c.Name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
r := require.New(t) | ||
|
||
io := iostreams.Test() | ||
ctx := &cmd.Context{ | ||
IO: io, | ||
Profile: c.Profile(t), | ||
ShutdownCtx: context.Background(), | ||
HCP: &client.Runtime{}, | ||
Output: format.New(io), | ||
} | ||
|
||
var addOnOpts AddOnOpts | ||
addOnOpts.testFunc = func(c *cmd.Command, args []string) error { | ||
return nil | ||
} | ||
cmd := NewCmdCreate(ctx, &addOnOpts) | ||
cmd.SetIO(io) | ||
|
||
cmd.Run(c.Args) | ||
|
||
if c.Expect != nil { | ||
r.Equal(c.Expect.Name, addOnOpts.Name) | ||
r.Equal(c.Expect.ApplicationName, addOnOpts.ApplicationName) | ||
r.Equal(c.Expect.AddOnDefinitionName, addOnOpts.AddOnDefinitionName) | ||
} | ||
}) | ||
} | ||
} |
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
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
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
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
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,76 @@ | ||
package addons | ||
|
||
import ( | ||
"fmt" | ||
|
||
"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" | ||
"github.com/hashicorp/hcp/internal/pkg/heredoc" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func NewCmdDestroy(ctx *cmd.Context, opts *AddOnOpts) *cmd.Command { | ||
cmd := &cmd.Command{ | ||
Name: "destroy", | ||
ShortHelp: "Destroy an HCP Waypoint add-ons.", | ||
LongHelp: heredoc.New(ctx.IO).Must(` | ||
The {{ template "mdCodeOrBold" "hcp waypoint add-ons destroy" }} command lets you | ||
destroy an existing HCP Waypoint add-on. | ||
`), | ||
Examples: []cmd.Example{ | ||
{ | ||
Preamble: "Destroy an HCP Waypoint add-on:", | ||
Command: heredoc.New(ctx.IO, heredoc.WithPreserveNewlines()).Must(` | ||
$ hcp waypoint add-ons destroy -n=my-addon | ||
`), | ||
}, | ||
}, | ||
RunF: func(c *cmd.Command, args []string) error { | ||
if opts.testFunc != nil { | ||
return opts.testFunc(c, args) | ||
} | ||
return addOnDestroy(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 to destroy.", | ||
Value: flagvalue.Simple("", &opts.Name), | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func addOnDestroy(opts *AddOnOpts) error { | ||
ns, err := opts.Namespace() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = opts.WS.WaypointServiceDestroyAddOn2( | ||
&waypoint_service.WaypointServiceDestroyAddOn2Params{ | ||
NamespaceID: ns.ID, | ||
Context: opts.Ctx, | ||
AddOnName: opts.Name, | ||
}, nil, | ||
) | ||
if err != nil { | ||
return errors.Wrapf(err, "%s failed to destroy add-on %q", | ||
opts.IO.ColorScheme().FailureIcon(), | ||
opts.Name) | ||
} | ||
|
||
fmt.Fprintf(opts.IO.Out(), "Add-on %s destroyed\n", opts.Name) | ||
|
||
return nil | ||
} |
Oops, something went wrong.