Skip to content

Commit 618c5d6

Browse files
authored
rules: Use WalkResources to avoid a bug in JSON syntax (#130)
1 parent d580add commit 618c5d6

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

rules/google_project_iam_audit_config_invalid_member.go

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
hcl "github.com/hashicorp/hcl/v2"
7+
"github.com/terraform-linters/tflint-plugin-sdk/terraform/configs"
78
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
89
"github.com/terraform-linters/tflint-ruleset-google/project"
910
)
@@ -45,32 +46,44 @@ func (r *GoogleProjectIamAuditConfigInvalidMemberRule) Link() string {
4546

4647
// Check checks whether member format is invalid
4748
func (r *GoogleProjectIamAuditConfigInvalidMemberRule) Check(runner tflint.Runner) error {
48-
return runner.WalkResourceBlocks(r.resourceType, r.blockName, func(block *hcl.Block) error {
49-
content, _, diags := block.Body.PartialContent(&hcl.BodySchema{
50-
Attributes: []hcl.AttributeSchema{
51-
{Name: r.attributeName},
52-
},
49+
return runner.WalkResources(r.resourceType, func(resource *configs.Resource) error {
50+
content, _, diags := resource.Config.PartialContent(&hcl.BodySchema{
51+
Blocks: []hcl.BlockHeaderSchema{{Type: r.blockName}},
5352
})
5453
if diags.HasErrors() {
5554
return diags
5655
}
5756

58-
if attribute, exists := content.Attributes[r.attributeName]; exists {
59-
var members []string
60-
err := runner.EvaluateExpr(attribute.Expr, &members, nil)
57+
for _, block := range content.Blocks {
58+
content, _, diags := block.Body.PartialContent(&hcl.BodySchema{
59+
Attributes: []hcl.AttributeSchema{
60+
{Name: r.attributeName},
61+
},
62+
})
63+
if diags.HasErrors() {
64+
return diags
65+
}
66+
67+
if attribute, exists := content.Attributes[r.attributeName]; exists {
68+
var members []string
69+
err := runner.EvaluateExpr(attribute.Expr, &members, nil)
6170

62-
return runner.EnsureNoError(err, func() error {
63-
for _, member := range members {
64-
if !isValidIAMMemberFormat(member) {
65-
return runner.EmitIssueOnExpr(
66-
r,
67-
fmt.Sprintf("%s is an invalid member format", member),
68-
attribute.Expr,
69-
)
71+
err = runner.EnsureNoError(err, func() error {
72+
for _, member := range members {
73+
if !isValidIAMMemberFormat(member) {
74+
return runner.EmitIssueOnExpr(
75+
r,
76+
fmt.Sprintf("%s is an invalid member format", member),
77+
attribute.Expr,
78+
)
79+
}
7080
}
81+
return nil
82+
})
83+
if err != nil {
84+
return err
7185
}
72-
return nil
73-
})
86+
}
7487
}
7588

7689
return nil

0 commit comments

Comments
 (0)