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)