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