Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AwsCloudWatchMetricAlarmInvalidUnitDetector #108

Merged
merged 1 commit into from
Apr 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions detector/aws_cloudwatch_metric_alarm_invalid_unit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package detector

import (
"fmt"

"github.com/hashicorp/hcl/hcl/ast"
"github.com/wata727/tflint/issue"
)

type AwsCloudWatchMetricAlarmInvalidUnitDetector struct {
*Detector
IssueType string
Target string
DeepCheck bool
validUnits map[string]bool
}

func (d *Detector) CreateAwsCloudWatchMetricAlarmInvalidUnitDetector() *AwsCloudWatchMetricAlarmInvalidUnitDetector {
return &AwsCloudWatchMetricAlarmInvalidUnitDetector{
Detector: d,
IssueType: issue.ERROR,
Target: "aws_cloudwatch_metric_alarm",
DeepCheck: false,
validUnits: map[string]bool{},
}
}

func (d *AwsCloudWatchMetricAlarmInvalidUnitDetector) PreProcess() {
// Ref: http://docs.aws.amazon.com/cli/latest/reference/cloudwatch/put-metric-alarm.html
d.validUnits = map[string]bool{
"Seconds": true,
"Microseconds": true,
"Milliseconds": true,
"Bytes": true,
"Kilobytes": true,
"Megabytes": true,
"Gigabytes": true,
"Terabytes": true,
"Bits": true,
"Kilobits": true,
"Megabits": true,
"Gigabits": true,
"Terabits": true,
"Percent": true,
"Count": true,
"Bytes/Second": true,
"Kilobytes/Second": true,
"Megabytes/Second": true,
"Gigabytes/Second": true,
"Terabytes/Second": true,
"Bits/Second": true,
"Kilobits/Second": true,
"Megabits/Second": true,
"Gigabits/Second": true,
"Terabits/Second": true,
"Count/Second": true,
"None": true,
}
}

func (d *AwsCloudWatchMetricAlarmInvalidUnitDetector) Detect(file string, item *ast.ObjectItem, issues *[]*issue.Issue) {
unitToken, err := hclLiteralToken(item, "unit")
if err != nil {
d.Logger.Error(err)
return
}
unit, err := d.evalToString(unitToken.Text)
if err != nil {
d.Logger.Error(err)
return
}

if !d.validUnits[unit] {
issue := &issue.Issue{
Type: d.IssueType,
Message: fmt.Sprintf("\"%s\" is invalid unit.", unit),
Line: unitToken.Pos.Line,
File: file,
}
*issues = append(*issues, issue)
}
}
94 changes: 94 additions & 0 deletions detector/aws_cloudwatch_metric_alarm_invalid_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package detector

import (
"reflect"
"testing"

"github.com/k0kubun/pp"
"github.com/wata727/tflint/config"
"github.com/wata727/tflint/issue"
)

func TestAwsCloudWatchMetricAlarmInvalidUnit(t *testing.T) {
cases := []struct {
Name string
Src string
Issues []*issue.Issue
}{
{
Name: "GB is invalid",
Src: `
resource "aws_cloudwatch_metric_alarm" "test" {
metric_name = "FreeableMemory"
namespace = "AWS/RDS"

period = "300"
statistic = "Average"
threshold = "1"
unit = "GB"
}`,
Issues: []*issue.Issue{
{
Type: "ERROR",
Message: "\"GB\" is invalid unit.",
Line: 9,
File: "test.tf",
},
},
},
{
Name: "Lowercase is invalid",
Src: `
resource "aws_cloudwatch_metric_alarm" "test" {
metric_name = "FreeableMemory"
namespace = "AWS/RDS"

period = "300"
statistic = "Average"
threshold = "1"
unit = "gigabytes"
}`,
Issues: []*issue.Issue{
{
Type: "ERROR",
Message: "\"gigabytes\" is invalid unit.",
Line: 9,
File: "test.tf",
},
},
},
{
Name: "Gigabytes is valid",
Src: `
resource "aws_cloudwatch_metric_alarm" "test" {
metric_name = "FreeableMemory"
namespace = "AWS/RDS"

period = "300"
statistic = "Average"
threshold = "1"
unit = "Gigabytes"
}`,
Issues: []*issue.Issue{},
},
}

for _, tc := range cases {
var issues = []*issue.Issue{}
err := TestDetectByCreatorName(
"CreateAwsCloudWatchMetricAlarmInvalidUnitDetector",
tc.Src,
"",
config.Init(),
config.Init().NewAwsClient(),
&issues,
)
if err != nil {
t.Fatalf("\nERROR: %s", err)
}

if !reflect.DeepEqual(issues, tc.Issues) {
t.Fatalf("\nBad: %s\nExpected: %s\n\ntestcase: %s", pp.Sprint(issues), pp.Sprint(tc.Issues), tc.Name)
}
}
}
1 change: 1 addition & 0 deletions detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ var detectors = map[string]string{
"aws_route_invalid_vpc_peering_connection": "CreateAwsRouteInvalidVpcPeeringConnectionDetector",
"aws_route_invalid_instance": "CreateAwsRouteInvalidInstanceDetector",
"aws_route_invalid_network_interface": "CreateAwsRouteInvalidNetworkInterfaceDetector",
"aws_cloudwatch_metric_alarm_invalid_unit": "CreateAwsCloudWatchMetricAlarmInvalidUnitDetector",
}

