Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support import block for tf 1.5.0+ #1339

Merged
merged 2 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/scanners/terraform/parser/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func (e *evaluator) getValuesByBlockType(blockType string) cty.Value {
continue
}
values[b.Label()] = val
case "locals", "moved":
case "locals", "moved", "import":
for key, val := range b.Values().AsValueMap() {
values[key] = val
}
Expand Down
14 changes: 13 additions & 1 deletion pkg/scanners/terraform/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ moved {

}

import {
to = cats_cat.mittens
id = "mittens"
}

resource "cats_cat" "mittens" {
name = "mittens"
special = true
}

resource "cats_kitten" "the-great-destroyer" {
name = "the great destroyer"
parent = cats_cat.mittens.name
parent = cats_cat.mittens.name
}

data "cats_cat" "the-cats-mother" {
Expand Down Expand Up @@ -105,6 +110,13 @@ data "cats_cat" "the-cats-mother" {
assert.Equal(t, "the great destroyer", resourceBlocks[1].GetAttribute("name").Value().AsString())
assert.Equal(t, "mittens", resourceBlocks[1].GetAttribute("parent").Value().AsString())

// import
importBlocks := blocks.OfType("import")

assert.Equal(t, "import", importBlocks[0].Type())
require.NotNil(t, importBlocks[0].GetAttribute("to"))
assert.Equal(t, "mittens", importBlocks[0].GetAttribute("id").Value().AsString())

// data
dataBlocks := blocks.OfType("data")
require.Len(t, dataBlocks, 1)
Expand Down
3 changes: 3 additions & 0 deletions pkg/terraform/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@ var Schema = &hcl.BodySchema{
{
Type: "moved",
},
{
Type: "import",
},
},
}
5 changes: 5 additions & 0 deletions pkg/terraform/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ var TypeVariable = Type{
refName: "var",
}

var TypeImport = Type{
name: "import",
}

var TypeLocal = Type{
name: "locals",
refName: "local",
Expand Down Expand Up @@ -60,6 +64,7 @@ var TypeTerraform = Type{

var ValidTypes = []Type{
TypeData,
TypeImport,
TypeLocal,
TypeModule,
TypeMoved,
Expand Down