Skip to content

Commit

Permalink
feat: support tf<0.12 var type expressions (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwereade authored Jun 7, 2024
1 parent bd1eac9 commit ce9fe22
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gotfparse/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ require (
google.golang.org/protobuf v1.33.0 // indirect
)

replace github.com/aquasecurity/trivy v0.50.1 => github.com/cloud-custodian/trivy v0.0.0-20240424083515-3f81c1ae3a4b
replace github.com/aquasecurity/trivy v0.50.1 => github.com/cloud-custodian/trivy v0.0.0-20240607110140-7d824ace9240
4 changes: 2 additions & 2 deletions gotfparse/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWR
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloud-custodian/trivy v0.0.0-20240424083515-3f81c1ae3a4b h1:8OnMK004B5bmzH010iBsMMr9mgTBLq5xj01M1O6MmRI=
github.com/cloud-custodian/trivy v0.0.0-20240424083515-3f81c1ae3a4b/go.mod h1:J1fAS5qlA63Amg4RP5fF8QsF67xvZC5RPZORsibbQQs=
github.com/cloud-custodian/trivy v0.0.0-20240607110140-7d824ace9240 h1:inb+4gL5NdxD8HAlpz77Owce4YzgWPSDXfh09zctmP8=
github.com/cloud-custodian/trivy v0.0.0-20240607110140-7d824ace9240/go.mod h1:J1fAS5qlA63Amg4RP5fF8QsF67xvZC5RPZORsibbQQs=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
Expand Down
25 changes: 25 additions & 0 deletions tests/terraform/vars-bad-types/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
variable "empty_block" {
// nothing here
}

output "empty_block" {
value = var.empty_block
}


variable "default_only" {
default = "huh"
}

output "default_only" {
value = var.default_only
}


variable "quoted_type" {
type = "string"
}

output "quoted_type" {
value = var.quoted_type
}
3 changes: 3 additions & 0 deletions tests/terraform/vars-bad-types/numbers.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
empty_block = 123
default_only = 456
quoted_type = 789
3 changes: 3 additions & 0 deletions tests/terraform/vars-bad-types/strings.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
empty_block = "one"
default_only = "two"
quoted_type = "three"
26 changes: 26 additions & 0 deletions tests/test_tfparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,32 @@ def test_multiple_var_files(tmp_path):
assert item["name"] == "my-app-logs"


def test_vars_bad_types(tmp_path):
# NOTE that the "quoted_type" test case is to allow rudimentary support for TF
# versions older than 0.12, which are still sometimes seen in the wild. It's
# not valid in any TF that's less than 5 years old.
mod_path = init_module("vars-bad-types", tmp_path, run_init=False)
assert get_outputs(load_from_path(mod_path)) == {
"empty_block": None,
"default_only": "huh",
"quoted_type": None,
}
assert get_outputs(load_from_path(mod_path, vars_paths=["numbers.tfvars"])) == {
"empty_block": 123,
"default_only": 456, # default value doesn't imply type
"quoted_type": "789", # quoted type is handled, value is coerced
}
assert get_outputs(load_from_path(mod_path, vars_paths=["strings.tfvars"])) == {
"empty_block": "one",
"default_only": "two",
"quoted_type": "three",
}


def get_outputs(parsed):
return {block["__tfmeta"]["label"]: block["value"] for block in parsed["output"]}


def test_parse_vpc_module(tmp_path):
mod_path = init_module("vpc_module", tmp_path, run_init=False)
parsed = load_from_path(mod_path, allow_downloads=True)
Expand Down

0 comments on commit ce9fe22

Please sign in to comment.