github.com/Azure/tflint-ruleset-azurerm-ext@v0.6.0/rules/azurerm_resource_tag.go (about) 1 package rules 2 3 import ( 4 "fmt" 5 "github.com/terraform-linters/tflint-plugin-sdk/logger" 6 7 "github.com/Azure/tflint-ruleset-azurerm-ext/project" 8 "github.com/hashicorp/go-multierror" 9 "github.com/hashicorp/hcl/v2" 10 "github.com/hashicorp/hcl/v2/hclsyntax" 11 "github.com/lonegunmanb/terraform-azurerm-schema/v3/generated" 12 "github.com/terraform-linters/tflint-plugin-sdk/tflint" 13 ) 14 15 var _ tflint.Rule = new(AzurermResourceTagRule) 16 17 // AzurermResourceTagRule checks whether the tags arg is specified if supported 18 type AzurermResourceTagRule struct { 19 tflint.DefaultRule 20 } 21 22 func (r *AzurermResourceTagRule) Name() string { 23 return "azurerm_resource_tag" 24 } 25 26 func (r *AzurermResourceTagRule) Enabled() bool { 27 return false 28 } 29 30 func (r *AzurermResourceTagRule) Severity() tflint.Severity { 31 return tflint.NOTICE 32 } 33 34 func (r *AzurermResourceTagRule) Link() string { 35 return project.ReferenceLink(r.Name()) 36 } 37 38 func (r *AzurermResourceTagRule) Check(runner tflint.Runner) error { 39 return Check(runner, r.CheckFile) 40 } 41 42 // NewAzurermResourceTagRule returns a new rule 43 func NewAzurermResourceTagRule() *AzurermResourceTagRule { 44 return &AzurermResourceTagRule{} 45 } 46 47 // CheckFile checks whether the tags arg is specified if supported 48 func (r *AzurermResourceTagRule) CheckFile(runner tflint.Runner, file *hcl.File) error { 49 body, ok := file.Body.(*hclsyntax.Body) 50 if !ok { 51 logger.Debug("skip azurerm_resource_tag since it's not hcl file") 52 return nil 53 } 54 blocks := body.Blocks 55 var err error 56 for _, block := range blocks { 57 var subErr error 58 switch block.Type { 59 case "resource": 60 subErr = r.visitAzResource(runner, block) 61 } 62 if subErr != nil { 63 err = multierror.Append(err, subErr) 64 } 65 } 66 return err 67 } 68 69 func (r *AzurermResourceTagRule) visitAzResource(runner tflint.Runner, azBlock *hclsyntax.Block) error { 70 resourceSchema, isAzureResource := generated.Resources[azBlock.Labels[0]] 71 if !isAzureResource { 72 return nil 73 } 74 _, isTagSupported := resourceSchema.Block.Attributes["tags"] 75 _, isTagSet := azBlock.Body.Attributes["tags"] 76 if isTagSupported && !isTagSet { 77 return runner.EmitIssue( 78 r, 79 fmt.Sprintf("`tags` argument is not set but supported in resource `%s`", azBlock.Labels[0]), 80 azBlock.DefRange(), 81 ) 82 } 83 return nil 84 }