-
Notifications
You must be signed in to change notification settings - Fork 362
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 #98 from wata727/aws_route_invalid_network_interface
Add AwsRouteInvalidNetworkInterfaceDetector
- Loading branch information
Showing
5 changed files
with
170 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
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,62 @@ | ||
package detector | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/hcl/hcl/ast" | ||
"github.com/wata727/tflint/issue" | ||
) | ||
|
||
type AwsRouteInvalidNetworkInterfaceDetector struct { | ||
*Detector | ||
IssueType string | ||
Target string | ||
DeepCheck bool | ||
networkInterfaces map[string]bool | ||
} | ||
|
||
func (d *Detector) CreateAwsRouteInvalidNetworkInterfaceDetector() *AwsRouteInvalidNetworkInterfaceDetector { | ||
return &AwsRouteInvalidNetworkInterfaceDetector{ | ||
Detector: d, | ||
IssueType: issue.ERROR, | ||
Target: "aws_route", | ||
DeepCheck: true, | ||
networkInterfaces: map[string]bool{}, | ||
} | ||
} | ||
|
||
func (d *AwsRouteInvalidNetworkInterfaceDetector) PreProcess() { | ||
resp, err := d.AwsClient.DescribeNetworkInterfaces() | ||
if err != nil { | ||
d.Logger.Error(err) | ||
d.Error = true | ||
return | ||
} | ||
|
||
for _, networkInterface := range resp.NetworkInterfaces { | ||
d.networkInterfaces[*networkInterface.NetworkInterfaceId] = true | ||
} | ||
} | ||
|
||
func (d *AwsRouteInvalidNetworkInterfaceDetector) Detect(file string, item *ast.ObjectItem, issues *[]*issue.Issue) { | ||
networkInterfaceToken, err := hclLiteralToken(item, "network_interface_id") | ||
if err != nil { | ||
d.Logger.Error(err) | ||
return | ||
} | ||
networkInterface, err := d.evalToString(networkInterfaceToken.Text) | ||
if err != nil { | ||
d.Logger.Error(err) | ||
return | ||
} | ||
|
||
if !d.networkInterfaces[networkInterface] { | ||
issue := &issue.Issue{ | ||
Type: d.IssueType, | ||
Message: fmt.Sprintf("\"%s\" is invalid network interface ID.", networkInterface), | ||
Line: networkInterfaceToken.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,94 @@ | ||
package detector | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/golang/mock/gomock" | ||
"github.com/k0kubun/pp" | ||
"github.com/wata727/tflint/config" | ||
"github.com/wata727/tflint/issue" | ||
"github.com/wata727/tflint/mock" | ||
) | ||
|
||
func TestDetectAwsRouteInvalidNetworkInterface(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Src string | ||
Response []*ec2.NetworkInterface | ||
Issues []*issue.Issue | ||
}{ | ||
{ | ||
Name: "network interface id is invalid", | ||
Src: ` | ||
resource "aws_route" "foo" { | ||
network_interface_id = "eni-1234abcd" | ||
}`, | ||
Response: []*ec2.NetworkInterface{ | ||
{ | ||
NetworkInterfaceId: aws.String("eni-5678abcd"), | ||
}, | ||
{ | ||
NetworkInterfaceId: aws.String("eni-abcd1234"), | ||
}, | ||
}, | ||
Issues: []*issue.Issue{ | ||
{ | ||
Type: "ERROR", | ||
Message: "\"eni-1234abcd\" is invalid network interface ID.", | ||
Line: 3, | ||
File: "test.tf", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "network interfaec id is valid", | ||
Src: ` | ||
resource "aws_route" "foo" { | ||
network_interface_id = "eni-1234abcd" | ||
}`, | ||
Response: []*ec2.NetworkInterface{ | ||
{ | ||
NetworkInterfaceId: aws.String("eni-1234abcd"), | ||
}, | ||
{ | ||
NetworkInterfaceId: aws.String("eni-abcd1234"), | ||
}, | ||
}, | ||
Issues: []*issue.Issue{}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
c := config.Init() | ||
c.DeepCheck = true | ||
|
||
awsClient := c.NewAwsClient() | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
ec2mock := mock.NewMockEC2API(ctrl) | ||
ec2mock.EXPECT().DescribeNetworkInterfaces(&ec2.DescribeNetworkInterfacesInput{}).Return(&ec2.DescribeNetworkInterfacesOutput{ | ||
NetworkInterfaces: tc.Response, | ||
}, nil) | ||
awsClient.Ec2 = ec2mock | ||
|
||
var issues = []*issue.Issue{} | ||
err := TestDetectByCreatorName( | ||
"CreateAwsRouteInvalidNetworkInterfaceDetector", | ||
tc.Src, | ||
"", | ||
c, | ||
awsClient, | ||
&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