Skip to content

Commit

Permalink
Merge pull request #42 from hashicorp/WAYP-2344-delete-list-update-re…
Browse files Browse the repository at this point in the history
…ad-add-on-definitions

Add commands to `read`, `list`, `update`, and `delete` HCP Waypoint add-on definitions
  • Loading branch information
paladin-devops authored Mar 25, 2024
2 parents 5a2cd52 + b7e7e8c commit 0597a44
Show file tree
Hide file tree
Showing 11 changed files with 732 additions and 2 deletions.
4 changes: 4 additions & 0 deletions internal/commands/waypoint/add-on/add_on_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ group lets you manage HCP Waypoint add-on definitions.
}

cmd.AddChild(NewCmdAddOnDefinitionCreate(ctx, opts))
cmd.AddChild(NewCmdAddOnDefinitionDelete(ctx, opts))
cmd.AddChild(NewCmdAddOnDefinitionList(ctx, opts))
cmd.AddChild(NewCmdAddOnDefinitionRead(ctx, opts))
cmd.AddChild(NewCmdAddOnDefinitionUpdate(ctx, opts))

return cmd
}
75 changes: 75 additions & 0 deletions internal/commands/waypoint/add-on/add_on_definition_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package addon

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 NewCmdAddOnDefinitionDelete(ctx *cmd.Context, opts *AddOnDefinitionOpts) *cmd.Command {
cmd := &cmd.Command{
Name: "delete",
ShortHelp: "Delete an HCP Waypoint add-on definition.",
LongHelp: heredoc.New(ctx.IO).Must(`
The {{ template "mdCodeOrBold" "hcp waypoint add-ons definitions delete" }}
command lets you delete an existing HCP Waypoint add-on definition.
`),
Examples: []cmd.Example{
{
Preamble: "Delete an HCP Waypoint add-on definition:",
Command: heredoc.New(ctx.IO, heredoc.WithPreserveNewlines()).Must(`
$ hcp waypoint add-ons definitions delete -n my-addon-definition
`),
},
},
RunF: func(c *cmd.Command, args []string) error {
if opts.testFunc != nil {
return opts.testFunc(c, args)
}
return addOnDefinitionDelete(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 to be deleted.",
Value: flagvalue.Simple("", &opts.Name),
Required: true,
},
},
},
}

return cmd
}

func addOnDefinitionDelete(opts *AddOnDefinitionOpts) error {
ns, err := opts.Namespace()
if err != nil {
return err
}

_, err = opts.WS.WaypointServiceDeleteAddOnDefinition2(
&waypoint_service.WaypointServiceDeleteAddOnDefinition2Params{
NamespaceID: ns.ID,
Context: opts.Ctx,
AddOnDefinitionName: opts.Name,
}, nil,
)
if err != nil {
return errors.Wrapf(err, "failed to delete add-on definition %q", opts.Name)
}

fmt.Fprintf(opts.IO.Err(), "Add-on definition %q deleted.", opts.Name)

return nil
}
84 changes: 84 additions & 0 deletions internal/commands/waypoint/add-on/add_on_definition_delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package addon

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 TestCmdAddOnDefinitionDelete(t *testing.T) {
t.Parallel()

cases := []struct {
Name string
Args []string
Profile func(t *testing.T) *profile.Profile
Error string
Expect *AddOnDefinitionOpts
}{
{
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",
},
Expect: &AddOnDefinitionOpts{
Name: "cli-test",
},
},
}

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),
Output: format.New(io),
HCP: &client.Runtime{},
ShutdownCtx: context.Background(),
}

var aodOpts AddOnDefinitionOpts
aodOpts.testFunc = func(c *cmd.Command, args []string) error {
return nil
}
cmd := NewCmdAddOnDefinitionDelete(ctx, &aodOpts)
cmd.SetIO(io)

cmd.Run(c.Args)

if c.Expect != nil {
r.Equal(c.Expect.Name, aodOpts.Name)
}
})
}
}
77 changes: 77 additions & 0 deletions internal/commands/waypoint/add-on/add_on_definition_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package addon

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/pkg/cmd"
"github.com/hashicorp/hcp/internal/pkg/format"
"github.com/hashicorp/hcp/internal/pkg/heredoc"
"github.com/pkg/errors"
)

func NewCmdAddOnDefinitionList(ctx *cmd.Context, opts *AddOnDefinitionOpts) *cmd.Command {
cmd := &cmd.Command{
Name: "list",
ShortHelp: "List all known HCP Waypoint add-on definitions.",
LongHelp: heredoc.New(ctx.IO).Must(`
The {{ template "mdCodeOrBold" "hcp waypoint add-ons definitions list" }}
command lets you list all existing HCP Waypoint add-on definitions.
`),
Examples: []cmd.Example{
{
Preamble: "List all known HCP Waypoint add-on definitions:",
Command: heredoc.New(ctx.IO, heredoc.WithPreserveNewlines()).Must(`
$ hcp waypoint add-ons definitions list
`),
},
},
RunF: func(c *cmd.Command, args []string) error {
if opts.testFunc != nil {
return opts.testFunc(c, args)
}
return addOnDefinitionsList(opts)
},
PersistentPreRun: func(c *cmd.Command, args []string) error {
return cmd.RequireOrgAndProject(ctx)
},
}

return cmd
}

