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