github.com/jpreese/tflint@v0.19.2-0.20200908152133-b01686250fb6/rules/awsrules/models/aws_efs_mount_target_invalid_ip_address.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 // AwsEfsMountTargetInvalidIPAddressRule checks the pattern is valid 15 type AwsEfsMountTargetInvalidIPAddressRule struct { 16 resourceType string 17 attributeName string 18 max int 19 min int 20 pattern *regexp.Regexp 21 } 22 23 // NewAwsEfsMountTargetInvalidIPAddressRule returns new rule with default attributes 24 func NewAwsEfsMountTargetInvalidIPAddressRule() *AwsEfsMountTargetInvalidIPAddressRule { 25 return &AwsEfsMountTargetInvalidIPAddressRule{ 26 resourceType: "aws_efs_mount_target", 27 attributeName: "ip_address", 28 max: 15, 29 min: 7, 30 pattern: regexp.MustCompile(`^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$`), 31 } 32 } 33 34 // Name returns the rule name 35 func (r *AwsEfsMountTargetInvalidIPAddressRule) Name() string { 36 return "aws_efs_mount_target_invalid_ip_address" 37 } 38 39 // Enabled returns whether the rule is enabled by default 40 func (r *AwsEfsMountTargetInvalidIPAddressRule) Enabled() bool { 41 return true 42 } 43 44 // Severity returns the rule severity 45 func (r *AwsEfsMountTargetInvalidIPAddressRule) Severity() string { 46 return tflint.ERROR 47 } 48 49 // Link returns the rule reference link 50 func (r *AwsEfsMountTargetInvalidIPAddressRule) Link() string { 51 return "" 52 } 53 54 // Check checks the pattern is valid 55 func (r *AwsEfsMountTargetInvalidIPAddressRule) 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 "ip_address must be 15 characters or less", 67 attribute.Expr.Range(), 68 ) 69 } 70 if len(val) < r.min { 71 runner.EmitIssue( 72 r, 73 "ip_address must be 7 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), `^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$`), 81 attribute.Expr.Range(), 82 ) 83 } 84 return nil 85 }) 86 }) 87 }