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