diff --git a/CHANGELOG.md b/CHANGELOG.md index 69fc6914..22f5c979 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ -## v2.0.1 +## v2.1.0 (unreleased) FEATURES: +- Support `-var-file` option to specify the path to the terraform variable file. - Support migrating resources from `azurerm` provider to `azapi` provider. ## v2.0.0-beta diff --git a/cmd/migrate_command.go b/cmd/migrate_command.go index 6469b2cd..6361b6ae 100644 --- a/cmd/migrate_command.go +++ b/cmd/migrate_command.go @@ -24,6 +24,7 @@ type MigrateCommand struct { Verbose bool Strict bool workingDir string + varFile string TargetProvider string } @@ -32,6 +33,7 @@ func (c *MigrateCommand) flags() *flag.FlagSet { fs.BoolVar(&c.Verbose, "v", false, "whether show terraform logs") fs.BoolVar(&c.Strict, "strict", false, "strict mode: API versions must be matched") fs.StringVar(&c.workingDir, "working-dir", "", "path to Terraform configuration files") + fs.StringVar(&c.varFile, "var-file", "", "path to the terraform variable file") fs.StringVar(&c.TargetProvider, "to", "", "Specify the provider to migrate to. The allowed values are: azurerm and azapi. Default is azurerm.") fs.Usage = func() { c.Ui.Error(c.Help()) } @@ -76,6 +78,8 @@ func (c *MigrateCommand) Run(args []string) int { Ui: c.Ui, Verbose: c.Verbose, Strict: c.Strict, + workingDir: c.workingDir, + varFile: c.varFile, TargetProvider: c.TargetProvider, } allResources := planCommand.Plan(terraform, false) diff --git a/cmd/migrate_command_test.go b/cmd/migrate_command_test.go index 45682cf9..a7c82dd4 100644 --- a/cmd/migrate_command_test.go +++ b/cmd/migrate_command_test.go @@ -111,7 +111,7 @@ provider "azurerm" { ErrorWriter: os.Stderr, }, } - p, err := terraform.Plan() + p, err := terraform.Plan(nil) if err != nil { log.Fatal(err) } @@ -182,7 +182,7 @@ provider "azurerm" { } // check no plan-diff - plan, err := terraform.Plan() + plan, err := terraform.Plan(nil) if err != nil { t.Fatal(err) } diff --git a/cmd/plan_command.go b/cmd/plan_command.go index a107b5c6..06897269 100644 --- a/cmd/plan_command.go +++ b/cmd/plan_command.go @@ -20,6 +20,7 @@ type PlanCommand struct { Verbose bool Strict bool workingDir string + varFile string TargetProvider string } @@ -28,6 +29,7 @@ func (c *PlanCommand) flags() *flag.FlagSet { fs.BoolVar(&c.Verbose, "v", false, "whether show terraform logs") fs.BoolVar(&c.Strict, "strict", false, "strict mode: API versions must be matched") fs.StringVar(&c.workingDir, "working-dir", "", "path to Terraform configuration files") + fs.StringVar(&c.varFile, "var-file", "", "path to the terraform variable file") fs.StringVar(&c.TargetProvider, "to", "", "Specify the provider to migrate to. The allowed values are: azurerm and azapi. Default is azurerm.") fs.Usage = func() { c.Ui.Error(c.Help()) } return fs @@ -78,7 +80,7 @@ func (c *PlanCommand) Synopsis() string { func (c *PlanCommand) Plan(terraform *tf.Terraform, isPlanOnly bool) []types.AzureResource { // get azapi resource from state log.Printf("[INFO] running terraform plan...") - p, err := terraform.Plan() + p, err := terraform.Plan(&c.varFile) if err != nil { log.Fatal(err) } diff --git a/tf/terraform.go b/tf/terraform.go index c7953480..290701d2 100644 --- a/tf/terraform.go +++ b/tf/terraform.go @@ -69,8 +69,12 @@ func (t *Terraform) Show() (*tfjson.State, error) { return t.exec.Show(context.TODO()) } -func (t *Terraform) Plan() (*tfjson.Plan, error) { - _, err := t.exec.Plan(context.TODO(), tfexec.Out(planfile)) +func (t *Terraform) Plan(varFile *string) (*tfjson.Plan, error) { + planOptions := []tfexec.PlanOption{tfexec.Out(planfile)} + if varFile != nil && *varFile != "" { + planOptions = append(planOptions, tfexec.VarFile(*varFile)) + } + _, err := t.exec.Plan(context.TODO(), planOptions...) if err != nil { return nil, err }