Skip to content

Commit

Permalink
Merge pull request #3 from albertodonato/aws-region-name-preset
Browse files Browse the repository at this point in the history
fix(terraform): add aws_region name to presets
  • Loading branch information
fwereade authored Jul 17, 2024
2 parents 7d824ac + ed05fef commit 15bfb2b
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 @@ -1797,3 +1797,42 @@ resource "test" "values" {
require.NotNil(t, bad_attr_2)
assert.Nil(t, bad_attr_2.GetRawValue())
}

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)
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())
assert.Equal(t, r1.Value().AsString(), "current-region")

r2 := block.GetAttribute("r2")
require.NotNil(t, r2)
assert.Equal(t, true, r2.IsResolvable())
assert.Equal(t, r2.Value().AsString(), "us-east-2")
}
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 15bfb2b

Please sign in to comment.