Skip to content

Commit

Permalink
Do initial work on adding the waypoint agent
Browse files Browse the repository at this point in the history
  • Loading branch information
evanphx committed Feb 7, 2024
1 parent 611e6c7 commit b87fb8c
Show file tree
Hide file tree
Showing 16 changed files with 1,736 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/commands/hcp/hcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/hcp/internal/commands/organizations"
"github.com/hashicorp/hcp/internal/commands/profile"
"github.com/hashicorp/hcp/internal/commands/projects"
"github.com/hashicorp/hcp/internal/commands/waypoint"
"github.com/hashicorp/hcp/internal/pkg/cmd"
)

Expand All @@ -22,6 +23,7 @@ func NewCmdHcp(ctx *cmd.Context) *cmd.Command {
c.AddChild(profile.NewCmdProfile(ctx))
c.AddChild(organizations.NewCmdOrganizations(ctx))
c.AddChild(iam.NewCmdIam(ctx))
c.AddChild(waypoint.NewCmdIam(ctx))

// Configure the command as the root command.
cmd.ConfigureRootCommand(ctx, c)
Expand Down
25 changes: 25 additions & 0 deletions internal/commands/waypoint/agent/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package agent

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

func NewCmdAgent(ctx *cmd.Context) *cmd.Command {
cmd := &cmd.Command{
Name: "agent",
ShortHelp: "Waypoint Agent.",
LongHelp: heredoc.New(ctx.IO).Must(`
The {{ Bold "waypoint agent" }} command group allows you to run and manage a local Waypoint
agent.
Agents are used in conjunction with HCP Waypoint Actions to allow actions to run on your
own systems when initiated from HCP Waypoint.
`),
}

cmd.AddChild(NewCmdRun(ctx))
cmd.AddChild(NewCmdQueue(ctx))
cmd.AddChild(NewCmdGroup(ctx))
return cmd
}
239 changes: 239 additions & 0 deletions internal/commands/waypoint/agent/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
package agent

import (
"context"
"fmt"

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

type GroupOpts struct {
Ctx context.Context
Profile *profile.Profile
IO iostreams.IOStreams
Output *format.Outputter

WS waypoint_service.ClientService

Name string
Description string
}

func NewCmdGroup(ctx *cmd.Context) *cmd.Command {
opts := &GroupOpts{
Ctx: ctx.ShutdownCtx,
Profile: ctx.Profile,
IO: ctx.IO,
Output: ctx.Output,
WS: waypoint_service.New(ctx.HCP, nil),
}

cmd := &cmd.Command{
Name: "group",
ShortHelp: "Manage HCP Waypoint Agent groups.",
LongHelp: heredoc.New(ctx.IO).Must(`
The {{ Bold "waypoint agent group" }} commands manage agent grousp.
`),
}

cmd.AddChild(NewCmdGroupCreate(ctx, opts))
cmd.AddChild(NewCmdGroupList(ctx, opts))
cmd.AddChild(NewCmdGroupDelete(ctx, opts))

return cmd
}

func NewCmdGroupCreate(ctx *cmd.Context, opts *GroupOpts) *cmd.Command {
cmd := &cmd.Command{
Name: "create",
ShortHelp: "Create a new HCP Waypoint Agent group.",
LongHelp: heredoc.New(ctx.IO).Must(`
The {{ Bold "waypoint agent group create" }} command creates a new Agent group.
`),
Flags: cmd.Flags{
Local: []*cmd.Flag{
{
Name: "name",
Shorthand: "n",
DisplayValue: "NAME",
Description: "Name for the new group.",
Value: flagvalue.Simple("", &opts.Name),
Required: true,
},
{
Name: "description",
Shorthand: "d",
DisplayValue: "TEXT",
Description: "Description for the group.",
Value: flagvalue.Simple("", &opts.Description),
},
},
},
RunF: func(c *cmd.Command, args []string) error {
return agentGroupCreate(c.Logger(), opts)
},
}

return cmd
}

func agentGroupCreate(log hclog.Logger, opts *GroupOpts) error {
resp, err := opts.WS.WaypointServiceGetNamespace(&waypoint_service.WaypointServiceGetNamespaceParams{
LocationOrganizationID: opts.Profile.OrganizationID,
LocationProjectID: opts.Profile.ProjectID,
Context: opts.Ctx,
}, nil)
if err != nil {
return errors.Wrapf(err, "Unable to access HCP project")
}

ns := resp.Payload.Namespace

ctx := opts.Ctx

_, err = opts.WS.WaypointServiceCreateAgentGroup(&waypoint_service.WaypointServiceCreateAgentGroupParams{
NamespaceID: ns.ID,
Body: &models.HashicorpCloudWaypointWaypointServiceCreateAgentGroupBody{
Description: opts.Description,
Name: opts.Name,
},
Context: ctx,
}, nil)

if err != nil {
fmt.Fprintf(opts.IO.Err(), "Error listing groups: %s", err)
return err
}

return nil
}

func NewCmdGroupDelete(ctx *cmd.Context, opts *GroupOpts) *cmd.Command {
cmd := &cmd.Command{
Name: "delete",
ShortHelp: "Delete a HCP Waypoint Agent group.",
LongHelp: heredoc.New(ctx.IO).Must(`
The {{ Bold "waypoint agent group delete" }} command delete an Agent group.
`),
Flags: cmd.Flags{
Local: []*cmd.Flag{
{
Name: "name",
Shorthand: "n",
DisplayValue: "NAME",
Description: "Name for the new group.",
Value: flagvalue.Simple("", &opts.Name),
Required: true,
},
},
},
RunF: func(c *cmd.Command, args []string) error {
return agentGroupDelete(c.Logger(), opts)
},
}

return cmd
}

func agentGroupDelete(log hclog.Logger, opts *GroupOpts) error {
resp, err := opts.WS.WaypointServiceGetNamespace(&waypoint_service.WaypointServiceGetNamespaceParams{
LocationOrganizationID: opts.Profile.OrganizationID,
LocationProjectID: opts.Profile.ProjectID,
Context: opts.Ctx,
}, nil)
if err != nil {
return errors.Wrapf(err, "Unable to access HCP project")
}

ns := resp.Payload.Namespace

ctx := opts.Ctx

_, err = opts.WS.WaypointServiceDeleteAgentGroup(&waypoint_service.WaypointServiceDeleteAgentGroupParams{
Name: opts.Name,
NamespaceID: ns.ID,
Context: ctx,
}, nil)

if err != nil {
fmt.Fprintf(opts.IO.Err(), "Error listing groups: %s", err)
return err
}

fmt.Fprintf(opts.IO.Out(), "Group deleted")
return nil
}

func NewCmdGroupList(ctx *cmd.Context, opts *GroupOpts) *cmd.Command {
cmd := &cmd.Command{
Name: "list",
ShortHelp: "List HCP Waypoint Agent groups.",
LongHelp: heredoc.New(ctx.IO).Must(`
The {{ Bold "waypoint agent group list" }} command lists groups registered.
`),
RunF: func(c *cmd.Command, args []string) error {
return agentGroupList(c.Logger(), opts)
},
}

return cmd
}

func agentGroupList(log hclog.Logger, opts *GroupOpts) error {
resp, err := opts.WS.WaypointServiceGetNamespace(&waypoint_service.WaypointServiceGetNamespaceParams{
LocationOrganizationID: opts.Profile.OrganizationID,
LocationProjectID: opts.Profile.ProjectID,
Context: opts.Ctx,
}, nil)
if err != nil {
return errors.Wrapf(err, "Unable to access HCP project")
}

ns := resp.Payload.Namespace

ctx := opts.Ctx

list, err := opts.WS.WaypointServiceListAgentGroups(&waypoint_service.WaypointServiceListAgentGroupsParams{
NamespaceID: ns.ID,
Context: ctx,
}, nil)

if err != nil {
fmt.Fprintf(opts.IO.Err(), "Error listing groups: %s", err)
return err
}

return opts.Output.Display(groupsDisplayer(list.Payload.Groups))
}

type groupsDisplayer []*models.HashicorpCloudWaypointAgentGroup

func (d groupsDisplayer) DefaultFormat() format.Format {
return format.Table
}

func (d groupsDisplayer) Payload() any {
return d
}

func (d groupsDisplayer) FieldTemplates() []format.Field {
return []format.Field{
{
Name: "Name",
ValueFormat: "{{ .Name }}",
},
{
Name: "Description",
ValueFormat: "{{ .Description }}",
},
}
}
Loading

0 comments on commit b87fb8c

Please sign in to comment.