github.com/terraform-linters/tflint-ruleset-azurerm@v0.26.0/rules/apispec/azurerm_virtual_machine_scale_set_invalid_priority.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 // AzurermVirtualMachineScaleSetInvalidPriorityRule checks the pattern is valid 14 type AzurermVirtualMachineScaleSetInvalidPriorityRule struct { 15 tflint.DefaultRule 16 17 resourceType string 18 attributeName string 19 enum []string 20 } 21 22 // NewAzurermVirtualMachineScaleSetInvalidPriorityRule returns new rule with default attributes 23 func NewAzurermVirtualMachineScaleSetInvalidPriorityRule() *AzurermVirtualMachineScaleSetInvalidPriorityRule { 24 return &AzurermVirtualMachineScaleSetInvalidPriorityRule{ 25 resourceType: "azurerm_virtual_machine_scale_set", 26 attributeName: "priority", 27 enum: []string{ 28 "Regular", 29 "Low", 30 "Spot", 31 }, 32 } 33 } 34 35 // Name returns the rule name 36 func (r *AzurermVirtualMachineScaleSetInvalidPriorityRule) Name() string { 37 return "azurerm_virtual_machine_scale_set_invalid_priority" 38 } 39 40 // Enabled returns whether the rule is enabled by default 41 func (r *AzurermVirtualMachineScaleSetInvalidPriorityRule) Enabled() bool { 42 return true 43 } 44 45 // Severity returns the rule severity 46 func (r *AzurermVirtualMachineScaleSetInvalidPriorityRule) Severity() tflint.Severity { 47 return tflint.ERROR 48 } 49 50 // Link returns the rule reference link 51 func (r *AzurermVirtualMachineScaleSetInvalidPriorityRule) Link() string { 52 return project.ReferenceLink(r.Name()) 53 } 54 55 // Check checks the pattern is valid 56 func (r *AzurermVirtualMachineScaleSetInvalidPriorityRule) Check(runner tflint.Runner) error { 57 resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ 58 Attributes: []hclext.AttributeSchema{ 59 {Name: r.attributeName}, 60 }, 61 }, nil) 62 if err != nil { 63 return err 64 } 65 66 for _, resource := range resources.Blocks { 67 attribute, exists := resource.Body.Attributes[r.attributeName] 68 if !exists { 69 continue 70 } 71 err := runner.EvaluateExpr(attribute.Expr, func (val string) error { 72 found := false 73 for _, item := range r.enum { 74 if item == val { 75 found = true 76 } 77 } 78 if !found { 79 runner.EmitIssue( 80 r, 81 fmt.Sprintf(`"%s" is an invalid value as priority`, truncateLongMessage(val)), 82 attribute.Expr.Range(), 83 ) 84 } 85 return nil 86 }, nil) 87 if err != nil { 88 return err 89 } 90 } 91 92 return nil 93 }