Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement replacement for "terraform.ReadPlan is no longer in use"

Tags:

terraform

Microsoft provides an example for testing Terraform infrastructure using Terratest: https://learn.microsoft.com/en-us/azure/terraform/terratest-in-terraform-modules There is an interesting part of code:

        // Terraform init and plan only
        tfPlanOutput := "terraform.tfplan"
        terraform.Init(t, tfOptions)
        terraform.RunTerraformCommand(t, tfOptions, terraform.FormatArgs(tfOptions.Vars, "plan", "-out="+tfPlanOutput)...)

        // Read and parse the plan output
        f, err := os.Open(path.Join(tfOptions.TerraformDir, tfPlanOutput))
        if err != nil {
            t.Fatal(err)
        }
        defer f.Close()
        plan, err := terraformCore.ReadPlan(f)

If you try to execute it on Terraform 0.12, you get error:

terraform.ReadPlan is no longer in use; use planfile.Open instead

The question is, how can I reimplement Microsoft's code to work with Terraform 0.12? How can I convert *File into Plan?

like image 240
Grzegorz P Avatar asked Dec 28 '25 21:12

Grzegorz P


1 Answers

This error message is aimed at people working on the Terraform codebase itself, rather than external callers. The terraform Go package is not a public API and so while it is technically possible for other Go codebases to call into it, there are no compatibility promises for it and any other systems calling into it are likely to be broken (as we see in this case) by refactoring in future releases.

With that said, Terraform 0.12 introduced a supported way to decode a saved plan file for inspection in other software, via terraform show -json <planfile>.

terratest has a function RunTerraformCommandAndGetStdoutE which you could use to run terraform show -json <planfile> and get the raw JSON output:

planJSON, err := terraform.RunTerraformCommandAndGetStdoutE(
    t, tfOptions, terraform.FormatArgs("show", "-json", tfPlanOutput)...
)

Then you can use the Go standard library encoding/json package to parse it into some struct types of your own that cover the subset of the JSON output format you need for the tests you intend to write.

like image 172
Martin Atkins Avatar answered Jan 01 '26 05:01

Martin Atkins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!