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