github.com/terraform-linters/tflint-ruleset-azurerm@v0.26.0/rules/apispec/azurerm_eventhub_namespace_invalid_capacity.go (about)

     1  // This file generated by `tools/apispec-rule-gen/main.go`. DO NOT EDIT
     2  
     3  package apispec
     4  
     5  import (
     6  
     7  	"github.com/terraform-linters/tflint-plugin-sdk/hclext"
     8  	"github.com/terraform-linters/tflint-plugin-sdk/tflint"
     9  	"github.com/terraform-linters/tflint-ruleset-azurerm/project"
    10  )
    11  
    12  // AzurermEventhubNamespaceInvalidCapacityRule checks the pattern is valid
    13  type AzurermEventhubNamespaceInvalidCapacityRule struct {
    14  	tflint.DefaultRule
    15  
    16  	resourceType  string
    17  	attributeName string
    18  	min           int
    19  }
    20  
    21  // NewAzurermEventhubNamespaceInvalidCapacityRule returns new rule with default attributes
    22  func NewAzurermEventhubNamespaceInvalidCapacityRule() *AzurermEventhubNamespaceInvalidCapacityRule {
    23  	return &AzurermEventhubNamespaceInvalidCapacityRule{
    24  		resourceType:  "azurerm_eventhub_namespace",
    25  		attributeName: "capacity",
    26  		min:           0,
    27  	}
    28  }
    29  
    30  // Name returns the rule name
    31  func (r *AzurermEventhubNamespaceInvalidCapacityRule) Name() string {
    32  	return "azurerm_eventhub_namespace_invalid_capacity"
    33  }
    34  
    35  // Enabled returns whether the rule is enabled by default
    36  func (r *AzurermEventhubNamespaceInvalidCapacityRule) Enabled() bool {
    37  	return true
    38  }
    39  
    40  // Severity returns the rule severity
    41  func (r *AzurermEventhubNamespaceInvalidCapacityRule) Severity() tflint.Severity {
    42  	return tflint.ERROR
    43  }
    44  
    45  // Link returns the rule reference link
    46  func (r *AzurermEventhubNamespaceInvalidCapacityRule) Link() string {
    47  	return project.ReferenceLink(r.Name())
    48  }
    49  
    50  // Check checks the pattern is valid
    51  func (r *AzurermEventhubNamespaceInvalidCapacityRule) Check(runner tflint.Runner) error {
    52  	resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{
    53  		Attributes: []hclext.AttributeSchema{
    54  			{Name: r.attributeName},
    55  		},
    56  	}, nil)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	for _, resource := range resources.Blocks {
    62  		attribute, exists := resource.Body.Attributes[r.attributeName]
    63  		if !exists {
    64  			continue
    65  		}
    66  		err := runner.EvaluateExpr(attribute.Expr, func (val int) error {
    67  			if val < r.min {
    68  				runner.EmitIssue(
    69  					r,
    70  					"capacity must be 0 or higher",
    71  					attribute.Expr.Range(),
    72  				)
    73  			}
    74  			return nil
    75  		}, nil)
    76  		if err != nil {
    77  			return err
    78  		}
    79  	}
    80  
    81  	return nil
    82  }