github.com/terraform-linters/tflint-ruleset-azurerm@v0.26.0/rules/azurerm_windows_virtual_machine_scale_set_invalid_sku.go (about)

     1  package rules
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/terraform-linters/tflint-plugin-sdk/hclext"
     7  	"github.com/terraform-linters/tflint-plugin-sdk/tflint"
     8  	"github.com/terraform-linters/tflint-ruleset-azurerm/project"
     9  )
    10  
    11  // AzurermWindowsVirtualMachineScaleSetInvalidSkuRule checks the pattern is valid
    12  type AzurermWindowsVirtualMachineScaleSetInvalidSkuRule struct {
    13  	tflint.DefaultRule
    14  
    15  	resourceType  string
    16  	attributeName string
    17  }
    18  
    19  // NewAzurermWindowsVirtualMachineScaleSetInvalidSkuRule returns new rule with default attributes
    20  func NewAzurermWindowsVirtualMachineScaleSetInvalidSkuRule() *AzurermWindowsVirtualMachineScaleSetInvalidSkuRule {
    21  	return &AzurermWindowsVirtualMachineScaleSetInvalidSkuRule{
    22  		resourceType:  "azurerm_windows_virtual_machine_scale_set",
    23  		attributeName: "sku",
    24  	}
    25  }
    26  
    27  // Name returns the rule name
    28  func (r *AzurermWindowsVirtualMachineScaleSetInvalidSkuRule) Name() string {
    29  	return "azurerm_windows_virtual_machine_scale_set_invalid_sku"
    30  }
    31  
    32  // Enabled returns whether the rule is enabled by default
    33  func (r *AzurermWindowsVirtualMachineScaleSetInvalidSkuRule) Enabled() bool {
    34  	return true
    35  }
    36  
    37  // Severity returns the rule severity
    38  func (r *AzurermWindowsVirtualMachineScaleSetInvalidSkuRule) Severity() tflint.Severity {
    39  	return tflint.ERROR
    40  }
    41  
    42  // Link returns the rule reference link
    43  func (r *AzurermWindowsVirtualMachineScaleSetInvalidSkuRule) Link() string {
    44  	return project.ReferenceLink(r.Name())
    45  }
    46  
    47  // Check checks the pattern is valid
    48  func (r *AzurermWindowsVirtualMachineScaleSetInvalidSkuRule) Check(runner tflint.Runner) error {
    49  	resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{
    50  		Attributes: []hclext.AttributeSchema{{Name: r.attributeName}},
    51  	}, nil)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	for _, resource := range resources.Blocks {
    57  		attribute, exists := resource.Body.Attributes[r.attributeName]
    58  		if !exists {
    59  			continue
    60  		}
    61  
    62  		err := runner.EvaluateExpr(attribute.Expr, func(val string) error {
    63  			found := false
    64  			for _, item := range validMachineSizes {
    65  				if item == val {
    66  					found = true
    67  				}
    68  			}
    69  			if !found {
    70  				runner.EmitIssue(
    71  					r,
    72  					fmt.Sprintf(`"%s" is an invalid value as sku`, val),
    73  					attribute.Expr.Range(),
    74  				)
    75  			}
    76  			return nil
    77  		}, nil)
    78  		if err != nil {
    79  			return err
    80  		}
    81  	}
    82  
    83  	return nil
    84  }