Skip to content

Commit

Permalink
Merge pull request #19 from hashicorp/add_tfc_create_cmd
Browse files Browse the repository at this point in the history
Commit new TFCConfig Create command.
  • Loading branch information
teresamychu authored Feb 22, 2024
2 parents 5fb4edb + 8d0e5de commit ae89ffc
Show file tree
Hide file tree
Showing 4 changed files with 183 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.NewCmdWaypoint(ctx))

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

import (
"context"

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

func NewCmdTFCConfig(ctx *cmd.Context) *cmd.Command {
cmd := &cmd.Command{
Name: "tfc-config",
ShortHelp: "Manage Terraform Cloud Configurations.",
LongHelp: heredoc.New(ctx.IO).Must(`
Manage the set of TFC Configs. New TFC Configs can be created using {{ Bold "hcp waypoint tfc-config set" }}
and existing profiles can be viewed using {{ Bold "hcp waypoint tfc-config get" }}.
`),
}

cmd.AddChild(NewCmdCreate(ctx, nil))
return cmd
}

func GetNamespace(ctx context.Context, client waypoint_service.ClientService, orgID, projectID string) (string, error) {

resp, err := client.WaypointServiceGetNamespace(
&waypoint_service.WaypointServiceGetNamespaceParams{
LocationOrganizationID: orgID,
LocationProjectID: projectID,
Context: ctx,
}, nil,
)
if err != nil {
return "", err
}

return resp.Payload.Namespace.ID, nil

}
122 changes: 122 additions & 0 deletions internal/commands/waypoint/tfcconfig/tfc_config_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package tfcconfig

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

func NewCmdCreate(ctx *cmd.Context, runF func(opts *TFCConfigOpts) error) *cmd.Command {
opts := &TFCConfigOpts{
Ctx: ctx.ShutdownCtx,
Output: ctx.Output,
Profile: ctx.Profile,
IO: ctx.IO,
WaypointClient: waypoint_service.New(ctx.HCP, nil),
}

cmd := &cmd.Command{
Name: "create",
ShortHelp: "Set TFC Configuration.",
LongHelp: heredoc.New(ctx.IO).Mustf(`{{ PreserveNewLines }}The {{Bold "hcp waypoint tfc-config create"}} command
sets the TFC Organization Name and TFC Team token that will be used in Waypoint.
There can only be one TFC Config set for each HCP Project.
TFC Configs can be reviewed using the {{Bold "hcp waypoint tfc-config read" }} command
and removed with the {{Bold "hcp waypoint tfc-config delete"}} command.{{ PreserveNewLines }}`),
Examples: []cmd.Example{
{
Preamble: `Create a new TFC Config in HCP Waypoint:`,
Command: heredoc.New(ctx.IO, heredoc.WithPreserveNewlines()).Must(`
$ hcp waypoint tfc-config create example-org <token>`),
},
},
Args: cmd.PositionalArguments{
Args: []cmd.PositionalArgument{
{
Name: "TFC_ORG",
Documentation: heredoc.New(ctx.IO).Must(`Name of the Terraform Cloud Organization.`),
},
{
Name: "TOKEN",
Documentation: heredoc.New(ctx.IO).Must(`{{ PreserveNewLines }}
Terraform Cloud Team token for the TFC organization.
Team token must be set in order to perform HCP Waypoint commands.
You can learn more about API tokens at: https://developer.hashicorp.com/terraform/cloud-docs/users-teams-organizations/api-tokens
HCP Waypoint requires Team level access tokens in order to run correctly.
Please ensure that your TFCConfig token has the correct permissions
{{ PreserveNewLines }}.
`),
},
},
},
RunF: func(c *cmd.Command, args []string) error {
opts.TfcOrg = args[0]
opts.Token = args[1]
if runF != nil {
return runF(opts)
}
return createRun(opts)
},
PersistentPreRun: func(c *cmd.Command, args []string) error {
return cmd.RequireOrgAndProject(ctx)
},
}
return cmd
}

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

TfcOrg string
Token string
WaypointClient waypoint_service.ClientService
}

func createRun(opts *TFCConfigOpts) error {
nsID, err := GetNamespace(opts.Ctx, opts.WaypointClient, opts.Profile.OrganizationID, opts.Profile.ProjectID)
if err != nil {
return fmt.Errorf("error getting namespace: %w", err)
}

ns := &models.HashicorpCloudWaypointRefNamespace{ID: nsID}
request := &models.HashicorpCloudWaypointCreateTFCConfigRequest{
Namespace: ns,
TfcConfig: &models.HashicorpCloudWaypointTFCConfig{
OrganizationName: opts.TfcOrg,
Token: opts.Token,
},
}
resp, err := opts.WaypointClient.WaypointServiceCreateTFCConfig(
&waypoint_service.WaypointServiceCreateTFCConfigParams{
Body: request,
NamespaceID: nsID,
Context: opts.Ctx,
}, nil,
)
if err != nil {
return fmt.Errorf("error creating TFC config: %w", err)

}

fmt.Fprintf(opts.IO.Err(), "%s TFC Config %q created!\n", opts.IO.ColorScheme().SuccessIcon(), resp.Payload.TfcConfig.OrganizationName)

return nil

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

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

func NewCmdWaypoint(ctx *cmd.Context) *cmd.Command {
cmd := &cmd.Command{
Name: "waypoint",
ShortHelp: "Manage Waypoint.",
LongHelp: heredoc.New(ctx.IO).Must(`The {{ Bold "hcp waypoint" }} command group allows users to manage HCP Waypoint resources through the CLI.
These commands allow the user to interact with their HCP Waypoint instance to manage their application deployment process.`),
}

cmd.AddChild(tfcconfig.NewCmdTFCConfig(ctx))
return cmd
}

0 comments on commit ae89ffc

Please sign in to comment.