tfx

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2019 License: BSD-3-Clause Imports: 27 Imported by: 3

Documentation

Overview

Package tfx extends Terraform operations for new use cases.

Index

Constants

View Source
const DefaultStateFile = "terraform.tfstate"

DefaultStateFile is the name of the default Terraform state file. It is redefined here to avoid importing the entire command package.

Variables

View Source
var Deps = make(DepMap)

Deps is the global dependency inference map.

Functions

func AddState

func AddState(a, b *tf.State) *tf.State

AddState performs 'a += b' operation on resources in a. Duplicate resources are ignored.

func ClearDeps

func ClearDeps(s *tf.State)

ClearDeps clears all resource dependencies.

func Config

func Config(s map[string]*schema.Schema, raw map[string]interface{}) (*schema.ResourceData, error)

Config creates schema.ResourceData from a raw config.

func DeepCopy

func DeepCopy(v interface{}) interface{}

DeepCopy returns a deep copy of v.

func DisableLogging

func DisableLogging()

DisableLogging disables all Terraform logging, while allowing other messages through.

func ExplainDiff

func ExplainDiff(d *tf.Diff) string

ExplainDiff returns a description of inconsistencies between actual state and desired config.

func InitSchemaProvider

func InitSchemaProvider(rp tf.ResourceProvider) *schema.Provider

InitSchemaProvider should be called from factory functions to initialize new schema.Provider instances. It disables DefaultFuncs to ensure deterministic behavior (these are normally used to get environment variables), and sets zero-value defaults to prevent any user prompts.

func LoadModule

func LoadModule(path string) (*module.Tree, error)

LoadModule reads module config from a file or directory ("" or "-" mean stdin).

func MakeFactory

func MakeFactory(f func() tf.ResourceProvider) tf.ResourceProviderFactory

MakeFactory adds a nil error return to a standard provider constructor to match factory function signature. This should be used instead of terraform.ResourceProviderFactoryFixed.

func NewState

func NewState() *tf.State

NewState returns an initialized empty state.

func ReadDiff

func ReadDiff(r io.Reader) (*tf.Diff, error)

ReadDiff reads Terraform diff from r. It supports both JSON-encoded diffs and plan files.

func ReadDiffFile

func ReadDiffFile(file string) (*tf.Diff, error)

ReadDiffFile reads Terraform diff from the specified file. It supports both JSON-encoded diffs and plan files.

func ReadPlanFile

func ReadPlanFile(file string) (*tf.Plan, error)

ReadPlanFile reads Terraform plan from the specified file.

func ReadStateFile

func ReadStateFile(file string) (*tf.State, error)

ReadStateFile reads Terraform state from the specified file.

func SetLogFilter

func SetLogFilter(w io.Writer, level string, requireLevel bool) error

SetLogFilter configures Terraform log filter. Since all Terraform components use the default logger (ugh... why?!?), this may affect other code as well. If requireLevel is true, any log message that does not have a level prefix is filtered out.

func SubState

func SubState(a, b *tf.State) *tf.State

SubState performs 'a -= b' operation on resources in a.

func WriteDiff

func WriteDiff(w io.Writer, d *tf.Diff) error

WriteDiff writes diff d to w in JSON format.

func WriteDiffFile

func WriteDiffFile(file string, d *tf.Diff) error

WriteDiffFile writes diff d to file in JSON format.

func WritePlanFile

func WritePlanFile(file string, p *tf.Plan) error

WritePlanFile writes plan p to file in binary format.

func WriteStateFile

func WriteStateFile(file string, s *tf.State) error

WriteStateFile writes Terraform state to the specified file.

Types

type AttrGen

type AttrGen map[string]interface{}

AttrGen is an attribute value generator used to create resources. Valid value types are: string, []string, func(i int) string, and func(i int) *string. Functions must return values for i in the range [0,n). Use "#" key to specify n when there are no []string attributes.

type Ctx

type Ctx struct {
	Meta        tf.ContextMeta
	Parallelism int
	Providers   ProviderMap
}

Ctx implements standard and non-standard Terraform operations using a provider registry.

