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