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