github.com/terraform-linters/tflint-ruleset-azurerm@v0.26.0/rules/apispec/azurerm_automation_schedule_invalid_frequency.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 // AzurermAutomationScheduleInvalidFrequencyRule checks the pattern is valid 14 type AzurermAutomationScheduleInvalidFrequencyRule struct { 15 tflint.DefaultRule 16 17 resourceType string 18 attributeName string 19 enum []string 20 } 21 22 // NewAzurermAutomationScheduleInvalidFrequencyRule returns new rule with default attributes 23 func NewAzurermAutomationScheduleInvalidFrequencyRule() *AzurermAutomationScheduleInvalidFrequencyRule { 24 return &AzurermAutomationScheduleInvalidFrequencyRule{ 25 resourceType: "azurerm_automation_schedule", 26 attributeName: "frequency", 27 enum: []string{ 28 "OneTime", 29 "Day", 30 "Hour", 31 "Week", 32 "Month", 33 "Minute", 34 }, 35 } 36 } 37 38 // Name returns the rule name 39 func (r *AzurermAutomationScheduleInvalidFrequencyRule) Name() string { 40 return "azurerm_automation_schedule_invalid_frequency" 41 } 42 43 // Enabled returns whether the rule is enabled by default 44 func (r *AzurermAutomationScheduleInvalidFrequencyRule) Enabled() bool { 45 return true 46 } 47 48 // Severity returns the rule severity 49 func (r *AzurermAutomationScheduleInvalidFrequencyRule) Severity() tflint.Severity { 50 return tflint.ERROR 51 } 52 53 // Link returns the rule reference link 54 func (r *AzurermAutomationScheduleInvalidFrequencyRule) Link() string { 55 return project.ReferenceLink(r.Name()) 56 } 57 58 // Check checks the pattern is valid 59 func (r *AzurermAutomationScheduleInvalidFrequencyRule) 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 frequency`, 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 }