github.com/wata727/tflint@v0.12.2-0.20191013070026-96dd0d36f385/rules/awsrules/api/rule.go.tmpl (about)

     1  // This file generated by `tools/api-rule-gen/main.go`. DO NOT EDIT
     2  
     3  package api
     4  
     5  import (
     6  	"fmt"
     7  	"log"
     8  
     9  	hcl "github.com/hashicorp/hcl/v2"
    10  	"github.com/wata727/tflint/tflint"
    11  )
    12  
    13  // {{ .RuleNameCC }}Rule checks whether attribute value actually exists
    14  type {{ .RuleNameCC }}Rule struct {
    15  	resourceType  string
    16  	attributeName string
    17  	data          map[string]bool
    18  	dataPrepared  bool
    19  }
    20  
    21  // New{{ .RuleNameCC }}Rule returns new rule with default attributes
    22  func New{{ .RuleNameCC }}Rule() *{{ .RuleNameCC }}Rule {
    23  	return &{{ .RuleNameCC }}Rule{
    24  		resourceType:  "{{ .ResourceType }}",
    25  		attributeName: "{{ .AttributeName }}",
    26  		data:          map[string]bool{},
    27  		dataPrepared:  false,
    28  	}
    29  }
    30  
    31  // Name returns the rule name
    32  func (r *{{ .RuleNameCC }}Rule) Name() string {
    33  	return "{{ .RuleName }}"
    34  }
    35  
    36  // Enabled returns whether the rule is enabled by default
    37  func (r *{{ .RuleNameCC }}Rule) Enabled() bool {
    38  	return true
    39  }
    40  
    41  // Severity returns the rule severity
    42  func (r *{{ .RuleNameCC }}Rule) Severity() string {
    43  	return tflint.ERROR
    44  }
    45  
    46  // Link returns the rule reference link
    47  func (r *{{ .RuleNameCC }}Rule) Link() string {
    48  	return ""
    49  }
    50  
    51  // Check checks whether the attributes are included in the list retrieved by {{ .ActionName }}
    52  func (r *{{ .RuleNameCC }}Rule) Check(runner *tflint.Runner) error {
    53  	log.Printf("[TRACE] Check `%s` rule for `%s` runner", r.Name(), runner.TFConfigPath())
    54  
    55  	return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error {
    56  		if !r.dataPrepared {
    57  			log.Print("[DEBUG] invoking {{ .ActionName }}")
    58  			var err error
    59  			r.data, err = runner.AwsClient.{{ .ActionName }}()
    60  			if err != nil {
    61  				err := &tflint.Error{
    62  					Code:    tflint.ExternalAPIError,
    63  					Level:   tflint.ErrorLevel,
    64  					Message: "An error occurred while invoking {{ .ActionName }}",
    65  					Cause:   err,
    66  				}
    67  				log.Printf("[ERROR] %s", err)
    68  				return err
    69  			}
    70  			r.dataPrepared = true
    71  		}
    72  
    73  {{- if eq .DataType "list" }}
    74  
    75  		return runner.EachStringSliceExprs(attribute.Expr, func(val string, expr hcl.Expression) {
    76  			if !r.data[val] {
    77  				runner.EmitIssue(
    78  					r,
    79  					fmt.Sprintf(`{{ .Template }}`, val),
    80  					expr.Range(),
    81  				)
    82  			}
    83  		})
    84  {{- else }}
    85  
    86  		var val string
    87  		err := runner.EvaluateExpr(attribute.Expr, &val)
    88  
    89  		return runner.EnsureNoError(err, func() error {
    90  			if !r.data[val] {
    91  				runner.EmitIssue(
    92  					r,
    93  					fmt.Sprintf(`{{ .Template }}`, val),
    94  					attribute.Expr.Range(),
    95  				)
    96  			}
    97  			return nil
    98  		})
    99  {{- end }}
   100  	})
   101  }