github.com/jpreese/tflint@v0.19.2-0.20200908152133-b01686250fb6/rules/awsrules/models/aws_iam_policy_invalid_name.go (about) 1 // This file generated by `generator/`. DO NOT EDIT 2 3 package models 4 5 import ( 6 "fmt" 7 "log" 8 "regexp" 9 10 hcl "github.com/hashicorp/hcl/v2" 11 "github.com/terraform-linters/tflint/tflint" 12 ) 13 14 // AwsIAMPolicyInvalidNameRule checks the pattern is valid 15 type AwsIAMPolicyInvalidNameRule struct { 16 resourceType string 17 attributeName string 18 max int 19 min int 20 pattern *regexp.Regexp 21 } 22 23 // NewAwsIAMPolicyInvalidNameRule returns new rule with default attributes 24 func NewAwsIAMPolicyInvalidNameRule() *AwsIAMPolicyInvalidNameRule { 25 return &AwsIAMPolicyInvalidNameRule{ 26 resourceType: "aws_iam_policy", 27 attributeName: "name", 28 max: 128, 29 min: 1, 30 pattern: regexp.MustCompile(`^[\w+=,.@-]+$`), 31 } 32 } 33 34 // Name returns the rule name 35 func (r *AwsIAMPolicyInvalidNameRule) Name() string { 36 return "aws_iam_policy_invalid_name" 37 } 38 39 // Enabled returns whether the rule is enabled by default 40 func (r *AwsIAMPolicyInvalidNameRule) Enabled() bool { 41 return true 42 } 43 44 // Severity returns the rule severity 45 func (r *AwsIAMPolicyInvalidNameRule) Severity() string { 46 return tflint.ERROR 47 } 48 49 // Link returns the rule reference link 50 func (r *AwsIAMPolicyInvalidNameRule) Link() string { 51 return "" 52 } 53 54 // Check checks the pattern is valid 55 func (r *AwsIAMPolicyInvalidNameRule) Check(runner *tflint.Runner) error { 56 log.Printf("[TRACE] Check `%s` rule for `%s` runner", r.Name(), runner.TFConfigPath()) 57 58 return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { 59 var val string 60 err := runner.EvaluateExpr(attribute.Expr, &val) 61 62 return runner.EnsureNoError(err, func() error { 63 if len(val) > r.max { 64 runner.EmitIssue( 65 r, 66 "name must be 128 characters or less", 67 attribute.Expr.Range(), 68 ) 69 } 70 if len(val) < r.min { 71 runner.EmitIssue( 72 r, 73 "name must be 1 characters or higher", 74 attribute.Expr.Range(), 75 ) 76 } 77 if !r.pattern.MatchString(val) { 78 runner.EmitIssue( 79 r, 80 fmt.Sprintf(`"%s" does not match valid pattern %s`, truncateLongMessage(val), `^[\w+=,.@-]+$`), 81 attribute.Expr.Range(), 82 ) 83 } 84 return nil 85 }) 86 }) 87 }