Skip to content

Commit

Permalink
🐛 migrate missing terraform.settings (#1945)
Browse files Browse the repository at this point in the history
* 🐛 migrate missing terraform.settings

* 🐛 fix terraform block filter to work with non-HCL

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
  • Loading branch information
arlimus authored Sep 27, 2023
1 parent faed5ce commit f60f101
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"type": "go",
"request": "launch",
"program": "${workspaceRoot}/apps/cnquery/cnquery.go",
"cwd": "${workspaceRoot}/",
"args": [
"run",
"local",
Expand Down
43 changes: 40 additions & 3 deletions providers/terraform/resources/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 2 additions & 7 deletions providers/terraform/resources/terraform.lr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f60f101

Please sign in to comment.