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

fix: account for multiple chars in resource id[IAC-3015] #298

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
3 changes: 3 additions & 0 deletions changes/unreleased/Fixed-20240725-160823.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Fixed
body: fix location computation for resources with multi-char ID inside square brackets
time: 2024-07-25T16:08:23.452724+03:00
2 changes: 1 addition & 1 deletion pkg/hcl_interpreter/source_locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

// Utility to strip out "[x]" parts from resource IDs.
var resourceIdBracketPattern = regexp.MustCompile(`\[[^[*]\]`)
var resourceIdBracketPattern = regexp.MustCompile(`\[[^[*]+\]`)

func (v *Evaluation) Location(
resourceId string,
Expand Down
35 changes: 35 additions & 0 deletions pkg/input/golden_location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,41 @@ var goldenLocationTests = []goldenLocationTest{
},
},
},
{
path: []interface{}{
"golden_test/tf/count-simple",
"aws_s3_bucket",
"aws_s3_bucket.example[10]", // test also double digit resource id
"bucket_prefix",
},
expected: LocationStack{
{
Path: "main.tf",
Line: 17,
Col: 3,
},
},
},
},
},
{
directory: "golden_test/tf/for-each",
cases: []goldenLocationTestCase{
{
path: []interface{}{
"golden_test/tf/for-each",
"aws_s3_bucket",
"aws_s3_bucket.mybuckets[prodabc123]",
"bucket_prefix",
},
expected: LocationStack{
{
Path: "main.tf",
Line: 27,
Col: 3,
},
},
},
},
},
}
Expand Down
72 changes: 72 additions & 0 deletions pkg/input/golden_test/tf/count-simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
"bucket_prefix": "example_0"
}
},
"aws_s3_bucket.example[10]": {
"id": "aws_s3_bucket.example[10]",
"resource_type": "aws_s3_bucket",
"namespace": "golden_test/tf/count-simple/main.tf",
"meta": {},
"attributes": {
"bucket_prefix": "example_10"
}
},
"aws_s3_bucket.example[1]": {
"id": "aws_s3_bucket.example[1]",
"resource_type": "aws_s3_bucket",
Expand All @@ -34,6 +43,69 @@
"attributes": {
"bucket_prefix": "example_2"
}
},
"aws_s3_bucket.example[3]": {
"id": "aws_s3_bucket.example[3]",
"resource_type": "aws_s3_bucket",
"namespace": "golden_test/tf/count-simple/main.tf",
"meta": {},
"attributes": {
"bucket_prefix": "example_3"
}
},
"aws_s3_bucket.example[4]": {
"id": "aws_s3_bucket.example[4]",
"resource_type": "aws_s3_bucket",
"namespace": "golden_test/tf/count-simple/main.tf",
"meta": {},
"attributes": {
"bucket_prefix": "example_4"
}
},
"aws_s3_bucket.example[5]": {
"id": "aws_s3_bucket.example[5]",
"resource_type": "aws_s3_bucket",
"namespace": "golden_test/tf/count-simple/main.tf",
"meta": {},
"attributes": {
"bucket_prefix": "example_5"
}
},
"aws_s3_bucket.example[6]": {
"id": "aws_s3_bucket.example[6]",
"resource_type": "aws_s3_bucket",
"namespace": "golden_test/tf/count-simple/main.tf",
"meta": {},
"attributes": {
"bucket_prefix": "example_6"
}
},
"aws_s3_bucket.example[7]": {
"id": "aws_s3_bucket.example[7]",
"resource_type": "aws_s3_bucket",
"namespace": "golden_test/tf/count-simple/main.tf",
"meta": {},
"attributes": {
"bucket_prefix": "example_7"
}
},
"aws_s3_bucket.example[8]": {
"id": "aws_s3_bucket.example[8]",
"resource_type": "aws_s3_bucket",
"namespace": "golden_test/tf/count-simple/main.tf",
"meta": {},
"attributes": {
"bucket_prefix": "example_8"
}
},
"aws_s3_bucket.example[9]": {
"id": "aws_s3_bucket.example[9]",
"resource_type": "aws_s3_bucket",
"namespace": "golden_test/tf/count-simple/main.tf",
"meta": {},
"attributes": {
"bucket_prefix": "example_9"
}
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/input/golden_test/tf/count-simple/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
# limitations under the License.

resource "aws_s3_bucket" "example" {
count = 3
count = 11
bucket_prefix = "example_${count.index}"
}
Loading