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