github.com/wata727/tflint@v0.12.2-0.20191013070026-96dd0d36f385/rules/awsrules/aws_elasticache_cluster_default_parameter_group.go (about)

     1  package awsrules
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"regexp"
     7  
     8  	hcl "github.com/hashicorp/hcl/v2"
     9  	"github.com/wata727/tflint/tflint"
    10  )
    11  
    12  // AwsElastiCacheClusterDefaultParameterGroupRule checks whether the cluster use default parameter group
    13  type AwsElastiCacheClusterDefaultParameterGroupRule struct {
    14  	resourceType  string
    15  	attributeName string
    16  }
    17  
    18  // NewAwsElastiCacheClusterDefaultParameterGroupRule returns new rule with default attributes
    19  func NewAwsElastiCacheClusterDefaultParameterGroupRule() *AwsElastiCacheClusterDefaultParameterGroupRule {
    20  	return &AwsElastiCacheClusterDefaultParameterGroupRule{
    21  		resourceType:  "aws_elasticache_cluster",
    22  		attributeName: "parameter_group_name",
    23  	}
    24  }
    25  
    26  // Name returns the rule name
    27  func (r *AwsElastiCacheClusterDefaultParameterGroupRule) Name() string {
    28  	return "aws_elasticache_cluster_default_parameter_group"
    29  }
    30  
    31  // Enabled returns whether the rule is enabled by default
    32  func (r *AwsElastiCacheClusterDefaultParameterGroupRule) Enabled() bool {
    33  	return true
    34  }
    35  
    36  // Severity returns the rule severity
    37  func (r *AwsElastiCacheClusterDefaultParameterGroupRule) Severity() string {
    38  	return tflint.NOTICE
    39  }
    40  
    41  // Link returns the rule reference link
    42  func (r *AwsElastiCacheClusterDefaultParameterGroupRule) Link() string {
    43  	return tflint.ReferenceLink(r.Name())
    44  }
    45  
    46  var defaultElastiCacheParameterGroupRegexp = regexp.MustCompile("^default")
    47  
    48  // Check checks the parameter group name starts with `default`
    49  func (r *AwsElastiCacheClusterDefaultParameterGroupRule) Check(runner *tflint.Runner) error {
    50  	log.Printf("[TRACE] Check `%s` rule for `%s` runner", r.Name(), runner.TFConfigPath())
    51  
    52  	return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error {
    53  		var parameterGroup string
    54  		err := runner.EvaluateExpr(attribute.Expr, &parameterGroup)
    55  
    56  		return runner.EnsureNoError(err, func() error {
    57  			if defaultElastiCacheParameterGroupRegexp.Match([]byte(parameterGroup)) {
    58  				runner.EmitIssue(
    59  					r,
    60  					fmt.Sprintf("\"%s\" is default parameter group. You cannot edit it.", parameterGroup),
    61  					attribute.Expr.Range(),
    62  				)
    63  			}
    64  			return nil
    65  		})
    66  	})
    67  }