github.com/terraform-linters/tflint@v0.51.2-0.20240520175844-3750771571b6/plugin/stub-generator/sources/testing/rules/aws_instance_autofix_conflict.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  )
     9  
    10  // AwsInstanceAutofixConflict checks whether ...
    11  type AwsInstanceAutofixConflict struct {
    12  	tflint.DefaultRule
    13  }
    14  
    15  // NewAwsInstanceAutofixConflictRule returns a new rule
    16  func NewAwsInstanceAutofixConflictRule() *AwsInstanceAutofixConflict {
    17  	return &AwsInstanceAutofixConflict{}
    18  }
    19  
    20  // Name returns the rule name
    21  func (r *AwsInstanceAutofixConflict) Name() string {
    22  	return "aws_instance_autofix_conflict"
    23  }
    24  
    25  // Enabled returns whether the rule is enabled by default
    26  func (r *AwsInstanceAutofixConflict) Enabled() bool {
    27  	return true
    28  }
    29  
    30  // Severity returns the rule severity
    31  func (r *AwsInstanceAutofixConflict) Severity() tflint.Severity {
    32  	return tflint.ERROR
    33  }
    34  
    35  // Link returns the rule reference link
    36  func (r *AwsInstanceAutofixConflict) Link() string {
    37  	return ""
    38  }
    39  
    40  // Check checks whether ...
    41  func (r *AwsInstanceAutofixConflict) Check(runner tflint.Runner) error {
    42  	resources, err := runner.GetResourceContent("aws_instance", &hclext.BodySchema{
    43  		Attributes: []hclext.AttributeSchema{{Name: "instance_type"}},
    44  	}, nil)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	for _, resource := range resources.Blocks {
    50  		attribute, exists := resource.Body.Attributes["instance_type"]
    51  		if !exists {
    52  			continue
    53  		}
    54  
    55  		err := runner.EvaluateExpr(attribute.Expr, func(instanceType string) error {
    56  			if instanceType != "[AUTO_FIXED]" {
    57  				return nil
    58  			}
    59  
    60  			return runner.EmitIssueWithFix(
    61  				r,
    62  				fmt.Sprintf("instance type is %s", instanceType),
    63  				attribute.Expr.Range(),
    64  				func(f tflint.Fixer) error {
    65  					// Add a new issue for terraform_autofix_comment rule
    66  					return f.ReplaceText(attribute.Expr.Range(), `"t2.micro" // autofixed`)
    67  				},
    68  			)
    69  		}, nil)
    70  		if err != nil {
    71  			return err
    72  		}
    73  	}
    74  
    75  	return nil
    76  }