Skip to content

Commit c19cbb5

Browse files
authored
Merge pull request #35 from wata727/add_previous_node_type_detector_for_elasticache
add previous type detector for elasticache
2 parents b02e16d + d30f970 commit c19cbb5

8 files changed

+149
-3
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package detector
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/wata727/tflint/issue"
7+
)
8+
9+
type AwsElastiCacheClusterPreviousTypeDetector struct {
10+
*Detector
11+
}
12+
13+
func (d *Detector) CreateAwsElastiCacheClusterPreviousTypeDetector() *AwsElastiCacheClusterPreviousTypeDetector {
14+
return &AwsElastiCacheClusterPreviousTypeDetector{d}
15+
}
16+
17+
func (d *AwsElastiCacheClusterPreviousTypeDetector) Detect(issues *[]*issue.Issue) {
18+
var previousNodeType = map[string]bool{
19+
"cache.m1.small": true,
20+
"cache.m1.medium": true,
21+
"cache.m1.large": true,
22+
"cache.m1.xlarge": true,
23+
"cache.m2.xlarge": true,
24+
"cache.m2.2xlarge": true,
25+
"cache.m2.4xlarge": true,
26+
"cache.c1.xlarge": true,
27+
"cache.t1.micro": true,
28+
}
29+
30+
for filename, list := range d.ListMap {
31+
for _, item := range list.Filter("resource", "aws_elasticache_cluster").Items {
32+
nodeTypeToken, err := hclLiteralToken(item, "node_type")
33+
if err != nil {
34+
d.Logger.Error(err)
35+
continue
36+
}
37+
nodeType, err := d.evalToString(nodeTypeToken.Text)
38+
if err != nil {
39+
d.Logger.Error(err)
40+
continue
41+
}
42+
43+
if previousNodeType[nodeType] {
44+
issue := &issue.Issue{
45+
Type: "WARNING",
46+
Message: fmt.Sprintf("\"%s\" is previous generation node type.", nodeType),
47+
Line: nodeTypeToken.Pos.Line,
48+
File: filename,
49+
}
50+
*issues = append(*issues, issue)
51+
}
52+
}
53+
}
54+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package detector
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/wata727/tflint/config"
8+
"github.com/wata727/tflint/issue"
9+
)
10+
11+
func TestDetectAwsElastiCacheClusterPreviousType(t *testing.T) {
12+
cases := []struct {
13+
Name string
14+
Src string
15+
Issues []*issue.Issue
16+
}{
17+
{
18+
Name: "cache.t1.micro is previous type",
19+
Src: `
20+
resource "aws_elasticache_cluster" "redis" {
21+
node_type = "cache.t1.micro"
22+
}`,
23+
Issues: []*issue.Issue{
24+
&issue.Issue{
25+
Type: "WARNING",
26+
Message: "\"cache.t1.micro\" is previous generation node type.",
27+
Line: 3,
28+
File: "test.tf",
29+
},
30+
},
31+
},
32+
{
33+
Name: "cache.t2.micro is not previous type",
34+
Src: `
35+
resource "aws_elasticache_cluster" "redis" {
36+
node_type = "cache.t2.micro"
37+
}`,
38+
Issues: []*issue.Issue{},
39+
},
40+
}
41+
42+
for _, tc := range cases {
43+
var issues = []*issue.Issue{}
44+
TestDetectByCreatorName(
45+
"CreateAwsElastiCacheClusterPreviousTypeDetector",
46+
tc.Src,
47+
config.Init(),
48+
config.Init().NewAwsClient(),
49+
&issues,
50+
)
51+
52+
if !reflect.DeepEqual(issues, tc.Issues) {
53+
t.Fatalf("Bad: %s\nExpected: %s\n\ntestcase: %s", issues, tc.Issues, tc.Name)
54+
}
55+
}
56+
}

