github.com/terraform-linters/tflint-ruleset-azurerm@v0.26.0/rules/rule.go.tmpl (about)

     1  package rules
     2  
     3  import (
     4  	"github.com/terraform-linters/tflint-plugin-sdk/hclext"
     5  	"github.com/terraform-linters/tflint-plugin-sdk/tflint"
     6  	"github.com/terraform-linters/tflint-ruleset-azurerm/project"
     7  )
     8  
     9  // TODO: Write the rule's description here
    10  // {{ .RuleNameCC }}Rule checks ...
    11  type {{ .RuleNameCC }}Rule struct {
    12  	tflint.DefaultRule
    13  
    14  	resourceType  string
    15  	attributeName string
    16  }
    17  
    18  // New{{ .RuleNameCC }}Rule returns new rule with default attributes
    19  func New{{ .RuleNameCC }}Rule() *{{ .RuleNameCC }}Rule {
    20  	return &{{ .RuleNameCC }}Rule{
    21  		// TODO: Write resource type and attribute name here
    22  		resourceType:  "...",
    23  		attributeName: "...",
    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  	// TODO: Determine whether the rule is enabled by default
    35  	return true
    36  }
    37  
    38  // Severity returns the rule severity
    39  func (r *{{ .RuleNameCC }}Rule) Severity() tflint.Severity {
    40  	// TODO: Determine the rule's severiry
    41  	return tflint.ERROR
    42  }
    43  
    44  // Link returns the rule reference link
    45  func (r *{{ .RuleNameCC }}Rule) Link() string {
    46  	// TODO: If the rule is so trivial that no documentation is needed, return "" instead.
    47  	return project.ReferenceLink(r.Name())
    48  }
    49  
    50  // TODO: Write the details of the inspection
    51  // Check checks ...
    52  func (r *{{ .RuleNameCC }}Rule) Check(runner tflint.Runner) error {
    53  	// TODO: Write the implementation here. See this documentation for what tflint.Runner can do.
    54  	//       https://pkg.go.dev/github.com/terraform-linters/tflint-plugin-sdk/tflint#Runner
    55  
    56  	resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{
    57  		Attributes: []hclext.AttributeSchema{
    58  			{Name: r.attributeName},
    59  		},
    60  	}, nil)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	for _, resource := range resources.Blocks {
    66  		attribute, exists := resource.Body.Attributes[r.attributeName]
    67  		if !exists {
    68  			continue
    69  		}
    70  
    71  		err := runner.EvaluateExpr(attribute.Expr, func (val string) error {
    72  			if val == "" {
    73  				runner.EmitIssue(
    74  					r,
    75  					"TODO",
    76  					attribute.Expr.Range(),
    77  				)
    78  			}
    79  			return nil
    80  		}, nil)
    81  		if err != nil {
    82  			return err
    83  		}
    84  	}
    85  
    86  	return nil
    87  }