From 7f28553440cd5b92990c6e3dca5917cf8449c1c0 Mon Sep 17 00:00:00 2001 From: Mike Ball Date: Thu, 15 Jun 2023 11:51:16 -0400 Subject: [PATCH] publicly expose terraform.ParsePlanJSON function This seeks to fix #1308 by publicly exposing the `terraform.ParsePlanJSON` function, in effect better enabling the use of `terratest` for programmatically analyzing and validating a Terraform plan produced by an upstream, non-`terratest` process. Signed-off-by: Mike Ball --- modules/terraform/plan.go | 2 +- modules/terraform/plan_struct.go | 4 ++-- modules/terraform/plan_struct_test.go | 6 +++--- modules/terraform/show.go | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/terraform/plan.go b/modules/terraform/plan.go index a804ab563..1b3730b80 100644 --- a/modules/terraform/plan.go +++ b/modules/terraform/plan.go @@ -93,7 +93,7 @@ func InitAndPlanAndShowWithStructE(t testing.TestingT, options *Options) (*PlanS if err != nil { return nil, err } - return parsePlanJson(jsonOut) + return ParsePlanJSON(jsonOut) } // InitAndPlanWithExitCode runs terraform init and plan with the given options and returns exitcode for the plan command. diff --git a/modules/terraform/plan_struct.go b/modules/terraform/plan_struct.go index ae6c6815d..e385825f1 100644 --- a/modules/terraform/plan_struct.go +++ b/modules/terraform/plan_struct.go @@ -27,9 +27,9 @@ type PlanStruct struct { ResourceChangesMap map[string]*tfjson.ResourceChange } -// parsePlanJson takes in the json string representation of the terraform plan and returns a go struct representation +// ParsePlanJSON takes in the json string representation of the terraform plan and returns a go struct representation // for easy introspection. -func parsePlanJson(jsonStr string) (*PlanStruct, error) { +func ParsePlanJSON(jsonStr string) (*PlanStruct, error) { plan := &PlanStruct{} if err := json.Unmarshal([]byte(jsonStr), &plan.RawPlan); err != nil { diff --git a/modules/terraform/plan_struct_test.go b/modules/terraform/plan_struct_test.go index b1e5c3ed8..a3551b3f7 100644 --- a/modules/terraform/plan_struct_test.go +++ b/modules/terraform/plan_struct_test.go @@ -23,7 +23,7 @@ func TestPlannedValuesMapWithBasicJson(t *testing.T) { // Retrieve test data from the terraform-json project. _, jsonData := http_helper.HttpGet(t, basicJsonUrl, nil) - plan, err := parsePlanJson(jsonData) + plan, err := ParsePlanJSON(jsonData) require.NoError(t, err) query := []string{ @@ -48,7 +48,7 @@ func TestPlannedValuesMapWithDeepModuleJson(t *testing.T) { // Retrieve test data from the terraform-json project. _, jsonData := http_helper.HttpGet(t, deepModuleJsonUrl, nil) - plan, err := parsePlanJson(jsonData) + plan, err := ParsePlanJSON(jsonData) require.NoError(t, err) query := []string{ @@ -64,7 +64,7 @@ func TestResourceChangesJson(t *testing.T) { // Retrieve test data from the terraform-json project. _, jsonData := http_helper.HttpGet(t, changesJsonUrl, nil) - plan, err := parsePlanJson(jsonData) + plan, err := ParsePlanJSON(jsonData) require.NoError(t, err) // Spot check a few changes to make sure the right address was registered diff --git a/modules/terraform/show.go b/modules/terraform/show.go index c21019488..860ef0c6b 100644 --- a/modules/terraform/show.go +++ b/modules/terraform/show.go @@ -40,7 +40,7 @@ func ShowWithStructE(t testing.TestingT, options *Options) (*PlanStruct, error) if err != nil { return nil, err } - planStruct, err := parsePlanJson(json) + planStruct, err := ParsePlanJSON(json) if err != nil { return nil, err }