func addOnDefinitionsList(opts *AddOnDefinitionOpts) error {
ns, err := opts.Namespace()
if err != nil {
return err
}

var addOnDefinitions []*models.HashicorpCloudWaypointAddOnDefinition

listResp, err := opts.WS.WaypointServiceListAddOnDefinitions(
&waypoint_service.WaypointServiceListAddOnDefinitionsParams{
NamespaceID: ns.ID,
Context: opts.Ctx,
}, nil,
)
if err != nil {
return errors.Wrap(err, "failed to list add-on definitions")
}

addOnDefinitions = append(addOnDefinitions, listResp.GetPayload().AddOnDefinitions...)

for listResp.GetPayload().Pagination.NextPageToken != "" {
listResp, err = opts.WS.WaypointServiceListAddOnDefinitions(
&waypoint_service.WaypointServiceListAddOnDefinitionsParams{
NamespaceID: ns.ID,
Context: opts.Ctx,
PaginationNextPageToken: &listResp.GetPayload().Pagination.NextPageToken,
}, nil)
if err != nil {
return errors.Wrapf(err, "failed to list paginated add-on definitions")
}

addOnDefinitions = append(addOnDefinitions, listResp.GetPayload().AddOnDefinitions...)
}

return opts.Output.Show(addOnDefinitions, format.Pretty)
}
60 changes: 60 additions & 0 deletions internal/commands/waypoint/add-on/add_on_definition_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package addon

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"
)

func TestNewCmdAddOnDefinitionList(t *testing.T) {
t.Parallel()

cases := []struct {
Name string
Args []string
Profile func(t *testing.T) *profile.Profile
Error string
Expect *AddOnDefinitionOpts
}{
{
Name: "No Org",
Profile: profile.TestProfile,
Args: []string{},
Error: "Organization ID must be configured",
},
// there's no args for the list command right now, but if that changes,
// we should add a test case here
}

for _, c := range cases {
c := c

t.Run(c.Name, func(t *testing.T) {
t.Parallel()

// Create a context.
io := iostreams.Test()
ctx := &cmd.Context{
IO: io,
Profile: c.Profile(t),
Output: format.New(io),
HCP: &client.Runtime{},
ShutdownCtx: context.Background(),
}

var tplOpts AddOnDefinitionOpts
tplOpts.testFunc = func(c *cmd.Command, args []string) error {
return nil
}
cmd := NewCmdAddOnDefinitionList(ctx, &tplOpts)
cmd.SetIO(io)

cmd.Run(c.Args)
})
}
}
73 changes: 73 additions & 0 deletions internal/commands/waypoint/add-on/add_on_definition_read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package addon

import (
"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/format"
"github.com/hashicorp/hcp/internal/pkg/heredoc"
"github.com/pkg/errors"
)

func NewCmdAddOnDefinitionRead(ctx *cmd.Context, opts *AddOnDefinitionOpts) *cmd.Command {
cmd := &cmd.Command{
Name: "read",
ShortHelp: "Read an HCP Waypoint add-on definition.",
LongHelp: heredoc.New(ctx.IO).Must(`
The {{ template "mdCodeOrBold" "hcp waypoint add-ons definitions read" }}
command lets you read an existing HCP Waypoint add-on definition.
`),
Examples: []cmd.Example{
{
Preamble: "Read an HCP Waypoint add-on definition:",
Command: heredoc.New(ctx.IO, heredoc.WithPreserveNewlines()).Must(`
$ hcp waypoint add-ons definitions read -n my-addon-definition
`),
},
},
RunF: func(c *cmd.Command, args []string) error {
if opts.testFunc != nil {
return opts.testFunc(c, args)
}
return addOnDefinitionRead(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),
Required: true,
},
},
},
}
return cmd
}

func addOnDefinitionRead(opts *AddOnDefinitionOpts) error {
ns, err := opts.Namespace()
if err != nil {
return errors.Wrap(err, "unable to access HCP project")
}

getResp, err := opts.WS.WaypointServiceGetAddOnDefinition2(
&waypoint_service.WaypointServiceGetAddOnDefinition2Params{
NamespaceID: ns.ID,
Context: opts.Ctx,
AddOnDefinitionName: opts.Name,
}, nil,
)
if err != nil {
return errors.Wrapf(err, "failed to get add-on definition %q", opts.Name)
}

getRespPayload := getResp.GetPayload()

return opts.Output.Show(getRespPayload.AddOnDefinition, format.Pretty)
}
Loading

0 comments on commit 0597a44

Please sign in to comment.