terraform

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package Terraform defines an SDK for running Terraform commands using the Terraform CLI binary.

Index

Constants

This section is empty.

Variables

View Source
var InitRequiredCommands = map[string]struct{}{
	"validate":     {},
	"plan":         {},
	"apply":        {},
	"destroy":      {},
	"console":      {},
	"graph":        {},
	"import":       {},
	"output":       {},
	"providers":    {},
	"refresh":      {},
	"show":         {},
	"state":        {},
	"taint":        {},
	"untaint":      {},
	"workspace":    {},
	"force-unlock": {},
}

InitRequiredCommands are the Terraform commands that require terraform init to be run first.

View Source
var ModuleSchema = &hcl.BodySchema{
	Attributes: []hcl.AttributeSchema{
		{
			Name: "source",
		},
		{
			Name: "version",
		},
		{
			Name: "providers",
		},
	},
}

ModuleSchema is the schema for the module block.

View Source
var ResourceSchema = &hcl.BodySchema{
	Attributes: []hcl.AttributeSchema{
		{
			Name: "provider",
		},
	},
}

ResourceSchema is the schema for the resource block.

View Source
var RootSchema = &hcl.BodySchema{
	Blocks: []hcl.BlockHeaderSchema{
		{
			Type:       "terraform",
			LabelNames: nil,
		},
		{
			Type: "locals",
		},
		{
			Type:       "variable",
			LabelNames: []string{"name"},
		},
		{
			Type:       "output",
			LabelNames: []string{"name"},
		},
		{
			Type:       "provider",
			LabelNames: []string{"name"},
		},
		{
			Type:       "resource",
			LabelNames: []string{"type", "name"},
		},
		{
			Type:       "data",
			LabelNames: []string{"type", "name"},
		},
		{
			Type:       "module",
			LabelNames: []string{"name"},
		},
	},
}

RootSchema is the Terraform root schema.

View Source
var TerraformSchema = &hcl.BodySchema{
	Blocks: []hcl.BlockHeaderSchema{
		{
			Type:       "backend",
			LabelNames: []string{"type"},
		},
		{
			Type: "required_providers",
		},
	},
}

TerraformSchema is the schema for the terraform block.

Functions

func FormatOutputForGitHubDiff

func FormatOutputForGitHubDiff(content string) string

FormatOutputForGitHubDiff formats the Terraform diff output for use with GitHub diff markdown formatting.

Types

type ApplyOptions

type ApplyOptions struct {
	File            *string
	AutoApprove     *bool
	CompactWarnings *bool
	Lock            *bool
	LockTimeout     *string
	Input           *bool
	NoColor         *bool
}

ApplyOptions are the set of options for running a terraform apply.

type FormatOptions

type FormatOptions struct {
	Check     *bool
	Diff      *bool
	List      *bool
	NoColor   *bool
	Recursive *bool
	Write     *bool
}

FormatOptions are the set of options for running terraform format.

type InitOptions

type InitOptions struct {
	Backend     *bool
	Input       *bool
	NoColor     *bool
	Lock        *bool
	LockTimeout *string
	Lockfile    *string
}

InitOptions are the set of options for running a terraform init.

type MockTerraformClient

type MockTerraformClient struct {
	InitResponse     *MockTerraformResponse
	ValidateResponse *MockTerraformResponse
	PlanResponse     *MockTerraformResponse
	ApplyResponse    *MockTerraformResponse
	ShowResponse     *MockTerraformResponse
	FormatResponse   *MockTerraformResponse
	RunResponse      *MockTerraformResponse
}

MockTerraformClient creates a mock TerraformClient for use with testing.

func (*MockTerraformClient) Apply

func (m *MockTerraformClient) Apply(ctx context.Context, stdout, stderr io.Writer, opts *ApplyOptions) (int, error)

func (*MockTerraformClient) Format

func (m *MockTerraformClient) Format(ctx context.Context, stdout, stderr io.Writer, opts *FormatOptions) (int, error)

func (*MockTerraformClient) Init

func (m *MockTerraformClient) Init(ctx context.Context, stdout, stderr io.Writer, opts *InitOptions) (int, error)

func (*MockTerraformClient) Plan

func (m *MockTerraformClient) Plan(ctx context.Context, stdout, stderr io.Writer, opts *PlanOptions) (int, error)

func (*MockTerraformClient) Run

func (m *MockTerraformClient) Run(ctx context.Context, stdout, stderr io.Writer, subcommand string, args ...string) (int, error)

func (*MockTerraformClient) Show

func (m *MockTerraformClient) Show(ctx context.Context, stdout, stderr io.Writer, opts *ShowOptions) (int, error)

func (*MockTerraformClient) Validate

func (m *MockTerraformClient) Validate(ctx context.Context, stdout, stderr io.Writer, opts *ValidateOptions) (int, error)

type MockTerraformResponse

type MockTerraformResponse struct {
	Stdout   string
	Stderr   string
	ExitCode int
	Err      error
}

type ModuleUsageGraph

type ModuleUsageGraph struct {
	// EntrypointToModules is the complete list of all modules used by a terraform entrypoint at any depth.
	EntrypointToModules map[string]map[string]struct{}
	// ModulesToEntrypoints is the complete list of all terraform entrypoints that use a particular module at any depth.
	ModulesToEntrypoints map[string]map[string]struct{}
}

