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