github.com/terraform-linters/tflint-ruleset-azurerm@v0.26.0/rules/apispec/azurerm_iothub_route_invalid_source.go (about) 1 // This file generated by `tools/apispec-rule-gen/main.go`. DO NOT EDIT 2 3 package apispec 4 5 import ( 6 "fmt" 7 8 "github.com/terraform-linters/tflint-plugin-sdk/hclext" 9 "github.com/terraform-linters/tflint-plugin-sdk/tflint" 10 "github.com/terraform-linters/tflint-ruleset-azurerm/project" 11 ) 12 13 // AzurermIothubRouteInvalidSourceRule checks the pattern is valid 14 type AzurermIothubRouteInvalidSourceRule struct { 15 tflint.DefaultRule 16 17 resourceType string 18 attributeName string 19 enum []string 20 } 21 22 // NewAzurermIothubRouteInvalidSourceRule returns new rule with default attributes 23 func NewAzurermIothubRouteInvalidSourceRule() *AzurermIothubRouteInvalidSourceRule { 24 return &AzurermIothubRouteInvalidSourceRule{ 25 resourceType: "azurerm_iothub_route", 26 attributeName: "source", 27 enum: []string{ 28 "Invalid", 29 "DeviceMessages", 30 "TwinChangeEvents", 31 "DeviceLifecycleEvents", 32 "DeviceJobLifecycleEvents", 33 "DigitalTwinChangeEvents", 34 "DeviceConnectionStateEvents", 35 "MqttBrokerMessages", 36 }, 37 } 38 } 39 40 // Name returns the rule name 41 func (r *AzurermIothubRouteInvalidSourceRule) Name() string { 42 return "azurerm_iothub_route_invalid_source" 43 } 44 45 // Enabled returns whether the rule is enabled by default 46 func (r *AzurermIothubRouteInvalidSourceRule) Enabled() bool { 47 return true 48 } 49 50 // Severity returns the rule severity 51 func (r *AzurermIothubRouteInvalidSourceRule) Severity() tflint.Severity { 52 return tflint.ERROR 53 } 54 55 // Link returns the rule reference link 56 func (r *AzurermIothubRouteInvalidSourceRule) Link() string { 57 return project.ReferenceLink(r.Name()) 58 } 59 60 // Check checks the pattern is valid 61 func (r *AzurermIothubRouteInvalidSourceRule) Check(runner tflint.Runner) error { 62 resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ 63 Attributes: []hclext.AttributeSchema{ 64 {Name: r.attributeName}, 65 }, 66 }, nil) 67 if err != nil { 68 return err 69 } 70 71 for _, resource := range resources.Blocks { 72 attribute, exists := resource.Body.Attributes[r.attributeName] 73 if !exists { 74 continue 75 } 76 err := runner.EvaluateExpr(attribute.Expr, func (val string) error { 77 found := false 78 for _, item := range r.enum { 79 if item == val { 80 found = true 81 } 82 } 83 if !found { 84 runner.EmitIssue( 85 r, 86 fmt.Sprintf(`"%s" is an invalid value as source`, truncateLongMessage(val)), 87 attribute.Expr.Range(), 88 ) 89 } 90 return nil 91 }, nil) 92 if err != nil { 93 return err 94 } 95 } 96 97 return nil 98 }