func NewDetector(templates map[string]*ast.File, state *state.TFState, tfvars []*ast.File, c *config.Config) (*Detector, error) {
Expand Down
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Issues are classified into the following three types.
- **AWS Route**
- [aws_route_not_specified_target](aws_route_not_specified_target.md)
- [aws_route_specified_multiple_targets](aws_route_specified_multiple_targets.md)
- **AWS CloudWatch Metric Alarm**
- [aws_cloudwatch_metric_alarm_invalid_unit](aws_cloudwatch_metric_alarm_invalid_unit.md)
- **Terraform**
- [terraform_module_pinned_source](terraform_module_pinned_source.md)

Expand Down
62 changes: 62 additions & 0 deletions docs/aws_cloudwatch_metric_alarm_invalid_unit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# AWS CloudWatch Metric Alarm Invalid Unit
This issue reported if you specify invalid unit. This issue type is ERROR.

## Example
```
resource "aws_cloudwatch_metric_alarm" "rds-writer-memory" {
alarm_name = "terraform-test-foobar5"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
statistic = "Average"
threshold = "80"
unit = "percent" // valid unit is "Percent"
alarm_description = "This metric monitor ec2 cpu utilization"
insufficient_data_actions = []
}
```

The following is the execution result of TFLint:

```
template.tf
ERROR:10 "percent" is invalid unit.

Result: 1 issues (1 errors , 0 warnings , 0 notices)
```

## Why
Only the followings are supported for CloudWatch alarm unit. If an invalid unit is specified, an error will occur at `terraform apply`.

- Seconds
- Microseconds
- Milliseconds
- Bytes
- Kilobytes
- Megabytes
- Gigabytes
- Terabytes
- Bits
- Kilobits
- Megabits
- Gigabits
- Terabits
- Percent
- Count
- Bytes/Second
- Kilobytes/Second
- Megabytes/Second
- Gigabytes/Second
- Terabytes/Second
- Bits/Second
- Kilobits/Second
- Megabits/Second
- Gigabits/Second
- Terabits/Second
- Count/Second
- None

## How To Fix
Check the unit and select a valid unit again.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resource "aws_db_instance" "mysql" {
allocated_storage = 10
engine = "mysql"
engine_version = "5.6.17"
instance_class = "${var.mysql_types[0]}}" // aws_db_instance_invalid_type
instance_class = "${var.mysql_types[0]}" // aws_db_instance_invalid_type
name = "mydb"
username = "foo"
password = "secret_password" // aws_db_instance_readable_password
Expand Down
10 changes: 8 additions & 2 deletions integration/general/result.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
},
{
"type": "ERROR",
"message": "\"t1.micro}\" is invalid instance type.",
"message": "\"t1.micro\" is invalid instance type.",
"line": 9,
"file": "github.com/wata727/example-module/aws_db_instance.tf"
},
Expand All @@ -86,7 +86,13 @@
{
"type": "WARNING",
"message": "Module source \"github.com/wata727/example-module\" is not pinned",
"line": 35,
"line": 49,
"file": "template.tf"
},
{
"type": "ERROR",
"message": "\"percent\" is invalid unit.",
"line": 43,
"file": "template.tf"
}
]
14 changes: 14 additions & 0 deletions integration/general/template.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ resource "aws_route" "multiple_specified" {
egress_only_gateway_id = "eigw-1234abcd"
}

resource "aws_cloudwatch_metric_alarm" "rds-writer-memory" {
alarm_name = "terraform-test-foobar5"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
statistic = "Average"
threshold = "80"
unit = "percent" // aws_cloudwatch_metric_alarm_invalid_unit
alarm_description = "This metric monitor ec2 cpu utilization"
insufficient_data_actions = []
}

module "ec2_instance" {
source = "github.com/wata727/example-module" // terraform_module_pinned_source
instance_types = "${var.instance_type}"
Expand Down