Skip to content

Commit

Permalink
🐛 avoid crashing terraform on unsupported plan calls (#1944)
Browse files Browse the repository at this point in the history
Happens when we call plan resources without operating on a Terraform
plan. This needs better handling for unsupported or invalid data. I
strongly recommend we instead inform users that there are no plan files
via an error in the future. For now, create compatibility with v8 to get
it release-ready.

Signed-off-by: Dominik Richter <[email protected]>
  • Loading branch information
arlimus authored Sep 27, 2023
1 parent 5abdc22 commit 8973e83
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions providers/terraform/resources/tfplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@ func (t *mqlTerraformPlan) id() (string, error) {
}

func initTerraformPlan(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
if len(args) != 0 {
return args, nil, nil
}
conn := runtime.Connection.(*connection.Connection)

plan, err := conn.Plan()
if err != nil {
return nil, nil, err
}

// TODO: This only creates compatibility with v8. Please revisit this section
// after https://github.com/mondoohq/cnquery/issues/1943 is clarified.
if plan == nil {
return map[string]*llx.RawData{
"formatVersion": llx.StringData(""),
"terraformVersion": llx.StringData(""),
"resourceChanges": llx.ArrayData([]interface{}{}, types.Resource("terraform.plan.resourceChange")),
}, nil, nil
}

args["formatVersion"] = llx.StringData(plan.FormatVersion)
args["terraformVersion"] = llx.StringData(plan.TerraformVersion)

Expand Down Expand Up @@ -154,6 +167,12 @@ func (t *mqlTerraformPlanConfiguration) providerConfig() ([]interface{}, error)
return nil, err
}

// TODO: This only creates compatibility with v8. Please revisit this section
// after https://github.com/mondoohq/cnquery/issues/1943 is clarified.
if plan == nil {
return []interface{}{}, nil
}

if plan.Configuration == nil {
return nil, nil
}
Expand All @@ -180,6 +199,12 @@ func (t *mqlTerraformPlanConfiguration) resources() ([]interface{}, error) {
return nil, err
}

// TODO: This only creates compatibility with v8. Please revisit this section
// after https://github.com/mondoohq/cnquery/issues/1943 is clarified.
if plan == nil {
return []interface{}{}, nil
}

if plan.Configuration == nil {
return nil, nil
}
Expand Down

0 comments on commit 8973e83

Please sign in to comment.