-
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.
Merge pull request #19 from hashicorp/add_tfc_create_cmd
Commit new TFCConfig Create command.
- Loading branch information
Showing
4 changed files
with
183 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,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
122
internal/commands/waypoint/tfcconfig/tfc_config_create.go
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,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 | ||
|
||
} |
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,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 | ||
} |