tferun

package module
v0.3.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 1, 2021 License: MIT Imports: 10 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-tfe-run PkgGoDev

CI Integration Go Report Card codecov

A Go library to create and follow up on a run on Terraform Cloud. This library uses the Terraform Cloud API and does not rely on a local Terraform installation.

When would this be useful?

  • You want to configure parameters that are not available using the CLI, for example the name of the run.
  • You want to schedule a run but not wait for its completion.
  • You are in an environment without Terraform CLI installed.

If you wish to integrate this into your GitHub Actions workflows, checkout the tfe-run action which wraps go-tfe-run into a custom action.

Visit the documentation on pkg.go.dev.

License

This software is distributed under the terms of the MIT license, see LICENSE for details.

Documentation

Overview

Package tferun provides a single simplified methods to schedule runs and retrieve outputs from a remote workspace on Terraform Enterprise or Cloud.

These methods are intended to be run as part of a CLI and will print information to stdout.

Example
ctx := context.TODO()
token := os.Getenv("TFE_TOKEN")

cfg := ClientConfig{
	Token:        token,
	Organization: "kvrhdn",
	Workspace:    "tfe-run",
}
client, err := NewClient(ctx, cfg)
if err != nil {
	fmt.Printf("could not create client: %v\n", err)
	os.Exit(1)
}

options := RunOptions{
	Message: String("Run created using tfe-run"),
}
output, err := client.Run(ctx, options)
if err != nil {
	fmt.Printf("run failed: %v\n", err)
	os.Exit(1)
}

fmt.Printf("Run created: %v", output.RunURL)
Example (TfVars)
ctx := context.TODO()
token := os.Getenv("TFE_TOKEN")

cfg := ClientConfig{
	Token:        token,
	Organization: "kvrhdn",
	Workspace:    "tfe-run",
}
client, err := NewClient(ctx, cfg)
if err != nil {
	fmt.Printf("could not create client: %v\n", err)
	os.Exit(1)
}

options := RunOptions{
	TfVars: String("my_var = \"foo\""),
}
output, err := client.Run(ctx, options)
if err != nil {
	fmt.Printf("run failed: %v\n", err)
	os.Exit(1)
}

fmt.Printf("Run created: %v", output.RunURL)

Index

Examples

Constants

View Source
const (
	DefaultAddress = "https://app.terraform.io"
)

Variables

View Source
var (
	// ErrTimeout is returned when an operation timed out.
	ErrTimeout = errors.New("timed out while polling")
)

Functions

func String

func String(s string) *string

String returns a pointer to the given string.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is used to interact with the Run API of a single workspace on Terraform Cloud.

func NewClient

func NewClient(ctx context.Context, cfg ClientConfig) (*Client, error)

NewClient creates a Client from ClientConfig.

func (*Client) GetTerraformOutputs

func (c *Client) GetTerraformOutputs(ctx context.Context) (map[string]string, error)

GetTerraformOutputs retrieves the outputs from the current Terraform state.

Example
ctx := context.TODO()
token := os.Getenv("TFE_TOKEN")

cfg := ClientConfig{
	Token:        token,
	Organization: "kvrhdn",
	Workspace:    "tfe-run",
}
client, err := NewClient(ctx, cfg)
if err != nil {
	fmt.Printf("could not create client: %v\n", err)
	os.Exit(1)
}

outputs, err := client.GetTerraformOutputs(ctx)
if err != nil {
	fmt.Printf("could not create client: %v\n", err)
	os.Exit(1)
}

fmt.Printf("Current outputs: %v", outputs)

func (*Client) Run

func (c *Client) Run(ctx context.Context, options RunOptions) (output RunOutput, err error)

Run creates a new run on Terraform Cloud.

If RunOptions.WaitForCompletion is set this method will block until the run is finished, except if the run is non-speculative and the workspace has disabled auto-apply (to avoid blocking indefinitely). If the run does not complete within one hour, ErrTimeout is returned. This will not cancel the remote operation.

type ClientConfig

type ClientConfig struct {
	// Token used to communicate with the Terraform Cloud API. Must be a user
	// or team API token.
	Token string
	// The organization on Terraform Cloud.
	Organization string
	// The workspace on Terraform Cloud.
	Workspace string
	// The address of the Terraform Enterprise API
	Address string
}

ClientConfig groups properties to configure a Client.

type RunOptions

type RunOptions struct {
	// Message to use as name of the run. This field is optional.
	Message *string
	// The directory that is uploaded to Terraform Cloud, respects
	// .terraformignore. Defaults to the current directory.
	Directory *string
	// The type of run to schedule.
	Type RunType
	// A list of resource addresses that are passed to the -target flag. For
	// more details, check https://www.terraform.io/docs/commands/plan.html#resource-targeting
	TargetAddrs []string
	// Whether we should wait for the non-speculative run to be applied. This
	// will block until the run is finished.
	WaitForCompletion bool
	// Contents of a auto.tfvars file that will be uploaded to Terraform Cloud.
	// This can be used to set temporary Terraform variables. These variables
	// will not be preserved across runs.
	TfVars *string
}

RunOptions groups all options available when creating a new run.

type RunOutput

type RunOutput struct {
	// URL to the run on Terraform Cloud.
	RunURL string
	// Whether this run has changes. After a speculative plan this would
	// indicate whether an apply would cause changes, after a non-speculative
	// plan this indicates whether the run has caused any changes.
	// This is not populated for non-speculative runs on workspaces that do not
	// have auto-apply configured or when WaitForCompletion is not set.
	HasChanges *bool
}

RunOutput holds the data that is generated by a run.

type RunType

type RunType int

RunType describes the type of run.

const (
	RunTypePlan RunType = iota
	RunTypeApply
	RunTypeDestroy
)

Declaration of run types.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址