tfmigrate

package
v0.3.23 Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: MIT Imports: 9 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MigrationConfig added in v0.2.0

type MigrationConfig struct {
	// Type is a type for migration.
	// Valid values are `state` and `multi_state`.
	Type string
	// Name is an arbitrary name for migration.
	Name string
	// Migrator is an interface of factory method for Migrator.
	Migrator MigratorConfig
}

MigrationConfig is a config for a migration.

type Migrator

type Migrator interface {
	// Plan computes a new state by applying state migration operations to a temporary state.
	// It will fail if terraform plan detects any diffs with the new state.
	Plan(ctx context.Context) error

	// Apply computes a new state and pushes it to remote state.
	// It will fail if terraform plan detects any diffs with the new state.
	// This is intended for solely state refactoring.
	// Any state migration operations should not break any real resources.
	Apply(ctx context.Context) error
}

Migrator abstracts migration operations.

type MigratorConfig added in v0.2.0

type MigratorConfig interface {
	// NewMigrator returns a new instance of Migrator.
	NewMigrator(o *MigratorOption) (Migrator, error)
}

MigratorConfig is an interface of factory method for Migrator.

type MigratorOption

type MigratorOption struct {
	// ExecPath is a string how terraform command is executed. Default to terraform.
	// It's intended to inject a wrapper command such as direnv.
	// e.g.) direnv exec . terraform
	// To use OpenTofu, set this to `tofu`.
	ExecPath string

	// PlanOut is a path to plan file to be saved.
	PlanOut string

	// IsBackendTerraformCloud is a boolean indicating if the remote backend is Terraform Cloud
	IsBackendTerraformCloud bool

	// BackendConfig is a -backend-config option for remote state
	BackendConfig []string
}

MigratorOption customizes a behavior of Migrator. It is used for shared settings across Migrator instances.

type MockMigrator added in v0.2.0

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

MockMigrator implements the Migrator interface for testing. It does nothing, but can return an error.

func NewMockMigrator added in v0.2.0

func NewMockMigrator(planError bool, applyError bool) *MockMigrator

NewMockMigrator returns a new MockMigrator instance.

func (*MockMigrator) Apply added in v0.2.0

func (m *MockMigrator) Apply(ctx context.Context) error

Apply computes a new state and pushes it to remote state. It does nothing, but can return an error.

func (*MockMigrator) Plan added in v0.2.0

func (m *MockMigrator) Plan(ctx context.Context) error

Plan computes a new state by applying state migration operations to a temporary state. It does nothing, but can return an error.

type MockMigratorConfig added in v0.2.0

type MockMigratorConfig struct {
	// PlanError is a flag to return an error on Plan().
	PlanError bool `hcl:"plan_error"`
	// ApplyError is a flag to return an error on Apply().
	ApplyError bool `hcl:"apply_error"`
}

MockMigratorConfig is a config for MockMigrator.

func (*MockMigratorConfig) NewMigrator added in v0.2.0

func (c *MockMigratorConfig) NewMigrator(_ *MigratorOption) (Migrator, error)

NewMigrator returns a new instance of MockMigrator.

type MultiStateAction

type MultiStateAction interface {
	// MultiStateUpdate updates given two states and returns new two states.
	MultiStateUpdate(ctx context.Context, fromTf tfexec.TerraformCLI, toTf tfexec.TerraformCLI, fromState *tfexec.State, toState *tfexec.State) (*tfexec.State, *tfexec.State, error)
}

MultiStateAction abstracts multi state migration operations. It's used for moving resources from one state to another.

func NewMultiStateActionFromString

func NewMultiStateActionFromString(cmdStr string) (MultiStateAction, error)

NewMultiStateActionFromString is a factory method which returns a new MultiStateAction from a given string. cmdStr is a plain text for state operation. This method is useful to build an action from terraform state command. Valid formats are the following. "mv <source> <destination>" "xmv <source> <destination>"

type MultiStateMigrator

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

MultiStateMigrator implements the Migrator interface.

func NewMultiStateMigrator

func NewMultiStateMigrator(fromDir string, toDir string, fromWorkspace string, toWorkspace string,
	actions []MultiStateAction, o *MigratorOption, force bool, fromSkipPlan bool, toSkipPlan bool) *MultiStateMigrator

NewMultiStateMigrator returns a new MultiStateMigrator instance.

func (*MultiStateMigrator) Apply

func (m *MultiStateMigrator) Apply(ctx context.Context) error

Apply computes new states and pushes them to remote states. It will fail if terraform plan detects any diffs with at least one new state. We are intended to this is used for state refactoring. Any state migration operations should not break any real resources.

func (*MultiStateMigrator) Plan

