github.com/jpreese/tflint@v0.19.2-0.20200908152133-b01686250fb6/rules/awsrules/models/pattern_rule.go.tmpl (about) 1 // This file generated by `generator/`. DO NOT EDIT 2 3 package models 4 5 import ( 6 {{- if and (or (ne .Pattern "") (ne (len .Enum) 0)) (ne .Sensitive true) }} 7 "fmt" 8 {{- end }} 9 "log" 10 {{- if ne .Pattern "" }} 11 "regexp" 12 {{- end }} 13 14 hcl "github.com/hashicorp/hcl/v2" 15 "github.com/terraform-linters/tflint/tflint" 16 ) 17 18 // {{ .RuleNameCC }}Rule checks the pattern is valid 19 type {{ .RuleNameCC }}Rule struct { 20 resourceType string 21 attributeName string 22 {{- if ne .Max 0 }} 23 max int 24 {{- end }} 25 {{- if ne .Min 0 }} 26 min int 27 {{- end }} 28 {{- if ne .Pattern "" }} 29 pattern *regexp.Regexp 30 {{- end }} 31 {{- if ne (len .Enum) 0 }} 32 enum []string 33 {{- end }} 34 } 35 36 // New{{ .RuleNameCC }}Rule returns new rule with default attributes 37 func New{{ .RuleNameCC }}Rule() *{{ .RuleNameCC }}Rule { 38 return &{{ .RuleNameCC }}Rule{ 39 resourceType: "{{ .ResourceType }}", 40 attributeName: "{{ .AttributeName }}", 41 {{- if ne .Max 0 }} 42 max: {{ .Max }}, 43 {{- end }} 44 {{- if ne .Min 0 }} 45 min: {{ .Min }}, 46 {{- end }} 47 {{- if ne .Pattern "" }} 48 pattern: regexp.MustCompile(`{{ .Pattern }}`), 49 {{- end }} 50 {{- if ne (len .Enum) 0 }} 51 enum: []string{ 52 {{- range $v := .Enum }} 53 "{{ $v }}", 54 {{- end }} 55 }, 56 {{- end }} 57 } 58 } 59 60 // Name returns the rule name 61 func (r *{{ .RuleNameCC }}Rule) Name() string { 62 return "{{ .RuleName }}" 63 } 64 65 // Enabled returns whether the rule is enabled by default 66 func (r *{{ .RuleNameCC }}Rule) Enabled() bool { 67 return true 68 } 69 70 // Severity returns the rule severity 71 func (r *{{ .RuleNameCC }}Rule) Severity() string { 72 return tflint.ERROR 73 } 74 75 // Link returns the rule reference link 76 func (r *{{ .RuleNameCC }}Rule) Link() string { 77 return "" 78 } 79 80 // Check checks the pattern is valid 81 func (r *{{ .RuleNameCC }}Rule) Check(runner *tflint.Runner) error { 82 log.Printf("[TRACE] Check `%s` rule for `%s` runner", r.Name(), runner.TFConfigPath()) 83 84 return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { 85 var val string 86 err := runner.EvaluateExpr(attribute.Expr, &val) 87 88 return runner.EnsureNoError(err, func() error { 89 {{- if ne .Max 0 }} 90 if len(val) > r.max { 91 runner.EmitIssue( 92 r, 93 "{{ .AttributeName }} must be {{ .Max }} characters or less", 94 attribute.Expr.Range(), 95 ) 96 } 97 {{- end }} 98 99 {{- if ne .Min 0 }} 100 if len(val) < r.min { 101 runner.EmitIssue( 102 r, 103 "{{ .AttributeName }} must be {{ .Min }} characters or higher", 104 attribute.Expr.Range(), 105 ) 106 } 107 {{- end }} 108 109 {{- if ne .Pattern "" }} 110 if !r.pattern.MatchString(val) { 111 runner.EmitIssue( 112 r, 113 {{- if .Sensitive }} 114 `{{ .AttributeName }} does not match valid pattern {{ .Pattern }}`, 115 {{- else }} 116 fmt.Sprintf(`"%s" does not match valid pattern %s`, truncateLongMessage(val), `{{ .Pattern }}`), 117 {{- end }} 118 attribute.Expr.Range(), 119 ) 120 } 121 {{- end }} 122 123 {{- if ne (len .Enum) 0 }} 124 found := false 125 for _, item := range r.enum { 126 if item == val { 127 found = true 128 } 129 } 130 if !found { 131 runner.EmitIssue( 132 r, 133 {{- if .Sensitive }} 134 `{{ .AttributeName }} is an invalid value`, 135 {{- else }} 136 fmt.Sprintf(`"%s" is an invalid value as {{ .AttributeName }}`, truncateLongMessage(val)), 137 {{- end }} 138 attribute.Expr.Range(), 139 ) 140 } 141 {{- end }} 142 return nil 143 }) 144 }) 145 }