ModuleUsageGraph describes which entrypoints use which modules and which modules are used by each entrypoint.

func ModuleUsage

func ModuleUsage(ctx context.Context, rootDir string, maxDepth *int, skipUnresolvableModules bool) (*ModuleUsageGraph, error)

ModuleUsage locates all the usages of modules in all terraform entrypoints and vice versa.

type Modules

type Modules struct {
	// ModulePaths is the list of all modules used in the particular terraform or module entrypoint.
	ModulePaths map[string]struct{}
}

Modules represents the details of the modules used by a given module or terraform entrypoint.

func ExtractModules

func ExtractModules(path string) (*Modules, hcl.Diagnostics, error)

ExtractModules extracts the details about the modules used in a particular file.

type PlanOptions

type PlanOptions struct {
	CompactWarnings  *bool
	DetailedExitcode *bool
	NoColor          *bool
	Input            *bool
	Lock             *bool
	LockTimeout      *string
	Out              *string
}

PlanOptions are the set of options for running a terraform plan.

type ShowOptions

type ShowOptions struct {
	File    *string
	NoColor *bool
	JSON    *bool
}

ShowOptions are the set of options for running a terraform show.

type Terraform

type Terraform interface {
	// Init runs the terraform init command.
	Init(context.Context, io.Writer, io.Writer, *InitOptions) (int, error)

	// Validate runs the terraform validate command.
	Validate(context.Context, io.Writer, io.Writer, *ValidateOptions) (int, error)

	// Plan runs the terraform plan command.
	Plan(context.Context, io.Writer, io.Writer, *PlanOptions) (int, error)

	// Apply runs the terraform apply command.
	Apply(context.Context, io.Writer, io.Writer, *ApplyOptions) (int, error)

	// Show runs the terraform show command.
	Show(context.Context, io.Writer, io.Writer, *ShowOptions) (int, error)

	// Format runs the terraform fmt command.
	Format(context.Context, io.Writer, io.Writer, *FormatOptions) (int, error)

	// Run runs a terraform command.
	Run(context.Context, io.Writer, io.Writer, string, ...string) (int, error)
}

Terraform is the interface for working with the Terraform CLI.

type TerraformBackendConfig

type TerraformBackendConfig struct {
	// GCSBucket is the GCS Bucket used in a terraform backend config block.
	GCSBucket *string `hcl:"bucket,attr"`
	// Prefix is the GCS Bucket prefix used in a terraform backend config block.
	Prefix *string `hcl:"prefix,attr"`
}

TerraformBackendConfig represents the terraform backend config block.

func ExtractBackendConfig

func ExtractBackendConfig(path string) (*TerraformBackendConfig, hcl.Diagnostics, error)

ExtractBackendConfig extracts the backend configuration from the backend block.

type TerraformClient

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

TerraformClient implements the Terraform interface.

func NewTerraformClient

func NewTerraformClient(workingDir string) *TerraformClient

NewTerraformClient creates a new Terraform client.

func (*TerraformClient) Apply

func (t *TerraformClient) Apply(ctx context.Context, stdout, stderr io.Writer, opts *ApplyOptions) (int, error)

Apply runs the Terraform apply command.

func (*TerraformClient) Format

func (t *TerraformClient) Format(ctx context.Context, stdout, stderr io.Writer, opts *FormatOptions) (int, error)

Format runs the Terraform format command.

func (*TerraformClient) Init

func (t *TerraformClient) Init(ctx context.Context, stdout, stderr io.Writer, opts *InitOptions) (int, error)

Init runs the terraform init command.

func (*TerraformClient) Plan

func (t *TerraformClient) Plan(ctx context.Context, stdout, stderr io.Writer, opts *PlanOptions) (int, error)

Plan runs the Terraform plan command.

func (*TerraformClient) Run

func (t *TerraformClient) Run(ctx context.Context, stdout, stderr io.Writer, subcommand string, args ...string) (int, error)

Run runs a Terraform command.

func (*TerraformClient) Show

func (t *TerraformClient) Show(ctx context.Context, stdout, stderr io.Writer, opts *ShowOptions) (int, error)

Show runs the Terraform show command.

func (*TerraformClient) Validate

func (t *TerraformClient) Validate(ctx context.Context, stdout, stderr io.Writer, opts *ValidateOptions) (int, error)

Validate runs the terraform validate command.

type TerraformEntrypoint

type TerraformEntrypoint struct {
	// Path is the filesystem path to the entrypoint.
	Path string
	// BackendFile is the filename of the terraform file that contains the backend config block.
	BackendFile string
}

TerraformEntrypoint describes a terraform entrypoint.

func GetEntrypointDirectories

func GetEntrypointDirectories(rootDir string, maxDepth *int) ([]*TerraformEntrypoint, error)

GetEntrypointDirectories gets all the directories that have Terraform config files containing a backend block to be used as an entrypoint module.

type TerraformResponse

type TerraformResponse struct {
	Stdout   io.Reader
	Stderr   io.Reader
	ExitCode int
}

TerraformResponse is the response from running a terraform command.

type ValidateOptions

type ValidateOptions struct {
	NoColor *bool
	JSON    *bool
}

ValidateOptions are the set of options for running a terraform validate.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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