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