-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from wata727/aws_route_not_specified_target
Add AwsRouteNotSpecifiedTargetDetector
- Loading branch information
Showing
5 changed files
with
177 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package detector | ||
|
||
import ( | ||
"github.com/hashicorp/hcl/hcl/ast" | ||
"github.com/wata727/tflint/issue" | ||
) | ||
|
||
type AwsRouteNotSpecifiedTargetDetector struct { | ||
*Detector | ||
IssueType string | ||
Target string | ||
DeepCheck bool | ||
} | ||
|
||
func (d *Detector) CreateAwsRouteNotSpecifiedTargetDetector() *AwsRouteNotSpecifiedTargetDetector { | ||
return &AwsRouteNotSpecifiedTargetDetector{ | ||
Detector: d, | ||
IssueType: issue.ERROR, | ||
Target: "aws_route", | ||
DeepCheck: false, | ||
} | ||
} | ||
|
||
func (d *AwsRouteNotSpecifiedTargetDetector) Detect(file string, item *ast.ObjectItem, issues *[]*issue.Issue) { | ||
if IsKeyNotFound(item, "gateway_id") && | ||
IsKeyNotFound(item, "egress_only_gateway_id") && | ||
IsKeyNotFound(item, "nat_gateway_id") && | ||
IsKeyNotFound(item, "instance_id") && | ||
IsKeyNotFound(item, "vpc_peering_connection_id") && | ||
IsKeyNotFound(item, "network_interface_id") { | ||
issue := &issue.Issue{ | ||
Type: d.IssueType, | ||
Message: "route target is not specified, each route must contain either a gateway_id, egress_only_gateway_id a nat_gateway_id, an instance_id or a vpc_peering_connection_id or a network_interface_id.", | ||
Line: item.Pos().Line, | ||
File: file, | ||
} | ||
*issues = append(*issues, issue) | ||
} | ||
|
||
} |
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,107 @@ | ||
package detector | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/k0kubun/pp" | ||
"github.com/wata727/tflint/config" | ||
"github.com/wata727/tflint/issue" | ||
) | ||
|
||
func TestDetectAwsRouteNotSpecifiedTarget(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Src string | ||
Issues []*issue.Issue | ||
}{ | ||
{ | ||
Name: "route target is not specified", | ||
Src: ` | ||
resource "aws_route" "foo" { | ||
route_table_id = "rtb-1234abcd" | ||
}`, | ||
Issues: []*issue.Issue{ | ||
{ | ||
Type: "ERROR", | ||
Message: "route target is not specified, each route must contain either a gateway_id, egress_only_gateway_id a nat_gateway_id, an instance_id or a vpc_peering_connection_id or a network_interface_id.", | ||
Line: 2, | ||
File: "test.tf", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "gateway_id is specified", | ||
Src: ` | ||
resource "aws_route" "foo" { | ||
route_table_id = "rtb-1234abcd" | ||
gateway_id = "igw-1234abcd" | ||
}`, | ||
Issues: []*issue.Issue{}, | ||
}, | ||
{ | ||
Name: "egress_only_gateway_id is specified", | ||
Src: ` | ||
resource "aws_route" "foo" { | ||
route_table_id = "rtb-1234abcd" | ||
egress_only_gateway_id = "eigw-1234abcd" | ||
}`, | ||
Issues: []*issue.Issue{}, | ||
}, | ||
{ | ||
Name: "nat_gateway_id is specified", | ||
Src: ` | ||
resource "aws_route" "foo" { | ||
route_table_id = "rtb-1234abcd" | ||
nat_gateway_id = "nat-1234abcd" | ||
}`, | ||
Issues: []*issue.Issue{}, | ||
}, | ||
{ | ||
Name: "instance_id is specified", | ||
Src: ` | ||
resource "aws_route" "foo" { | ||
route_table_id = "rtb-1234abcd" | ||
instance_id = "i-1234abcd" | ||
}`, | ||
Issues: []*issue.Issue{}, | ||
}, | ||
{ | ||
Name: "vpc_peering_connection_id is specified", | ||
Src: ` | ||
resource "aws_route" "foo" { | ||
route_table_id = "rtb-1234abcd" | ||
vpc_peering_connection_id = "pcx-1234abcd" | ||
}`, | ||
Issues: []*issue.Issue{}, | ||
}, | ||
{ | ||
Name: "network_interface_id is specified", | ||
Src: ` | ||
resource "aws_route" "foo" { | ||
route_table_id = "rtb-1234abcd" | ||
network_interface_id = "eni-1234abcd" | ||
}`, | ||
Issues: []*issue.Issue{}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
var issues = []*issue.Issue{} | ||
err := TestDetectByCreatorName( | ||
"CreateAwsRouteNotSpecifiedTargetDetector", | ||
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) | ||
} | ||
} | ||
} |
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,27 @@ | ||
# AWS Route Not Specified Target | ||
Report this issue if route target is not specified. This issue type is ERROR. | ||
|
||
## Example | ||
``` | ||
resource "aws_route" "foo" { | ||
route_table_id = "rtb-1234abcd" | ||
destination_cidr_block = "10.0.1.0/22" | ||
} | ||
``` | ||
|
||
The following is the execution result of TFLint: | ||
|
||
|
||
``` | ||
$ tflint | ||
template.tf | ||
ERROR:1 route target is not specified, each route must contain either a gateway_id, egress_only_gateway_id a nat_gateway_id, an instance_id or a vpc_peering_connection_id or a network_interface_id. | ||
Result: 1 issues (1 errors , 0 warnings , 0 notices) | ||
``` | ||
|
||
## Why | ||
`aws_route` creates new routing in route tables. If a routing target is not specified, an error will occur when run `terraform apply`. | ||
|
||
## How To Fix | ||
Please add a routing target. There are kinds of `gateway_id`, `egress_only_gateway_id`, `nat_gateway_id`, `instance_id`, `vpc_peering_connection_id`, `network_interface_id`. |