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