Skip to content

Commit

Permalink
Merge pull request #48 from hashicorp/WAYP-2309-waypoint-applications…
Browse files Browse the repository at this point in the history
…-cli

`hcp waypoint applications` CLI
  • Loading branch information
paladin-devops authored Apr 3, 2024
2 parents fb27381 + 78962bb commit 232b858
Show file tree
Hide file tree
Showing 38 changed files with 881 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .changelog/48.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
waypoint: Add `waypoint applications` CLI for managing HCP Waypoint applications.
```
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/hashicorp/go-hclog v1.5.0
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/hcl/v2 v2.19.1
github.com/hashicorp/hcp-sdk-go v0.89.0
github.com/hashicorp/hcp-sdk-go v0.90.1-0.20240329164050-71ea01d5b883
github.com/lithammer/dedent v1.1.0
github.com/manifoldco/promptui v0.9.0
github.com/mitchellh/cli v1.1.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/hcl/v2 v2.19.1 h1://i05Jqznmb2EXqa39Nsvyan2o5XyMowW5fnCKW5RPI=
github.com/hashicorp/hcl/v2 v2.19.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE=
github.com/hashicorp/hcp-sdk-go v0.89.0 h1:OpN2Yvr0YL/9kEMXpa1fvq5v7d66A4Vc4tilL/fwcwc=
github.com/hashicorp/hcp-sdk-go v0.89.0/go.mod h1:vQ4fzdL1AmhIAbCw+4zmFe5Hbpajj3NvRWkJoVuxmAk=
github.com/hashicorp/hcp-sdk-go v0.90.1-0.20240329164050-71ea01d5b883 h1:zL8s06fHaxLw31I4xYbAwpaPuAylrB6JtUaduDdRREo=
github.com/hashicorp/hcp-sdk-go v0.90.1-0.20240329164050-71ea01d5b883/go.mod h1:vQ4fzdL1AmhIAbCw+4zmFe5Hbpajj3NvRWkJoVuxmAk=
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package addon
package addons

import (
"github.com/hashicorp/hcp/internal/commands/waypoint/add-ons/definitions"
"github.com/hashicorp/hcp/internal/pkg/cmd"
"github.com/hashicorp/hcp/internal/pkg/heredoc"
)
Expand All @@ -15,7 +16,7 @@ manage HCP Waypoint add-ons and add-on definitions.
`),
}

cmd.AddChild(NewCmdAddOnDefinition(ctx))
cmd.AddChild(definitions.NewCmdAddOnDefinition(ctx))

return cmd
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package addon
package definitions

