-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AwsECSClusterDuplicateNameDetector
- Loading branch information
Showing
6 changed files
with
228 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
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,63 @@ | ||
package detector | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/wata727/tflint/issue" | ||
"github.com/wata727/tflint/schema" | ||
) | ||
|
||
type AwsECSClusterDuplicateNameDetector struct { | ||
*Detector | ||
clusters map[string]bool | ||
} | ||
|
||
func (d *Detector) CreateAwsECSClusterDuplicateNameDetector() *AwsECSClusterDuplicateNameDetector { | ||
nd := &AwsECSClusterDuplicateNameDetector{ | ||
Detector: d, | ||
clusters: map[string]bool{}, | ||
} | ||
nd.Name = "aws_ecs_cluster_duplicate_name" | ||
nd.IssueType = issue.ERROR | ||
nd.TargetType = "resource" | ||
nd.Target = "aws_ecs_cluster" | ||
nd.DeepCheck = true | ||
return nd | ||
} | ||
|
||
func (d *AwsECSClusterDuplicateNameDetector) PreProcess() { | ||
resp, err := d.AwsClient.DescribeClusters() | ||
if err != nil { | ||
d.Logger.Error(err) | ||
d.Error = true | ||
return | ||
} | ||
|
||
for _, cluster := range resp.Clusters { | ||
d.clusters[*cluster.ClusterName] = true | ||
} | ||
} | ||
|
||
func (d *AwsECSClusterDuplicateNameDetector) Detect(resource *schema.Resource, issues *[]*issue.Issue) { | ||
nameToken, ok := resource.GetToken("name") | ||
if !ok { | ||
return | ||
} | ||
name, err := d.evalToString(nameToken.Text) | ||
if err != nil { | ||
d.Logger.Error(err) | ||
return | ||
} | ||
|
||
identityCheckFunc := func(attributes map[string]string) bool { return attributes["name"] == name } | ||
if d.clusters[name] && !d.State.Exists(d.Target, resource.Id, identityCheckFunc) { | ||
issue := &issue.Issue{ | ||
Detector: d.Name, | ||
Type: d.IssueType, | ||
Message: fmt.Sprintf("\"%s\" is duplicate name. It must be unique.", name), | ||
Line: nameToken.Pos.Line, | ||
File: nameToken.Pos.Filename, | ||
} | ||
*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,138 @@ | ||
package detector | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ecs" | ||
"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 TestDetectAwsECSDuplicateName(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Src string | ||
State string | ||
Response []*ecs.Cluster | ||
Issues []*issue.Issue | ||
}{ | ||
{ | ||
Name: "Cluster name is duplicate", | ||
Src: ` | ||
resource "aws_ecs_cluster" "foo" { | ||
name = "white-hart" | ||
}`, | ||
Response: []*ecs.Cluster{ | ||
{ | ||
ClusterName: aws.String("white-hart"), | ||
}, | ||
{ | ||
ClusterName: aws.String("black-hart"), | ||
}, | ||
}, | ||
Issues: []*issue.Issue{ | ||
{ | ||
Detector: "aws_ecs_cluster_duplicate_name", | ||
Type: "ERROR", | ||
Message: "\"white-hart\" is duplicate name. It must be unique.", | ||
Line: 3, | ||
File: "test.tf", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "Cluster name is unique", | ||
Src: ` | ||
resource "aws_ecs_cluster" "foo" { | ||
name = "white-hart" | ||
}`, | ||
Response: []*ecs.Cluster{ | ||
{ | ||
ClusterName: aws.String("brown-hart"), | ||
}, | ||
{ | ||
ClusterName: aws.String("black-hart"), | ||
}, | ||
}, | ||
Issues: []*issue.Issue{}, | ||
}, | ||
{ | ||
Name: "Cluster is duplicate, but exists in state", | ||
Src: ` | ||
resource "aws_ecs_cluster" "foo" { | ||
name = "white-hart" | ||
}`, | ||
State: ` | ||
{ | ||
"modules": [ | ||
{ | ||
"resources": { | ||
"aws_ecs_cluster.foo": { | ||
"type": "aws_ecs_cluster", | ||
"depends_on": [], | ||
"primary": { | ||
"id": "arn:aws:ecs:us-east-1:hogehoge:cluster/white-hart", | ||
"attributes": { | ||
"id": "arn:aws:ecs:us-east-1:hogehoge:cluster/white-hart", | ||
"name": "white-hart" | ||
}, | ||
"meta": {}, | ||
"tainted": false | ||
}, | ||
"deposed": [], | ||
"provider": "" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
`, | ||
Response: []*ecs.Cluster{ | ||
{ | ||
ClusterName: aws.String("white-hart"), | ||
}, | ||
{ | ||
ClusterName: aws.String("black-hart"), | ||
}, | ||
}, | ||
Issues: []*issue.Issue{}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
c := config.Init() | ||
c.DeepCheck = true | ||
|
||
awsClient := c.NewAwsClient() | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
ecsmock := mock.NewMockECSAPI(ctrl) | ||
ecsmock.EXPECT().ListClusters(&ecs.ListClustersInput{}).Return(&ecs.ListClustersOutput{}, nil) | ||
ecsmock.EXPECT().DescribeClusters(&ecs.DescribeClustersInput{}).Return(&ecs.DescribeClustersOutput{ | ||
Clusters: tc.Response, | ||
}, nil) | ||
awsClient.Ecs = ecsmock | ||
|
||
var issues = []*issue.Issue{} | ||
err := TestDetectByCreatorName( | ||
"CreateAwsECSClusterDuplicateNameDetector", | ||
tc.Src, | ||
tc.State, | ||
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