func (m *MultiStateMigrator) Plan(ctx context.Context) error

Plan computes new states by applying multi state migration operations to temporary states. It will fail if terraform plan detects any diffs with at least one new state.

type MultiStateMigratorConfig added in v0.2.0

type MultiStateMigratorConfig struct {
	// FromDir is a working directory where states of resources move from.
	FromDir string `hcl:"from_dir"`
	// FromSkipPlan controls whether or not to run and analyze Terraform plan
	// within the from_dir.
	FromSkipPlan bool `hcl:"from_skip_plan,optional"`
	// ToDir is a working directory where states of resources move to.
	ToDir string `hcl:"to_dir"`
	// ToSkipPlan controls whether or not to run and analyze Terraform plan
	// within the to_dir.
	ToSkipPlan bool `hcl:"to_skip_plan,optional"`
	// FromWorkspace is a workspace within FromDir
	FromWorkspace string `hcl:"from_workspace,optional"`
	// ToWorkspace is a workspace within ToDir
	ToWorkspace string `hcl:"to_workspace,optional"`
	// Actions is a list of multi state action.
	// Each action is a plain text for state operation.
	// Valid formats are the following.
	// "mv <source> <destination>"
	Actions []string `hcl:"actions"`
	// Force option controls behaviour in case of unexpected diff in plan.
	// When set forces applying even if plan shows diff.
	Force bool `hcl:"force,optional"`
}

MultiStateMigratorConfig is a config for MultiStateMigrator.

func (*MultiStateMigratorConfig) NewMigrator added in v0.2.0

func (c *MultiStateMigratorConfig) NewMigrator(o *MigratorOption) (Migrator, error)

NewMigrator returns a new instance of MultiStateMigrator.

type MultiStateMvAction

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

MultiStateMvAction implements the MultiStateAction interface. MultiStateMvAction moves a resource from a dir to another. It also can rename an address of resource.

func NewMultiStateMvAction

func NewMultiStateMvAction(source string, destination string) *MultiStateMvAction

NewMultiStateMvAction returns a new MultiStateMvAction instance.

func (*MultiStateMvAction) MultiStateUpdate

func (a *MultiStateMvAction) MultiStateUpdate(ctx context.Context, fromTf tfexec.TerraformCLI, toTf tfexec.TerraformCLI, fromState *tfexec.State, toState *tfexec.State) (*tfexec.State, *tfexec.State, error)

MultiStateUpdate updates given two states and returns new two states. It moves a resource from a dir to another. It also can rename an address of resource.

type MultiStateXmvAction added in v0.3.10

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

MultiStateXmvAction implements the MultiStateAction interface. MultiStateXmvAction is an extended version of MultiStateMvAction. It allows you to move multiple resouces with wildcard matching.

func NewMultiStateXmvAction added in v0.3.10

func NewMultiStateXmvAction(source string, destination string) *MultiStateXmvAction

NewMultiStateXmvAction returns a new MultiStateXmvAction instance.

func (*MultiStateXmvAction) MultiStateUpdate added in v0.3.10

func (a *MultiStateXmvAction) MultiStateUpdate(ctx context.Context, fromTf tfexec.TerraformCLI, toTf tfexec.TerraformCLI, fromState *tfexec.State, toState *tfexec.State) (*tfexec.State, *tfexec.State, error)

MultiStateUpdate updates given two states and returns new two states. It moves a resource from a dir to another. It also can rename an address of resource.

type StateAction

type StateAction interface {
	// StateUpdate updates a given state and returns a new state.
	StateUpdate(ctx context.Context, tf tfexec.TerraformCLI, state *tfexec.State) (*tfexec.State, error)
}

StateAction abstracts state migration operations.

func NewStateActionFromString

func NewStateActionFromString(cmdStr string) (StateAction, error)

NewStateActionFromString is a factory method which returns a new StateAction from a given string. cmdStr is a plain text for state operation. This method is useful to build an action from terraform state command. Valid formats are the following. "mv <source> <destination>" "rm <addresses>... "import <address> <id>" "xmv <source> <destination>"

type StateImportAction

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

StateImportAction implements the StateAction interface. StateImportAction imports an existing resource to state. Note that the Terraform has terraform import command, not terraform state import command. According to the help of import command, this is because a future version Terraform will not only import state, but also generate configuration. We intentionally use term "StateImportAction" to clarify it imports state only.

func NewStateImportAction

func NewStateImportAction(address string, id string) *StateImportAction

NewStateImportAction returns a new StateImportAction instance.

func (*StateImportAction) StateUpdate

func (a *StateImportAction) StateUpdate(ctx context.Context, tf tfexec.TerraformCLI, state *tfexec.State) (*tfexec.State, error)