detector/detector.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var detectors = map[string]string{
5050
"aws_elasticache_cluster_invalid_subnet_group": "CreateAwsElastiCacheClusterInvalidSubnetGroupDetector",
5151
"aws_elasticache_cluster_invalid_security_group": "CreateAwsElastiCacheClusterInvalidSecurityGroupDetector",
5252
"aws_elasticache_cluster_invalid_type": "CreateAwsElastiCacheClusterInvalidTypeDetector",
53+
"aws_elasticache_cluster_previous_type": "CreateAwsElastiCacheClusterPreviousTypeDetector",
5354
}
5455

5556
func NewDetector(listMap map[string]*ast.ObjectList, c *config.Config) (*Detector, error) {

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Issues are classified into the following three types.
2222
- [aws_db_instance_default_parameter_group](aws_db_instance_default_parameter_group.md)
2323
- AWS ElastiCache Cluster
2424
- [aws_elasticache_cluster_invalid_type](aws_elasticache_cluster_invalid_type.md)
25+
- [aws_elasticache_cluster_previous_type](aws_elasticache_cluster_previous_type.md)
2526
- [aws_elasticache_cluster_default_parameter_group](aws_elasticache_cluster_default_parameter_group.md)
2627

2728
### Invalid Issues

docs/aws_db_instance_invalid_type.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ Result: 1 issues (1 errors , 0 warnings , 0 notices)
3030
If an invalid instance type is specified, an error will occur at `terraform apply`.
3131

3232
## How to fix
33-
Check the [instance type list](https://aws.amazon.com/jp/rds/details/) and select a valid instance type again.
33+
Check the [instance type list](https://aws.amazon.com/rds/details/) and select a valid instance type again.

docs/aws_db_instance_previous_type.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ Result: 1 issues (0 errors , 1 warnings , 0 notices)
3030
There are two types of instance types, the current generation and the previous generation. The current generation is superior to the previous generation in terms of performance and fee. AWS also officially states that unless there is a special reason, you should use the instance type of the current generation.
3131

3232
## How to fix
33-
Follow the [upgrade paths](https://aws.amazon.com/jp/rds/previous-generation/) and confirm that the instance type of the current generation can be used, then select again.
33+
Follow the [upgrade paths](https://aws.amazon.com/rds/previous-generation/) and confirm that the instance type of the current generation can be used, then select again.

docs/aws_elasticache_cluster_invalid_type.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ Result: 1 issues (1 errors , 0 warnings , 0 notices)
3131
If an invalid node type is specified, an error will occur at `terraform apply`.
3232

3333
## How to fix
34-
Check the [node type list](https://aws.amazon.com/jp/elasticache/details/) and select a valid node type again.
34+
Check the [node type list](https://aws.amazon.com/elasticache/details/) and select a valid node type again.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# AWS ElastiCache Cluster Instance Previous Type
2+
Report this issue if you have specified the previous node type. This issue type is WARNING.
3+
4+
## Example
5+
```
6+
resource "aws_elasticache_cluster" "redis" {
7+
cluster_id = "app"
8+
engine = "redis"
9+
engine_version = "3.2.4"
10+
maintenance_window = "sun:00:00-sun:06:00"
11+
node_type = "cache.t1.micro" // previous type!
12+
num_cache_nodes = 1
13+
port = 6379
14+
parameter_group_name = "default.redis3.2"
15+
subnet_group_name = "app-subnet-group"
16+
security_group_ids = ["${aws_security_group.redis.id}"]
17+
}
18+
```
19+
20+
The following is the execution result of TFLint:
21+
22+
```
23+
$ tflint
24+
template.tf
25+
WARNING:6 "cache.t1.micro" is previous generation node type.
26+
27+
Result: 1 issues (0 errors , 1 warnings , 0 notices)
28+
```
29+
30+
## Why
31+
There are two types of node types, the current generation and the previous generation. The current generation is superior to the previous generation in terms of performance and fee. AWS also officially states that unless there is a special reason, you should use the node type of the current generation.
32+
33+
## How to fix
34+
Follow the [upgrade paths](https://aws.amazon.com/elasticache/previous-generation/) and confirm that the node type of the current generation can be used, then select again.

0 commit comments

Comments
 (0)