tfmigrator

package
Version: v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2021 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func QuickRun

func QuickRun(ctx context.Context, planner Planner) error

QuickRun provides CLI interface to run tfmigrator quickly. `flag` package is used.

-help
-dry-run
-log-level
-state - source state file path
args - Terraform Configuration file paths

QuickRun is a simple helper function and is designed to implement CLI easily. If you want to customize QuickRun, you can use other low level API like `Runner`.

func QuickRunBatch added in v0.5.0

func QuickRunBatch(ctx context.Context, planner BatchPlanner) error

QuickRunBatch provides CLI interface to run tfmigrator quickly. `flag` package is used.

-help
-dry-run
-log-level
-state - source state file path
args - Terraform Configuration file paths

Compared with QuickRun, QuickRunBatch is a low level API.

func ValidateResourceName

func ValidateResourceName(name string) error

ValidateResourceName validates a Terraform Resource name.

Types

type BatchPlanFunc added in v0.5.0

type BatchPlanFunc func(state *tfjson.State, addressFileMap map[string]string) ([]Result, error)

BatchPlanFunc plans how Terraform resource are migrated.

type BatchPlanner added in v0.5.0

type BatchPlanner interface {
	Plan(state *tfjson.State, addressFileMap map[string]string) ([]Result, error)
}

BatchPlanner plans how Terraform resources are migrated. Note that BatchPlanner itself doesn't change Terraform State and Terraform Configuration files. BatchPlanner determines the updated resource name, outputted State file path, and outputted Terraform Configuration file path. tfmigrator migrates according to the plan of BatchPlanner. Compared with Planner, BatchPlanner is a low level API. If you want to use the dependency between resources, BatchPlanner is more appropriate than Planner.

func NewBatchPlanner added in v0.5.0

func NewBatchPlanner(fn BatchPlanFunc) BatchPlanner

NewBatchPlanner is a helper function to create BatchPlanner from PlanFunc.

type BatchRunner added in v0.5.0

type BatchRunner struct {
	Planner     BatchPlanner `validate:"required"`
	Logger      log.Logger
	HCLEdit     *hcledit.Client `validate:"required"`
	StateReader *tfstate.Reader `validate:"required"`
	Migrator    *Migrator
	Outputter   Outputter
	DryRun      bool
}

BatchRunner migrates Terraform Configuration and State with BatchPlanner.

func (*BatchRunner) Run added in v0.5.0

func (runner *BatchRunner) Run(ctx context.Context, opt *RunOpt) error

Run reads Terraform Configuration and State and migrate them.

type MigratedResource

type MigratedResource struct {
	// Address is a new resource address.
	// If Address is empty, the address isn't changed.
	Address string
	// Dirname is a directory path where Terraform Configuration file and State exists.
	// HCLFileBasename is a file name of Terraform Configuration.
	// StateBasename is a file name of Terraform State.
	// If Dirname and StateBasename is empty, the same State is updated.
	// If Dirname and HCLFileBasename is empty, the Terraform Configuration is updated in-place.
	Dirname         string
	HCLFileBasename string
	StateBasename   string
	// If Removed is true, the Resource is removed from Terraform Configuration and State.
	Removed            bool
	SkipHCLMigration   bool
	SkipStateMigration bool
}

MigratedResource is a plan how a resource is migrated.

func (*MigratedResource) HCLAddress added in v0.3.0

func (rsc *MigratedResource) HCLAddress() string

HCLAddress returns a resource address like `resource.null_resource.foo`.

func (*MigratedResource) HCLFilePath added in v0.5.0

func (rsc *MigratedResource) HCLFilePath() string

HCLFilePath returns a file path to the Terraform Configuration file where the migrated Configuration is written.

func (*MigratedResource) StatePath

func (rsc *MigratedResource) StatePath() string

StatePath returns a file path to Terraform State file.

type Migrator added in v0.5.0

