Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

support check block for tf 1.5.0+ #1354

Merged
merged 1 commit into from
Jun 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 @@ -389,7 +389,7 @@ func (e *evaluator) getValuesByBlockType(blockType string) cty.Value {
for key, val := range b.Values().AsValueMap() {
values[key] = val
}
case "provider", "module":
case "provider", "module", "check":
if b.Label() == "" {
continue
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/scanners/terraform/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ data "cats_cat" "the-cats-mother" {
name = local.proxy
}

check "cats_mittens_is_special" {
data "cats_cat" "mittens" {
name = "mittens"
}

assert {
condition = data.cats_cat.mittens.special == true
error_message = "${data.cats_cat.mittens.name} must be special"
}
}

`,
})

Expand Down Expand Up @@ -127,6 +138,17 @@ data "cats_cat" "the-cats-mother" {
assert.Equal(t, "the-cats-mother", dataBlocks[0].NameLabel())

assert.Equal(t, "boots", dataBlocks[0].GetAttribute("name").Value().AsString())

// check
checkBlocks := blocks.OfType("check")
require.Len(t, checkBlocks, 1)
require.Len(t, checkBlocks[0].Labels(), 1)

assert.Equal(t, "check", checkBlocks[0].Type())
assert.Equal(t, "cats_mittens_is_special", checkBlocks[0].TypeLabel())

require.NotNil(t, checkBlocks[0].GetBlock("data"))
require.NotNil(t, checkBlocks[0].GetBlock("assert"))
}

func Test_Modules(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/terraform/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ var Schema = &hcl.BodySchema{
Type: "module",
LabelNames: []string{"name"},
},
{
Type: "check",
LabelNames: []string{"name"},
},
{
Type: "resource",
LabelNames: []string{"type", "name"},
Expand Down
5 changes: 5 additions & 0 deletions pkg/terraform/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func (t Type) ShortName() string {
return t.name
}

var TypeCheck = Type{
name: "check",
}

var TypeData = Type{
name: "data",
}
Expand Down Expand Up @@ -63,6 +67,7 @@ var TypeTerraform = Type{
}

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