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