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

     1  package awsrules
     2  
     3  import (
     4  	"log"
     5  
     6  	hcl "github.com/hashicorp/hcl/v2"
     7  	"github.com/wata727/tflint/tflint"
     8  )
     9  
    10  // {{ .RuleNameCC }}Rule checks ...
    11  type {{ .RuleNameCC }}Rule struct {
    12  	resourceType  string
    13  	attributeName string
    14  	// Add more field
    15  	dataPrepared bool
    16  }
    17  
    18  // New{{ .RuleNameCC }}Rule returns new rule with default attributes
    19  func New{{ .RuleNameCC }}Rule() *{{ .RuleNameCC }}Rule {
    20  	return &{{ .RuleNameCC }}Rule{
    21  		resourceType:  "...",
    22  		attributeName: "...",
    23  		dataPrepared:  false,
    24  	}
    25  }
    26  
    27  // Name returns the rule name
    28  func (r *{{ .RuleNameCC }}Rule) Name() string {
    29  	return "{{ .RuleName }}"
    30  }
    31  
    32  // Enabled returns whether the rule is enabled by default
    33  func (r *{{ .RuleNameCC }}Rule) Enabled() bool {
    34  	return true
    35  }
    36  
    37  // Severity returns the rule severity
    38  func (r *{{ .RuleNameCC }}Rule) Severity() string {
    39  	return tflint.ERROR
    40  }
    41  
    42  // Link returns the rule reference link
    43  func (r *{{ .RuleNameCC }}Rule) Link() string {
    44  	return ""
    45  }
    46  
    47  // Check checks ...
    48  func (r *{{ .RuleNameCC }}Rule) Check(runner *tflint.Runner) error {
    49  	log.Printf("[INFO] Check `%s` rule for `%s` runner", r.Name(), runner.TFConfigPath())
    50  
    51  	return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error {
    52  		if !r.dataPrepared {
    53  			log.Print("[DEBUG] Fetch ...")
    54  			resp, err := runner.AwsClient.Service.CallAPI()
    55  			if err != nil {
    56  				err := &tflint.Error{
    57  					Code:    tflint.ExternalAPIError,
    58  					Level:   tflint.ErrorLevel,
    59  					Message: "An error occurred while describing ...",
    60  					Cause:   err,
    61  				}
    62  				log.Printf("[ERROR] %s", err)
    63  				return err
    64  			}
    65  			r.Body = resp.Body
    66  
    67  			r.dataPrepared = true
    68  		}
    69  
    70  		var val string
    71  		err := runner.EvaluateExpr(attribute.Expr, &val)
    72  
    73  		return runner.EnsureNoError(err, func() error {
    74  			if isInvalid(val) {
    75  				runner.EmitIssue(
    76  					r,
    77  					"TODO",
    78  					attribute.Expr.Range(),
    79  				)
    80  			}
    81  			return nil
    82  		})
    83  	})
    84  }