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