func Context

func Context() *Ctx

Context returns a new context configured to use default providers.

func (*Ctx) Apply

func (c *Ctx) Apply(t *module.Tree, s *tf.State) (*tf.State, error)

Apply does a plan/apply operation to ensure that state s matches config t and returns the new state.

func (*Ctx) Conform

func (c *Ctx) Conform(t *module.Tree, s *tf.State, strict bool) (StateTransform, error)

Conform returns a transformation that associates root module resource states in s with their configurations in t. If strict is true, the transform will remove any non-conforming resources.

func (*Ctx) Diff

func (c *Ctx) Diff(t *module.Tree, s *tf.State) (*tf.Diff, error)

Diff return the changes required to apply configuration t to state s. If s is nil, an empty state is assumed.

func (*Ctx) Mutate

func (c *Ctx) Mutate(s *tf.State, cfg *MutateCfg) (*tf.Diff, error)

func (*Ctx) Passthrough

func (c *Ctx) Passthrough(t *module.Tree, s *tf.State) (*tf.State, error)

Passthrough does a plan/apply operation with no-op provider CRUD methods and returns the new state. The providers are prevented from making any API calls, and the resulting (invalid) state becomes a copy of the input config.

func (*Ctx) Patch

func (c *Ctx) Patch(s *tf.State, d *tf.Diff) (*tf.State, error)

Patch applies diff d to state s and returns the new state. Unlike the standard apply operation, this one does not require a valid config. It works by building and walking a modified apply graph that omits all config references, which means that node evaluation may have slightly different behavior. For example, resource lifecycle information is only available in the config, so create-before-destroy behavior cannot be implemented.

func (*Ctx) Plan

func (c *Ctx) Plan(t *module.Tree, s *tf.State) (*tf.Plan, error)

Plan returns a plan to apply configuration t to state s. If s is nil, an empty state is assumed.

func (*Ctx) Refresh

func (c *Ctx) Refresh(s *tf.State) (*tf.State, error)

Refresh updates the state of all resources in s and returns the new state.

func (*Ctx) SetDefaults

func (c *Ctx) SetDefaults(s *tf.State)

SetDefaults sets default values for any missing resource attributes in s. This is only needed after refreshing a scanned state.

type DepMap

type DepMap map[string][]DepSpec

DepMap is a resource dependency inference map. Keys are resource types. Values are dependency specifications for attributes of that type. These maps are usually generated for each provider via depgen.

func (DepMap) Add

func (dm DepMap) Add(m DepMap)

Add copies all entries from m to dm.

func (DepMap) Infer

func (dm DepMap) Infer(s *tf.State)

Infer updates dependencies for all resources in s. This is most commonly used for states created via a scan.

type DepSpec

type DepSpec struct{ Attr, SrcType, SrcAttr string }

DepSpec specifies that the value of attribute Attr is obtained in HCL by interpolating "${SrcType.<name>.SrcAttr}". Resource dependencies are inferred by comparing the value(s) of the destination attribute with those of all available sources.

type MutateCfg

type MutateCfg struct {
	Seed  int64
	Limit int
	Funcs []MutateFunc
}

MutateCfg determines the behavior of the Mutate operation.

type MutateFunc

type MutateFunc func(*MutateState)

MutateFunc is a function that can modify resources.

type MutateState

type MutateState struct {
	*schema.ResourceData

	Rand   *rand.Rand
	Diff   *tf.ModuleDiff
	Module *tf.ModuleState
	Type   string
	Key    string
	Schema map[string]*schema.Schema
}

MutateState contains the state of the current resource as well as the rest

func (*MutateState) RandID

func (ms *MutateState) RandID(n int) string

RandID returns a random alphanumeric (base62) string of length n with the first character always an upper-case letter.

type ProviderMap

type ProviderMap map[string]*provider

ProviderMap is an in-memory provider registry. It returns provider resolvers for Terraform context operations and provides access to provider schemas.

var Providers ProviderMap

Providers is the default in-memory provider registry.

func (*ProviderMap) Add

func (pm *ProviderMap) Add(name, version string, f tf.ResourceProviderFactory)

