Skip to content

Commit

Permalink
fix(terraform): add aws_region name to presets
Browse files Browse the repository at this point in the history
  • Loading branch information
albertodonato committed Jul 17, 2024
1 parent b76a725 commit f0b56dc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
39 changes: 39 additions & 0 deletions pkg/iac/scanners/terraform/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1745,3 +1745,42 @@ func TestTFVarsFileDoesNotExist(t *testing.T) {
_, _, err := parser.EvaluateAll(context.TODO())
assert.ErrorContains(t, err, "file does not exist")
}

func Test_AWSRegionNameDefined(t *testing.T) {

fs := testutil.CreateFS(t, map[string]string{
"code/test.tf": `
data "aws_region" "current" {}
data "aws_region" "other" {
name = "us-east-2"
}
resource "something" "blah" {
r1 = data.aws_region.current.name
r2 = data.aws_region.other.name
}
`,
})

parser := New(fs, "", OptionStopOnHCLError(true))
require.NoError(t, parser.ParseFS(context.TODO(), "code"))
modules, _, err := parser.EvaluateAll(context.TODO())
assert.NoError(t, err)

Check failure on line 1769 in pkg/iac/scanners/terraform/parser/parser_test.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest-m)

require-error: for error assertions use require (testifylint)
require.Len(t, modules, 1)
rootModule := modules[0]

blocks := rootModule.GetResourcesByType("something")
require.Len(t, blocks, 1)
block := blocks[0]

r1 := block.GetAttribute("r1")
require.NotNil(t, r1)
assert.Equal(t, true, r1.IsResolvable())

Check failure on line 1779 in pkg/iac/scanners/terraform/parser/parser_test.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest-m)

bool-compare: use assert.True (testifylint)
assert.Equal(t, r1.Value().AsString(), "current-region")

Check failure on line 1780 in pkg/iac/scanners/terraform/parser/parser_test.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest-m)

expected-actual: need to reverse actual and expected values (testifylint)

r2 := block.GetAttribute("r2")
require.NotNil(t, r2)
assert.Equal(t, true, r2.IsResolvable())

Check failure on line 1784 in pkg/iac/scanners/terraform/parser/parser_test.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest-m)

bool-compare: use assert.True (testifylint)
assert.Equal(t, r2.Value().AsString(), "us-east-2")

Check failure on line 1785 in pkg/iac/scanners/terraform/parser/parser_test.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest-m)

expected-actual: need to reverse actual and expected values (testifylint)
}
5 changes: 4 additions & 1 deletion pkg/iac/terraform/presets.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ func createPresetValues(b *Block) map[string]cty.Value {
presets["arn"] = cty.StringVal(b.ID())
}

// workaround for weird iam feature
switch b.TypeLabel() {
// workaround for weird iam feature
case "aws_iam_policy_document":
presets["json"] = cty.StringVal(b.ID())
// If the user leaves the name blank, Terraform will automatically generate a unique name
case "aws_launch_template":
presets["name"] = cty.StringVal(uuid.New().String())
// allow referencing the current region name
case "aws_region":
presets["name"] = cty.StringVal("current-region")
}

return presets
Expand Down

0 comments on commit f0b56dc

Please sign in to comment.