From f60f1018da51f199365ccce52a83ab96f248a1ee Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Wed, 27 Sep 2023 02:39:42 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20migrate=20missing=20terraform.se?= =?UTF-8?q?ttings=20(#1945)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐛 migrate missing terraform.settings * 🐛 fix terraform block filter to work with non-HCL Signed-off-by: Dominik Richter --- .vscode/launch.json | 1 + providers/terraform/resources/hcl.go | 43 +++++++++++++++++-- providers/terraform/resources/terraform.lr.go | 9 +--- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 4c377740b7..569da0563a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -41,6 +41,7 @@ "type": "go", "request": "launch", "program": "${workspaceRoot}/apps/cnquery/cnquery.go", + "cwd": "${workspaceRoot}/", "args": [ "run", "local", diff --git a/providers/terraform/resources/hcl.go b/providers/terraform/resources/hcl.go index fe6cbad554..dc0cc3bd35 100644 --- a/providers/terraform/resources/hcl.go +++ b/providers/terraform/resources/hcl.go @@ -95,7 +95,13 @@ func (t *mqlTerraform) blocks() ([]interface{}, error) { func filterBlockByType(runtime *plugin.Runtime, filterType string) ([]interface{}, error) { conn := runtime.Connection.(*connection.Connection) - files := conn.Parser().Files() + parsed := conn.Parser() + if parsed == nil { + // no results, because this is not a regular parsed HCL + return []interface{}{}, nil + } + + files := parsed.Files() var mqlHclBlocks []interface{} for k := range files { @@ -538,8 +544,39 @@ func (t *mqlTerraformModule) block() (*mqlTerraformBlock, error) { return mqlHclBlock, nil } -func (g *mqlTerraformSettings) id() (string, error) { - return "terraform.settings", nil +func initTerraformSettings(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) { + blocks, err := filterBlockByType(runtime, "terraform") + if err != nil { + return nil, nil, err + } + + if len(blocks) != 1 { + // no terraform settings block found, this is ok for terraform and not an error + // TODO: return modified arguments to load from recording + return nil, &mqlTerraformSettings{ + Block: plugin.TValue[*mqlTerraformBlock]{State: plugin.StateIsSet | plugin.StateIsNull}, + RequiredProviders: plugin.TValue[interface{}]{State: plugin.StateIsSet, Data: []interface{}{}}, + }, nil + } + + settingsBlock := blocks[0].(*mqlTerraformBlock) + args["block"] = llx.ResourceData(settingsBlock, "terraform.block") + args["requiredProviders"] = llx.DictData(map[string]interface{}{}) + + if settingsBlock.block.State == plugin.StateIsSet { + hb := settingsBlock.block.Data + requireProviderBlock := getBlockByName(hb, "required_providers") + if requireProviderBlock != nil { + attributes, _ := requireProviderBlock.Body.JustAttributes() + dict, err := hclResolvedAttributesToDict(attributes) + if err != nil { + return nil, nil, err + } + args["requiredProviders"] = llx.DictData(dict) + } + } + + return args, nil, nil } func getBlockByName(hb *hcl.Block, name string) *hcl.Block { diff --git a/providers/terraform/resources/terraform.lr.go b/providers/terraform/resources/terraform.lr.go index 4656c5bdd0..a1048eb4d1 100644 --- a/providers/terraform/resources/terraform.lr.go +++ b/providers/terraform/resources/terraform.lr.go @@ -38,7 +38,7 @@ func init() { Create: createTerraformModule, }, "terraform.settings": { - // to override args, implement: initTerraformSettings(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) + Init: initTerraformSettings, Create: createTerraformSettings, }, "terraform.state": { @@ -1297,12 +1297,7 @@ func createTerraformSettings(runtime *plugin.Runtime, args map[string]*llx.RawDa return res, err } - if res.__id == "" { - res.__id, err = res.id() - if err != nil { - return nil, err - } - } + // to override __id implement: id() (string, error) if runtime.HasRecording { args, err = runtime.ResourceFromRecording("terraform.settings", res.__id)