Add adds a new provider to the registry. Version is optional. The factory function must return a new provider instance for each call (i.e. do not use terraform.ResourceProviderFactoryFixed wrapper).

func (ProviderMap) DefaultResolver

func (pm ProviderMap) DefaultResolver() tf.ResourceProviderResolver

DefaultResolver returns a resolver for unmodified providers.

func (ProviderMap) ImportResources

func (pm ProviderMap) ImportResources(typ string, attrs AttrGen) ([]Resource, error)

ImportResources calls NewResource for each "id" attribute (or for "#" invocations of its generator function), applies the resource importer, and populates any remaining attribute values.

func (ProviderMap) MakeResources

func (pm ProviderMap) MakeResources(typ string, attrs AttrGen) ([]Resource, error)

MakeResources calls NewResource for each "id" attribute (or for "#" invocations of its generator function) and populates any remaining attribute values.

func (ProviderMap) NewResource

func (pm ProviderMap) NewResource(typ, id string, useImport bool) (Resource, error)

NewResource returns a skeleton resource state for the specified resource type and ID. If useImport is true, the resource importer is applied to the new resource. Importers that return multiple new states or make API calls are not supported.

func (ProviderMap) PassthroughResolver

func (pm ProviderMap) PassthroughResolver() tf.ResourceProviderResolver

PassthroughResolver returns a SchemaResolver with all schema validations disabled.

func (ProviderMap) ResourceSchema

func (pm ProviderMap) ResourceSchema(typ string) (*schema.Provider, *schema.Resource)

ResourceSchema returns the provider and resource schema for the specified resource type. It returns (nil, nil) if the type is unknown or not implemented via schema.Provider.

func (ProviderMap) Schema

func (pm ProviderMap) Schema(provider string) *schema.Provider

Schema returns the schema for the specified provider. It returns nil if the provider is not registered or not implemented via schema.Provider. The returned value is cached and must only be used for local schema operations.

func (ProviderMap) SchemaResolver

func (pm ProviderMap) SchemaResolver() tf.ResourceProviderResolver

SchemaResolver returns a resolver for schema-only providers. Provider configuration and CRUD operations are disabled, preventing the provider from requiring a valid config or making any API calls.

type Resource

type Resource struct {
	Key string
	*tf.ResourceState
	// contains filtered or unexported fields
}

Resource associates a state key with tf.ResourceState.

func (*Resource) Data

func (r *Resource) Data() *schema.ResourceData

Data returns resource data for schema-aware operations.

func (*Resource) Schema

func (r *Resource) Schema() *schema.Resource

Schema returns resource schema.

type StateTransform

type StateTransform map[string]string

StateTransform defines state resource address transformations. It can change resource keys, move resources between modules, and remove resources. Dependencies are updated as needed as long as they stay within the same module. Keys and values are Terraform resource addresses. Resource types are not validated. An empty value removes the resource.

func NormStateKeys

func NormStateKeys(s *tf.State) (StateTransform, error)

NormStateKeys returns a transformation that normalizes resource state keys using provider names and resource IDs.

func (StateTransform) Apply

func (st StateTransform) Apply(s *tf.State) error

Apply updates resource state keys according to the transformation map. The state is not modified if an error is returned. Missing resources are silently ignored. Address collisions are resolved in favor of the transformation, so the map {A: B} will replace an existing resource B with A. Without an explicit {B: ""} entry, resources that depended on B will depend on A after such transformation.

func (StateTransform) ApplyToDiff

func (st StateTransform) ApplyToDiff(d *tf.Diff) error

ApplyToDiff updates resource diff keys according to the transformation map. The diff may be modified after an error.

func (StateTransform) Inverse

func (st StateTransform) Inverse() StateTransform

Inverse returns an inverse state transformation. It returns nil if st is destructive. Implicit resource replacement is not detected.

Directories

Path Synopsis
Package depgen extracts resource dependency information from Terraform provider documentation and test files.
Package depgen extracts resource dependency information from Terraform provider documentation and test files.

Jump to

Keyboard shortcuts

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