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