github.com/terraform-linters/tflint-ruleset-azurerm@v0.26.0/rules/apispec/azurerm_batch_pool_invalid_name.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 "regexp" 8 9 "github.com/terraform-linters/tflint-plugin-sdk/hclext" 10 "github.com/terraform-linters/tflint-plugin-sdk/tflint" 11 "github.com/terraform-linters/tflint-ruleset-azurerm/project" 12 ) 13 14 // AzurermBatchPoolInvalidNameRule checks the pattern is valid 15 type AzurermBatchPoolInvalidNameRule struct { 16 tflint.DefaultRule 17 18 resourceType string 19 attributeName string 20 pattern *regexp.Regexp 21 } 22 23 // NewAzurermBatchPoolInvalidNameRule returns new rule with default attributes 24 func NewAzurermBatchPoolInvalidNameRule() *AzurermBatchPoolInvalidNameRule { 25 return &AzurermBatchPoolInvalidNameRule{ 26 resourceType: "azurerm_batch_pool", 27 attributeName: "name", 28 pattern: regexp.MustCompile(`^[a-zA-Z0-9_-]+$`), 29 } 30 } 31 32 // Name returns the rule name 33 func (r *AzurermBatchPoolInvalidNameRule) Name() string { 34 return "azurerm_batch_pool_invalid_name" 35 } 36 37 // Enabled returns whether the rule is enabled by default 38 func (r *AzurermBatchPoolInvalidNameRule) Enabled() bool { 39 return true 40 } 41 42 // Severity returns the rule severity 43 func (r *AzurermBatchPoolInvalidNameRule) Severity() tflint.Severity { 44 return tflint.ERROR 45 } 46 47 // Link returns the rule reference link 48 func (r *AzurermBatchPoolInvalidNameRule) Link() string { 49 return project.ReferenceLink(r.Name()) 50 } 51 52 // Check checks the pattern is valid 53 func (r *AzurermBatchPoolInvalidNameRule) Check(runner tflint.Runner) error { 54 resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ 55 Attributes: []hclext.AttributeSchema{ 56 {Name: r.attributeName}, 57 }, 58 }, nil) 59 if err != nil { 60 return err 61 } 62 63 for _, resource := range resources.Blocks { 64 attribute, exists := resource.Body.Attributes[r.attributeName] 65 if !exists { 66 continue 67 } 68 err := runner.EvaluateExpr(attribute.Expr, func (val string) error { 69 if !r.pattern.MatchString(val) { 70 runner.EmitIssue( 71 r, 72 fmt.Sprintf(`"%s" does not match valid pattern %s`, truncateLongMessage(val), `^[a-zA-Z0-9_-]+$`), 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 }