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