type Migrator struct {
	Stdout       io.Writer        `validate:"required"`
	HCLEdit      *hcledit.Client  `validate:"required"`
	StateUpdater *tfstate.Updater `validate:"required"`
	DryRun       bool
}

Migrator migrates Terraform Configuration and State.

func (*Migrator) Migrate added in v0.5.0

func (migrator *Migrator) Migrate(ctx context.Context, src *Source, migratedResource *MigratedResource) error

Migrate migrates Terraform Configuration and State with `terraform state mv` and `hcledit`.

func (*Migrator) MigrateHCL added in v0.5.0

func (migrator *Migrator) MigrateHCL(src *Source, migratedResource *MigratedResource) error

MigrateHCL migrate Terraform Configuration file.

func (*Migrator) MigrateState added in v0.5.0

func (migrator *Migrator) MigrateState(ctx context.Context, src *Source, migratedResource *MigratedResource) error

MigrateState migrates Terraform State.

type Outputter

type Outputter interface {
	Output([]Result) error
}

Outputter outputs the result.

func NewYAMLOutputter

func NewYAMLOutputter(out io.Writer) Outputter

NewYAMLOutputter returns a YAMLOutputter.

type PlanFunc

type PlanFunc func(src *Source) (*MigratedResource, error)

PlanFunc plans how a Terraform resource is migrated.

type Planner

type Planner interface {
	Plan(src *Source) (*MigratedResource, error)
}

Planner plans how a Terraform resource is migrated. Note that Planner itself doesn't change Terraform State and Terraform Configuration files. Planner determines the updated resource name, outputted State file path, and outputted Terraform Configuration file path. tfmigrator migrates according to the plan of Planner.

func CombinePlanners

func CombinePlanners(planners ...Planner) Planner

CombinePlanners creates a migrator from given migrators.

func NewPlanner

func NewPlanner(fn PlanFunc) Planner

NewPlanner is a helper function to create Planner from PlanFunc.

type Result

type Result struct {
	Source           *Source
	MigratedResource *MigratedResource
}

Result contains source Terraform Resource and the plan how the resource is migrated. Result is used to output the result by Outputter.

type RunOpt

type RunOpt struct {
	// SourceStatePath is the file path to State.
	// If SourceStatePath is empty, State is read by `terraform show -json` command.
	SourceStatePath string
	// SourceHCLFilePaths is a list of Terraform Configuration file paths.
	SourceHCLFilePaths []string `validate:"required"`
}

RunOpt is an option of Runner#Run method.

type Runner

type Runner struct {
	Planner     Planner `validate:"required"`
	Logger      log.Logger
	HCLEdit     *hcledit.Client `validate:"required"`
	StateReader *tfstate.Reader `validate:"required"`
	Outputter   Outputter
	Migrator    *Migrator
	DryRun      bool
}

Runner provides high level API to migrate Terraform Configuration and State.

func (*Runner) Run

func (runner *Runner) Run(ctx context.Context, opt *RunOpt) error

Run reads Terraform Configuration and State and migrate them.

type Source

type Source struct {
	Resource *tfjson.StateResource
	Module   *tfjson.StateModule
	// If the resource isn't found in Terraform Configuration files, HCLFilePath is empty
	HCLFilePath string
	StatePath   string
}

Source is a source Terraform resource.

func (*Source) Address

func (src *Source) Address() string

Address returns a resource address like `null_resource.foo`.

func (*Source) HCLAddress added in v0.3.0

func (src *Source) HCLAddress() string

HCLAddress returns a resource address like `resource.null_resource.foo`.

type YAMLOutputter

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

YAMLOutputter outputs the result with YAML format.

func (*YAMLOutputter) Output

func (outputter *YAMLOutputter) Output(results []Result) error

Output outputs the result with YAML format.

Directories

Path Synopsis
hcledit is a wrapper of https://github.com/minamijoyo/hcledit .
hcledit is a wrapper of https://github.com/minamijoyo/hcledit .

Jump to

Keyboard shortcuts

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