StateUpdate updates a given state and returns a new state. It imports an existing resource to state.

type StateMigrator

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

StateMigrator implements the Migrator interface.

func NewStateMigrator

func NewStateMigrator(dir string, workspace string, actions []StateAction,
	o *MigratorOption, force bool, skipPlan bool) *StateMigrator

NewStateMigrator returns a new StateMigrator instance.

func (*StateMigrator) Apply

func (m *StateMigrator) Apply(ctx context.Context) error

Apply computes a new state and pushes it to remote state. It will fail if terraform plan detects any diffs with the new state. We are intended to this is used for state refactoring. Any state migration operations should not break any real resources.

func (*StateMigrator) Plan

func (m *StateMigrator) Plan(ctx context.Context) error

Plan computes a new state by applying state migration operations to a temporary state. It will fail if terraform plan detects any diffs with the new state.

type StateMigratorConfig added in v0.2.0

type StateMigratorConfig struct {
	// Dir is a working directory for executing terraform command.
	// Default to `.` (current directory).
	Dir string `hcl:"dir,optional"`
	// Actions is a list of state action.
	// action is a plain text for state operation.
	// Valid formats are the following.
	// "mv <source> <destination>"
	// "rm <addresses>...
	// "import <address> <id>"
	// We could define strict block schema for action, but intentionally use a
	// schema-less string to allow us to easily copy terraform state command to
	// action.
	Actions []string `hcl:"actions"`
	// Force option controls behaviour in case of unexpected diff in plan.
	// When set forces applying even if plan shows diff.
	Force bool `hcl:"force,optional"`
	// SkipPlan controls whether or not to run and analyze Terraform plan.
	SkipPlan bool `hcl:"to_skip_plan,optional"`
	// Workspace is the state workspace which the migration works with.
	Workspace string `hcl:"workspace,optional"`
}

StateMigratorConfig is a config for StateMigrator.

func (*StateMigratorConfig) NewMigrator added in v0.2.0

func (c *StateMigratorConfig) NewMigrator(o *MigratorOption) (Migrator, error)

NewMigrator returns a new instance of StateMigrator.

type StateMvAction

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

StateMvAction implements the StateAction interface. StateMvAction moves a resource from source address to destination address in the same tfstate file.

func NewStateMvAction

func NewStateMvAction(source string, destination string) *StateMvAction

NewStateMvAction returns a new StateMvAction instance.

func (*StateMvAction) StateUpdate

func (a *StateMvAction) StateUpdate(ctx context.Context, tf tfexec.TerraformCLI, state *tfexec.State) (*tfexec.State, error)

StateUpdate updates a given state and returns a new state. It moves a resource from source address to destination address in the same tfstate file.

type StateReplaceProviderAction added in v0.3.15

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

StateReplaceProviderAction implements the StateAction interface. StateReplaceProviderAction replaces a provider from source address to destination address in the same tfstate file.

func NewStateReplaceProviderAction added in v0.3.15

func NewStateReplaceProviderAction(source string, destination string) *StateReplaceProviderAction

NewStateReplaceProviderAction returns a new StateReplaceProviderAction instance.

func (*StateReplaceProviderAction) StateUpdate added in v0.3.15

StateUpdate updates a given state and returns a new state. It moves a provider from source address to destination address in the same tfstate file.

type StateRmAction

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

StateRmAction implements the StateAction interface. StateRmAction removes resources from state at given addresses.

func NewStateRmAction

func NewStateRmAction(addresses []string) *StateRmAction

NewStateRmAction returns a new StateRmAction instance.

func (*StateRmAction) StateUpdate

func (a *StateRmAction) StateUpdate(ctx context.Context, tf tfexec.TerraformCLI, state *tfexec.State) (*tfexec.State, error)

StateUpdate updates a given state and returns a new state. It removes resources from state at given addresses.

type StateXmvAction added in v0.3.10

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

StateXmvAction implements the StateAction interface. StateXmvAction is an extended version of StateMvAction. It allows you to move multiple resouces with wildcard matching.

func NewStateXmvAction added in v0.3.10

func NewStateXmvAction(source string, destination string) *StateXmvAction

NewStateXmvAction returns a new StateXmvAction instance.

func (*StateXmvAction) StateUpdate added in v0.3.10

func (a *StateXmvAction) StateUpdate(ctx context.Context, tf tfexec.TerraformCLI, state *tfexec.State) (*tfexec.State, error)

StateUpdate updates a given state and returns a new state. Source resources have wildcards which should be matched against the tf state. Each occurrence will generate a move command.

Jump to

Keyboard shortcuts

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