github.com/terraform-linters/tflint@v0.51.2-0.20240520175844-3750771571b6/plugin/stub-generator/sources/example/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/terraform-linters/tflint-plugin-sdk/hclext" 7 "github.com/terraform-linters/tflint-plugin-sdk/plugin" 8 "github.com/terraform-linters/tflint-plugin-sdk/tflint" 9 ) 10 11 func main() { 12 plugin.Serve(&plugin.ServeOpts{ 13 RuleSet: &tflint.BuiltinRuleSet{ 14 Name: "example", 15 Version: "0.1.0", 16 Rules: []tflint.Rule{ 17 NewAwsInstanceExampleTypeRule(), 18 }, 19 }, 20 }) 21 } 22 23 // AwsInstanceExampleTypeRule checks whether ... 24 type AwsInstanceExampleTypeRule struct { 25 tflint.DefaultRule 26 } 27 28 // NewAwsInstanceExampleTypeRule returns a new rule 29 func NewAwsInstanceExampleTypeRule() *AwsInstanceExampleTypeRule { 30 return &AwsInstanceExampleTypeRule{} 31 } 32 33 // Name returns the rule name 34 func (r *AwsInstanceExampleTypeRule) Name() string { 35 return "aws_instance_example_type" 36 } 37 38 // Enabled returns whether the rule is enabled by default 39 func (r *AwsInstanceExampleTypeRule) Enabled() bool { 40 return true 41 } 42 43 // Severity returns the rule severity 44 func (r *AwsInstanceExampleTypeRule) Severity() tflint.Severity { 45 return tflint.ERROR 46 } 47 48 // Link returns the rule reference link 49 func (r *AwsInstanceExampleTypeRule) Link() string { 50 return "" 51 } 52 53 // Check checks whether ... 54 func (r *AwsInstanceExampleTypeRule) Check(runner tflint.Runner) error { 55 resources, err := runner.GetResourceContent("aws_instance", &hclext.BodySchema{ 56 Attributes: []hclext.AttributeSchema{{Name: "instance_type"}}, 57 }, nil) 58 if err != nil { 59 return err 60 } 61 62 for _, resource := range resources.Blocks { 63 attribute, exists := resource.Body.Attributes["instance_type"] 64 if !exists { 65 continue 66 } 67 68 err := runner.EvaluateExpr(attribute.Expr, func(instanceType string) error { 69 return runner.EmitIssue( 70 r, 71 fmt.Sprintf("instance type is %s", instanceType), 72 attribute.Expr.Range(), 73 ) 74 }, nil) 75 if err != nil { 76 return err 77 } 78 } 79 80 return nil 81 }