Skip to content

Commit

Permalink
fix(parser): support nameless tf resources
Browse files Browse the repository at this point in the history
  • Loading branch information
liorj-orca committed Aug 16, 2023
1 parent fdea929 commit 512eff3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
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

0 comments on commit 512eff3

Please sign in to comment.