github.com/terraform-linters/tflint@v0.51.2-0.20240520175844-3750771571b6/plugin/stub-generator/sources/testing/rules/aws_iam_role_example.go (about)

     1  package rules
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/terraform-linters/tflint-plugin-sdk/hclext"
     7  	"github.com/terraform-linters/tflint-plugin-sdk/tflint"
     8  )
     9  
    10  // AwsIAMRoleExampleRule checks whether ...
    11  type AwsIAMRoleExampleRule struct {
    12  	tflint.DefaultRule
    13  }
    14  
    15  // NewAwsIAMPolicyExampleRule returns a new rule
    16  func NewAwsIAMRoleExampleRule() *AwsIAMRoleExampleRule {
    17  	return &AwsIAMRoleExampleRule{}
    18  }
    19  
    20  // Name returns the rule name
    21  func (r *AwsIAMRoleExampleRule) Name() string {
    22  	return "aws_iam_role_example"
    23  }
    24  
    25  // Enabled returns whether the rule is enabled by default
    26  func (r *AwsIAMRoleExampleRule) Enabled() bool {
    27  	return true
    28  }
    29  
    30  // Severity returns the rule severity
    31  func (r *AwsIAMRoleExampleRule) Severity() tflint.Severity {
    32  	return tflint.ERROR
    33  }
    34  
    35  // Link returns the rule reference link
    36  func (r *AwsIAMRoleExampleRule) Link() string {
    37  	return ""
    38  }
    39  
    40  // Check checks whether ...
    41  func (r *AwsIAMRoleExampleRule) Check(runner tflint.Runner) error {
    42  	resources, err := runner.GetResourceContent("aws_iam_role", &hclext.BodySchema{
    43  		Blocks: []hclext.BlockSchema{
    44  			{
    45  				Type: "inline_policy",
    46  				Body: &hclext.BodySchema{
    47  					Attributes: []hclext.AttributeSchema{{Name: "name"}},
    48  				},
    49  			},
    50  		},
    51  	}, &tflint.GetModuleContentOption{
    52  		ModuleCtx:  tflint.SelfModuleCtxType,
    53  		ExpandMode: tflint.ExpandModeNone,
    54  	})
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	for _, resource := range resources.Blocks {
    60  		for _, policy := range resource.Body.Blocks {
    61  			if err := runner.EmitIssue(r, "inline policy found", policy.DefRange); err != nil {
    62  				return err
    63  			}
    64  
    65  			attribute, exists := policy.Body.Attributes["name"]
    66  			if !exists {
    67  				continue
    68  			}
    69  
    70  			err := runner.EvaluateExpr(attribute.Expr, func(name string) error {
    71  				return runner.EmitIssue(
    72  					r,
    73  					fmt.Sprintf("name is %s", name),
    74  					attribute.Expr.Range(),
    75  				)
    76  			}, nil)
    77  			if err != nil {
    78  				return err
    79  			}
    80  		}
    81  	}
    82  
    83  	return nil
    84  }