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..85960fd7df 100644 --- a/providers/terraform/resources/hcl.go +++ b/providers/terraform/resources/hcl.go @@ -538,8 +538,37 @@ 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 + args["block"] = nil + args["requiredProviders"] = llx.DictData(map[string]interface{}{}) + return args, nil, 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)