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(parser): support nameless tf resources #6510

Merged
merged 3 commits into from
Aug 29, 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
38 changes: 29 additions & 9 deletions pkg/parser/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,39 @@ func processElements(elements model.Document, path string) {
}
}

func processResourcesElements(resourcesElements model.Document, path string) error {
for _, v2 := range resourcesElements {
switch t := v2.(type) {
case []interface{}:
return errors.New("failed to process resources")
case interface{}:
if elements, ok := t.(model.Document); ok {
processElements(elements, path)
}
}
}
return nil
}

func processResources(doc model.Document, path string) error {
var resourcesElements model.Document
for _, resources := range doc { // iterate over resources
resourcesElements = resources.(model.Document)
for _, v2 := range resourcesElements { // resource name
switch t := v2.(type) {
case []interface{}:
return errors.New("failed to process resources")
case interface{}:
if elements, ok := t.(model.Document); ok {
processElements(elements, path)
for _, resources := range doc {
switch t := resources.(type) {
case []interface{}: // support the case of nameless resources - where we get a list of resources
for _, value := range t {
resourcesElements = value.(model.Document)
err := processResourcesElements(resourcesElements, path)
if err != nil {
return err
}
}

case interface{}:
resourcesElements = t.(model.Document)
err := processResourcesElements(resourcesElements, path)
if err != nil {
return err
}
}
}
return nil
Expand Down
34 changes: 32 additions & 2 deletions pkg/parser/terraform/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ variable "default" {
type = "string"
default = "default_var_file"
}

data "aws_ami" "example" {
most_recent = true

owners = ["self"]
tags = {
Name = "app-server"
Expand All @@ -64,6 +64,22 @@ data "aws_ami" "example" {
}
}
`
namelessResource = `resource "aws_lb" {
name = "test-lb-tf-1"
internal = false
load_balancer_type = "network"
subnets = [for subnet in aws_subnet.public : subnet.id]
enable_deletion_protection = true
}

resource "aws_lb" {
name = "test-lb-tf-2"
internal = false
load_balancer_type = "network"
subnets = [for subnet in aws_subnet.public : subnet.id]
enable_deletion_protection = true
}
`
)

type fileTest struct {
Expand Down Expand Up @@ -127,6 +143,20 @@ func Test_Parentheses_Expr(t *testing.T) {
require.Contains(t, ami.(model.Document)["tags"], "Tag/default_var_file")
}

// Test_namelessResource tests the case of the nameless resource where the resource name is not specified and model.Document resource is a list
func Test_namelessResource(t *testing.T) {
parser := NewDefault()
document, _, err := parser.Parse("namelessResource.tf", []byte(namelessResource))
require.NoError(t, err)
require.Len(t, document, 1)
require.Contains(t, document[0], "resource")
require.Len(t, document[0]["resource"].(model.Document)["aws_lb"].([]interface{}), 2)
require.Equal(t, document[0]["resource"].(model.Document)["aws_lb"].([]interface{})[0].(model.Document)["name"],
"test-lb-tf-1")
require.Equal(t, document[0]["resource"].(model.Document)["aws_lb"].([]interface{})[1].(model.Document)["name"],
"test-lb-tf-2")
}

// Test_Resolve tests the functions [Resolve()] and all the methods called by them
func Test_Resolve(t *testing.T) {
parser := NewDefault()
Expand Down
Loading