generated from terraform-linters/tflint-ruleset-template
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aws_elasticache_replication_group (#143)
* aws_elasticache_replication_group - fixes #140 Add tests fir aws_elasticache_replication_group # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Mon Jul 19 15:34:06 2021 -0400 # # On branch aws_elasticache_replication_group # Changes to be committed: # modified: docs/rules/aws_elasticache_cluster_default_parameter_group.md # new file: docs/rules/aws_elasticache_replication_group_default_parameter_group.md # new file: rules/aws_elasticache_replication_group_default_parameter_group.go # new file: rules/aws_elasticache_replication_group_default_parameter_group_test.go # new file: rules/aws_elasticache_replication_group_invalid_type.go # new file: rules/aws_elasticache_replication_group_invalid_type_test.go # * aws_elasticache_replication_group - Fix variable, and registered to provider * aws_elasticache_replication_group - Add to readme * aws_elasticache_replication_group - Add docs for rule and register with provider * aws_elasticache_replication_group - Add missing docs * aws_elasticache_replication_group - Add previous type rule for replication group Co-authored-by: James Ray <jray@five-startech.com>
- Loading branch information
Showing
14 changed files
with
622 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# aws_elasticache_cluster_invalid_type | ||
|
||
Disallow using invalid type. | ||
|
||
## Example | ||
|
||
```hcl | ||
resource "aws_elasticache_cluster" "default" { | ||
node_type = "cache.t3.mini" // invalid type! | ||
engine_version = "6.x" | ||
maintenance_window = "thu:02:30-thu:03:30" | ||
num_cache_nodes = 1 | ||
apply_immediately = false | ||
parameter_group_name = "custom.redis6.x.cluster.on" | ||
port = 6379 | ||
cluster_id = "cluster_id" | ||
snapshot_retention_limit = 1 | ||
subnet_group_name = aws_elasticache_subnet_group.private.name | ||
security_group_ids = [aws_security_group.redis_service.id] | ||
} | ||
``` | ||
|
||
``` | ||
$ tflint | ||
1 issue(s) found: | ||
Warning: "cache.t3.mini" is an invalid node type. (aws_elasticache_cluster_invalid_type) | ||
on template.tf line 5: | ||
2: node_type = "cache.t3.mini" // invalid type! | ||
``` | ||
|
||
## Why | ||
|
||
Apply will fail. (Plan will succeed with the invalid value though) | ||
|
||
## How To Fix | ||
|
||
Select valid type according to the [document](https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/CacheNodes.SupportedTypes.html#CacheNodes.SupportedTypesByRegion) |
36 changes: 36 additions & 0 deletions
36
docs/rules/aws_elasticache_replication_group_default_parameter_group.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# aws_elasticache_replication_group_default_parameter_group | ||
|
||
Disallow using default parameter group. | ||
|
||
## Example | ||
|
||
```hcl | ||
resource "aws_elasticache_replication_group" "redis" { | ||
cluster_id = "app" | ||
engine = "redis" | ||
engine_version = "3.2.4" | ||
maintenance_window = "sun:00:00-sun:06:00" | ||
node_type = "cache.m4.large" | ||
num_cache_nodes = 1 | ||
port = 6379 | ||
parameter_group_name = "default.redis3.2" // default paramete group! | ||
subnet_group_name = "app-subnet-group" | ||
security_group_ids = ["${aws_security_group.redis.id}"] | ||
} | ||
``` | ||
|
||
``` | ||
$ tflint | ||
1 issue(s) found: | ||
Notice: "default.redis3.2" is default parameter group. You cannot edit it. (aws_elasticache_replication_group_default_parameter_group) | ||
on template.tf line 9: | ||
9: parameter_group_name = "default.redis3.2" // default parameter group! | ||
``` | ||
|
||
## Why | ||
|
||
You can modify parameter values in a custom parameter group, but you can't change the parameter values in a default parameter group. | ||
|
||
## How To Fix | ||
|
||
Create a new parameter group, and change the `parameter_group_name` to that. |
47 changes: 47 additions & 0 deletions
47
docs/rules/aws_elasticache_replication_group_invalid_type.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# aws_elasticache_replication_group_invalid_type | ||
|
||
Disallow using invalid type. | ||
|
||
## Example | ||
|
||
```hcl | ||
resource "aws_elasticache_replication_group" "default" { | ||
node_type = "cache.t3.mini" // invalid type! | ||
at_rest_encryption_enabled = true | ||
automatic_failover_enabled = true | ||
engine_version = "6.x" | ||
maintenance_window = "thu:02:30-thu:03:30" | ||
apply_immediately = false | ||
parameter_group_name = "custom.redis6.x.cluster.on" | ||
port = 6379 | ||
replication_group_description = " " | ||
replication_group_id = "replication_group_id" | ||
snapshot_retention_limit = 1 | ||
subnet_group_name = aws_elasticache_subnet_group.private.name | ||
security_group_ids = [aws_security_group.redis_service.id] | ||
cluster_mode { | ||
replicas_per_node_group = 1 | ||
num_node_groups = 2 | ||
} | ||
} | ||
``` | ||
|
||
``` | ||
$ tflint | ||
1 issue(s) found: | ||
Warning: "cache.t3.mini" is an invalid node type. (aws_elasticache_replication_group_invalid_type) | ||
on template.tf line 5: | ||
2: node_type = "cache.t3.mini" // invalid type! | ||
``` | ||
|
||
## Why | ||
|
||
Apply will fail. (Plan will succeed with the invalid value though) | ||
|
||
## How To Fix | ||
|
||
Select valid type according to the [document](https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/CacheNodes.SupportedTypes.html#CacheNodes.SupportedTypesByRegion) |
47 changes: 47 additions & 0 deletions
47
docs/rules/aws_elasticache_replication_group_previous_type.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# aws_elasticache_replication_group_previous_type | ||
|
||
Disallow using previous node types. | ||
|
||
## Example | ||
|
||
```hcl | ||
resource "aws_elasticache_replication_group" "default" { | ||
node_type = "cache.t1.micro" // previous node type! | ||
at_rest_encryption_enabled = true | ||
automatic_failover_enabled = true | ||
engine_version = "6.x" | ||
maintenance_window = "thu:02:30-thu:03:30" | ||
apply_immediately = false | ||
parameter_group_name = "custom.redis6.x.cluster.on" | ||
port = 6379 | ||
replication_group_description = " " | ||
replication_group_id = "replication_group_id" | ||
snapshot_retention_limit = 1 | ||
subnet_group_name = aws_elasticache_subnet_group.private.name | ||
security_group_ids = [aws_security_group.redis_service.id] | ||
cluster_mode { | ||
replicas_per_node_group = 1 | ||
num_node_groups = 2 | ||
} | ||
} | ||
``` | ||
|
||
``` | ||
$ tflint | ||
1 issue(s) found: | ||
Warning: "cache.t1.micro" is previous generation node type. (aws_elasticache_replication_group_previous_type) | ||
on template.tf line 6: | ||
2: node_type = "cache.t1.micro" // previous node type! | ||
``` | ||
|
||
## Why | ||
|
||
Previous node types are inferior to current generation in terms of performance and fee. Unless there is a special reason, you should avoid to use these ones. | ||
|
||
## How To Fix | ||
|
||
Select a current generation node type according to the [upgrade paths](https://aws.amazon.com/elasticache/previous-generation/). |
65 changes: 65 additions & 0 deletions
65
rules/aws_elasticache_replication_group_default_parameter_group.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package rules | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
hcl "github.com/hashicorp/hcl/v2" | ||
"github.com/terraform-linters/tflint-plugin-sdk/tflint" | ||
"github.com/terraform-linters/tflint-ruleset-aws/project" | ||
) | ||
|
||
// AwsElastiCacheReplicationGroupDefaultParameterGroupRule checks whether the cluster use default parameter group | ||
type AwsElastiCacheReplicationGroupDefaultParameterGroupRule struct { | ||
resourceType string | ||
attributeName string | ||
} | ||
|
||
// NewAwsElastiCacheReplicationGroupDefaultParameterGroupRule returns new rule with default attributes | ||
func NewAwsElastiCacheReplicationGroupDefaultParameterGroupRule() *AwsElastiCacheReplicationGroupDefaultParameterGroupRule { | ||
return &AwsElastiCacheReplicationGroupDefaultParameterGroupRule{ | ||
resourceType: "aws_elasticache_replication_group", | ||
attributeName: "parameter_group_name", | ||
} | ||
} | ||
|
||
// Name returns the rule name | ||
func (r *AwsElastiCacheReplicationGroupDefaultParameterGroupRule) Name() string { | ||
return "aws_elasticache_replication_group_default_parameter_group" | ||
} | ||
|
||
// Enabled returns whether the rule is enabled by default | ||
func (r *AwsElastiCacheReplicationGroupDefaultParameterGroupRule) Enabled() bool { | ||
return true | ||
} | ||
|
||
// Severity returns the rule severity | ||
func (r *AwsElastiCacheReplicationGroupDefaultParameterGroupRule) Severity() string { | ||
return tflint.NOTICE | ||
} | ||
|
||
// Link returns the rule reference link | ||
func (r *AwsElastiCacheReplicationGroupDefaultParameterGroupRule) Link() string { | ||
return project.ReferenceLink(r.Name()) | ||
} | ||
|
||
var defaultElastiCacheReplicationParameterGroupRegexp = regexp.MustCompile("^default") | ||
|
||
// Check checks the parameter group name starts with `default` | ||
func (r *AwsElastiCacheReplicationGroupDefaultParameterGroupRule) Check(runner tflint.Runner) error { | ||
return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { | ||
var parameterGroup string | ||
err := runner.EvaluateExpr(attribute.Expr, ¶meterGroup, nil) | ||
|
||
return runner.EnsureNoError(err, func() error { | ||
if defaultElastiCacheParameterGroupRegexp.Match([]byte(parameterGroup)) { | ||
runner.EmitIssueOnExpr( | ||
r, | ||
fmt.Sprintf("\"%s\" is default parameter group. You cannot edit it.", parameterGroup), | ||
attribute.Expr, | ||
) | ||
} | ||
return nil | ||
}) | ||
}) | ||
} |
55 changes: 55 additions & 0 deletions
55
rules/aws_elasticache_replication_group_default_parameter_group_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package rules | ||
|
||
import ( | ||
"testing" | ||
|
||
hcl "github.com/hashicorp/hcl/v2" | ||
"github.com/terraform-linters/tflint-plugin-sdk/helper" | ||
) | ||
|
||
func Test_AwsElastiCacheReplicationGroupDefaultParameterGroup(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Content string | ||
Expected helper.Issues | ||
}{ | ||
{ | ||
Name: "default.redis3.2 is default parameter group", | ||
Content: ` | ||
resource "aws_elasticache_replication_group" "cache" { | ||
parameter_group_name = "default.redis3.2" | ||
}`, | ||
Expected: helper.Issues{ | ||
{ | ||
Rule: NewAwsElastiCacheReplicationGroupDefaultParameterGroupRule(), | ||
Message: "\"default.redis3.2\" is default parameter group. You cannot edit it.", | ||
Range: hcl.Range{ | ||
Filename: "resource.tf", | ||
Start: hcl.Pos{Line: 3, Column: 28}, | ||
End: hcl.Pos{Line: 3, Column: 46}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "application3.2 is not default parameter group", | ||
Content: ` | ||
resource "aws_elasticache_replication_group" "cache" { | ||
parameter_group_name = "application3.2" | ||
}`, | ||
Expected: helper.Issues{}, | ||
}, | ||
} | ||
|
||
rule := NewAwsElastiCacheReplicationGroupDefaultParameterGroupRule() | ||
|
||
for _, tc := range cases { | ||
runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content}) | ||
|
||
if err := rule.Check(runner); err != nil { | ||
t.Fatalf("Unexpected error occurred: %s", err) | ||
} | ||
|
||
helper.AssertIssues(t, tc.Expected, runner.Issues) | ||
} | ||
} |
Oops, something went wrong.