Skip to content

Commit

Permalink
support -var-file option
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu committed Nov 11, 2024
1 parent 067a517 commit 0500284
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions cmd/migrate_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type MigrateCommand struct {
Verbose bool
Strict bool
workingDir string
varFile string
TargetProvider string
}

Expand All @@ -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()) }
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion cmd/plan_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type PlanCommand struct {
Verbose bool
Strict bool
workingDir string
varFile string
TargetProvider string
}

Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 6 additions & 2 deletions tf/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
planOptions = append(planOptions, tfexec.VarFile(varFile))
}
_, err := t.exec.Plan(context.TODO(), planOptions...)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 0500284

Please sign in to comment.