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