-
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.
Do initial work on adding the waypoint agent
- Loading branch information
Showing
16 changed files
with
1,736 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
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,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 | ||
} |
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,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 }}", | ||
}, | ||
} | ||
} |
Oops, something went wrong.