import (
"github.com/hashicorp/hcp/internal/commands/waypoint/opts"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package addon
package definitions

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package addon
package definitions

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package addon
package definitions

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package addon
package definitions

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package addon
package definitions

import (
"github.com/hashicorp/hcp-sdk-go/clients/cloud-waypoint-service/preview/2023-08-18/client/waypoint_service"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package addon
package definitions

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package addon
package definitions

import (
"github.com/hashicorp/hcp-sdk-go/clients/cloud-waypoint-service/preview/2023-08-18/client/waypoint_service"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package addon
package definitions

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package addon
package definitions

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package addon
package definitions

import (
"context"
Expand Down
41 changes: 41 additions & 0 deletions internal/commands/waypoint/applications/applications.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package applications

import (
"github.com/hashicorp/hcp/internal/commands/waypoint/opts"
"github.com/hashicorp/hcp/internal/pkg/cmd"
"github.com/hashicorp/hcp/internal/pkg/heredoc"
)

type ApplicationOpts struct {
opts.WaypointOpts

Name string
TemplateName string
ActionConfigNames []string
ReadmeMarkdownFile string

testFunc func(c *cmd.Command, args []string) error
}

func NewCmdApplications(ctx *cmd.Context) *cmd.Command {
opts := &ApplicationOpts{
WaypointOpts: opts.New(ctx),
}

cmd := &cmd.Command{
Name: "applications",
ShortHelp: "Manage HCP Waypoint applications.",
LongHelp: heredoc.New(ctx.IO).Must(`
The {{ template "mdCodeOrBold" "hcp waypoint applications" }} command group lets you manage
HCP Waypoint applications.
`),
}

cmd.AddChild(NewCmdApplicationsCreate(ctx, opts))
cmd.AddChild(NewCmdApplicationsDestroy(ctx, opts))
cmd.AddChild(NewCmdApplicationsList(ctx, opts))
cmd.AddChild(NewCmdApplicationsRead(ctx, opts))
cmd.AddChild(NewCmdApplicationsUpdate(ctx, opts))

return cmd
}
104 changes: 104 additions & 0 deletions internal/commands/waypoint/applications/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package applications

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 NewCmdApplicationsCreate(ctx *cmd.Context, opts *ApplicationOpts) *cmd.Command {
cmd := &cmd.Command{
Name: "create",
ShortHelp: "Create a new HCP Waypoint application.",
LongHelp: heredoc.New(ctx.IO).Must(`
The {{ template "mdCodeOrBold" "hcp waypoint applications create" }} command lets you create
a new HCP Waypoint application.
`),
Examples: []cmd.Example{
{
Preamble: "Create a new HCP Waypoint application:",
Command: heredoc.New(ctx.IO, heredoc.WithPreserveNewlines()).Must(`
$ hcp waypoint application create -n my-application -t my-templates
`),
},
},
RunF: func(c *cmd.Command, args []string) error {
if opts.testFunc != nil {
return opts.testFunc(c, args)
}
return applicationCreate(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 application.",
Value: flagvalue.Simple("", &opts.Name),
Required: true,
},
{
Name: "templates-name",
Shorthand: "t",
DisplayValue: "TEMPLATE_NAME",
Description: "The name of the templates to use for the application.",
Value: flagvalue.Simple("", &opts.TemplateName),
Required: true,
},
{
Name: "action-config-name",
DisplayValue: "ACTION_CONFIG_NAME",
Description: "The name of the action configuration to be added to the application.",
Value: flagvalue.SimpleSlice(nil, &opts.ActionConfigNames),
Required: false,
Repeatable: true,
},
},
},
}

return cmd
}

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

actionConfigs := make([]*models.HashicorpCloudWaypointActionCfgRef, len(opts.ActionConfigNames))
for i, name := range opts.ActionConfigNames {
actionConfigs[i] = &models.HashicorpCloudWaypointActionCfgRef{
Name: name,
}
}

_, err = opts.WS.WaypointServiceCreateApplicationFromTemplate(
&waypoint_service.WaypointServiceCreateApplicationFromTemplateParams{
NamespaceID: ns.ID,
Context: opts.Ctx,
Body: &models.HashicorpCloudWaypointWaypointServiceCreateApplicationFromTemplateBody{
Name: opts.Name,
ApplicationTemplate: &models.HashicorpCloudWaypointRefApplicationTemplate{
Name: opts.TemplateName,
},
ActionCfgRefs: actionConfigs,
},
}, nil)
if err != nil {
return errors.Wrapf(err, "failed to create application %q", opts.Name)
}

fmt.Fprintf(opts.IO.Err(), "Application %q created.", opts.Name)

return nil
}
87 changes: 87 additions & 0 deletions internal/commands/waypoint/applications/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package applications

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

cases := []struct {
Name string
Args []string
Profile func(t *testing.T) *profile.Profile
Error string
Expect *ApplicationOpts
}{
{
Name: "No Org",
Profile: profile.TestProfile,
Args: []string{},
Error: "Organization ID must be configured",
},
{
Name: "No Name",
Profile: profile.TestProfile,
Args: []string{"-t", "templates-name"},
Error: "The name of the application is required",
},
{
Name: "No Template Name",
Profile: profile.TestProfile,
Args: []string{"-n", "app-name"},
Error: "The name of the templates to use for the application is required",
},
{
Name: "Happy",
Profile: profile.TestProfile,
Args: []string{"-n", "app-name", "-t", "templates-name"},
Expect: &ApplicationOpts{
Name: "app-name",
TemplateName: "templates-name",
},
},
}

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

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

r := require.New(t)

// 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 appOpts ApplicationOpts
appOpts.testFunc = func(c *cmd.Command, args []string) error {
return nil
}
cmd := NewCmdApplicationsCreate(ctx, &appOpts)
cmd.SetIO(io)

cmd.Run(c.Args)

if c.Expect != nil {
r.Equal(c.Expect.Name, appOpts.Name)
r.Equal(c.Expect.TemplateName, appOpts.TemplateName)
}
})
}
}
Loading

0 comments on commit 232b858

Please sign in to comment.