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

Improve ineffecient module detector method #10

Merged
merged 4 commits into from
Dec 11, 2016
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
14 changes: 11 additions & 3 deletions detector/aws_db_instance_default_parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import (
"github.com/wata727/tflint/issue"
)

func (d *Detector) DetectAwsDbInstanceDefaultParameterGroup(issues *[]*issue.Issue) {
type AwsDBInstanceDefaultParameterGroupDetector struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [golint] exported type AwsDBInstanceDefaultParameterGroupDetector should have comment or be unexported

*Detector
}

func (d *Detector) CreateAwsDBInstanceDefaultParameterGroupDetector() *AwsDBInstanceDefaultParameterGroupDetector {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [golint] exported method Detector.CreateAwsDBInstanceDefaultParameterGroupDetector should have comment or be unexported

return &AwsDBInstanceDefaultParameterGroupDetector{d}
}

func (d *AwsDBInstanceDefaultParameterGroupDetector) Detect(issues *[]*issue.Issue) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [golint] exported method AwsDBInstanceDefaultParameterGroupDetector.Detect should have comment or be unexported

for filename, list := range d.ListMap {
for _, item := range list.Filter("resource", "aws_db_instance").Items {
parameterGroupToken, err := hclLiteralToken(item, "parameter_group_name")
Expand All @@ -21,7 +29,7 @@ func (d *Detector) DetectAwsDbInstanceDefaultParameterGroup(issues *[]*issue.Iss
continue
}

if isDefaultDbParameterGroup(parameterGroup) {
if d.isDefaultDbParameterGroup(parameterGroup) {
issue := &issue.Issue{
Type: "NOTICE",
Message: fmt.Sprintf("\"%s\" is default parameter group. You cannot edit it.", parameterGroup),
Expand All @@ -34,6 +42,6 @@ func (d *Detector) DetectAwsDbInstanceDefaultParameterGroup(issues *[]*issue.Iss
}
}

func isDefaultDbParameterGroup(s string) bool {
func (d *AwsDBInstanceDefaultParameterGroupDetector) isDefaultDbParameterGroup(s string) bool {
return regexp.MustCompile("^default").Match([]byte(s))
}
24 changes: 8 additions & 16 deletions detector/aws_db_instance_default_parameter_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import (
"reflect"
"testing"

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

func TestDetectAwsDbInstanceDefaultParameterGroup(t *testing.T) {
func TestDetectAwsDBInstanceDefaultParameterGroup(t *testing.T) {
cases := []struct {
Name string
Src string
Expand Down Expand Up @@ -43,19 +40,14 @@ resource "aws_db_instance" "db" {
}

for _, tc := range cases {
listMap := make(map[string]*ast.ObjectList)
root, _ := parser.Parse([]byte(tc.Src))
list, _ := root.Node.(*ast.ObjectList)
listMap["test.tf"] = list

evalConfig, _ := evaluator.NewEvaluator(listMap, config.Init())
d := &Detector{
ListMap: listMap,
EvalConfig: evalConfig,
}

var issues = []*issue.Issue{}
d.DetectAwsDbInstanceDefaultParameterGroup(&issues)
TestDetectByCreatorName(
"CreateAwsDBInstanceDefaultParameterGroupDetector",
tc.Src,
config.Init(),
config.Init().NewAwsClient(),
&issues,
)

if !reflect.DeepEqual(issues, tc.Issues) {
t.Fatalf("Bad: %s\nExpected: %s\n\ntestcase: %s", issues, tc.Issues, tc.Name)
Expand Down
14 changes: 11 additions & 3 deletions detector/aws_elasticache_cluster_default_parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import (
"github.com/wata727/tflint/issue"
)

func (d *Detector) DetectAwsElasticacheClusterDefaultParameterGroup(issues *[]*issue.Issue) {
type AwsElastiCacheClusterDefaultParameterGroupDetector struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [golint] exported type AwsElastiCacheClusterDefaultParameterGroupDetector should have comment or be unexported

*Detector
}

func (d *Detector) CreateAwsElastiCacheClusterDefaultParameterGroupDetector() *AwsElastiCacheClusterDefaultParameterGroupDetector {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [golint] exported method Detector.CreateAwsElastiCacheClusterDefaultParameterGroupDetector should have comment or be unexported

return &AwsElastiCacheClusterDefaultParameterGroupDetector{d}
}

func (d *AwsElastiCacheClusterDefaultParameterGroupDetector) Detect(issues *[]*issue.Issue) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [golint] exported method AwsElastiCacheClusterDefaultParameterGroupDetector.Detect should have comment or be unexported

for filename, list := range d.ListMap {
for _, item := range list.Filter("resource", "aws_elasticache_cluster").Items {
parameterGroupToken, err := hclLiteralToken(item, "parameter_group_name")
Expand All @@ -21,7 +29,7 @@ func (d *Detector) DetectAwsElasticacheClusterDefaultParameterGroup(issues *[]*i
continue
}

if isDefaultCacheParameterGroup(parameterGroup) {
if d.isDefaultCacheParameterGroup(parameterGroup) {
issue := &issue.Issue{
Type: "NOTICE",
Message: fmt.Sprintf("\"%s\" is default parameter group. You cannot edit it.", parameterGroup),
Expand All @@ -34,6 +42,6 @@ func (d *Detector) DetectAwsElasticacheClusterDefaultParameterGroup(issues *[]*i
}
}

func isDefaultCacheParameterGroup(s string) bool {
func (d *AwsElastiCacheClusterDefaultParameterGroupDetector) isDefaultCacheParameterGroup(s string) bool {
return regexp.MustCompile("^default").Match([]byte(s))
}
24 changes: 8 additions & 16 deletions detector/aws_elasticache_cluster_default_parameter_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import (
"reflect"
"testing"

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

func TestDetectAwsElasticacheClusterDefaultParameterGroup(t *testing.T) {
func TestDetectAwsElastiCacheClusterDefaultParameterGroup(t *testing.T) {
cases := []struct {
Name string
Src string
Expand Down Expand Up @@ -43,19 +40,14 @@ resource "aws_elasticache_cluster" "cache" {
}

for _, tc := range cases {
listMap := make(map[string]*ast.ObjectList)
root, _ := parser.Parse([]byte(tc.Src))
list, _ := root.Node.(*ast.ObjectList)
listMap["test.tf"] = list

evalConfig, _ := evaluator.NewEvaluator(listMap, config.Init())
d := &Detector{
ListMap: listMap,
EvalConfig: evalConfig,
}

var issues = []*issue.Issue{}
d.DetectAwsElasticacheClusterDefaultParameterGroup(&issues)
TestDetectByCreatorName(
"CreateAwsElastiCacheClusterDefaultParameterGroupDetector",
tc.Src,
config.Init(),
config.Init().NewAwsClient(),
&issues,
)

if !reflect.DeepEqual(issues, tc.Issues) {
t.Fatalf("Bad: %s\nExpected: %s\n\ntestcase: %s", issues, tc.Issues, tc.Name)
Expand Down
12 changes: 10 additions & 2 deletions detector/aws_instance_default_standard_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import (
"github.com/wata727/tflint/issue"
)

func (d *Detector) DetectAwsInstanceDefaultStandardVolume(issues *[]*issue.Issue) {
type AwsInstanceDefaultStandardVolumeDetector struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [golint] exported type AwsInstanceDefaultStandardVolumeDetector should have comment or be unexported

*Detector
}

func (d *Detector) CreateAwsInstanceDefaultStandardVolumeDetector() *AwsInstanceDefaultStandardVolumeDetector {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [golint] exported method Detector.CreateAwsInstanceDefaultStandardVolumeDetector should have comment or be unexported

return &AwsInstanceDefaultStandardVolumeDetector{d}
}

func (d *AwsInstanceDefaultStandardVolumeDetector) Detect(issues *[]*issue.Issue) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [golint] exported method AwsInstanceDefaultStandardVolumeDetector.Detect should have comment or be unexported

for filename, list := range d.ListMap {
for _, item := range list.Filter("resource", "aws_instance").Items {
d.detectForBlockDevices(issues, item, filename, "root_block_device")
Expand All @@ -14,7 +22,7 @@ func (d *Detector) DetectAwsInstanceDefaultStandardVolume(issues *[]*issue.Issue
}
}

func (d *Detector) detectForBlockDevices(issues *[]*issue.Issue, item *ast.ObjectItem, filename string, device string) {
func (d *AwsInstanceDefaultStandardVolumeDetector) detectForBlockDevices(issues *[]*issue.Issue, item *ast.ObjectItem, filename string, device string) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [gotype] undeclared name: ast

if !IsKeyNotFound(item, device) {
deviceItems, err := hclObjectItems(item, device)
if err != nil {
Expand Down
22 changes: 7 additions & 15 deletions detector/aws_instance_default_standard_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import (
"reflect"
"testing"

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

Expand Down Expand Up @@ -110,19 +107,14 @@ resource "aws_instance" "web" {
}

for _, tc := range cases {
listMap := make(map[string]*ast.ObjectList)
root, _ := parser.Parse([]byte(tc.Src))
list, _ := root.Node.(*ast.ObjectList)
listMap["test.tf"] = list

evalConfig, _ := evaluator.NewEvaluator(listMap, config.Init())
d := &Detector{
ListMap: listMap,
EvalConfig: evalConfig,
}

var issues = []*issue.Issue{}
d.DetectAwsInstanceDefaultStandardVolume(&issues)
TestDetectByCreatorName(
"CreateAwsInstanceDefaultStandardVolumeDetector",
tc.Src,
config.Init(),
config.Init().NewAwsClient(),
&issues,
)

if !reflect.DeepEqual(issues, tc.Issues) {
t.Fatalf("Bad: %s\nExpected: %s\n\ntestcase: %s", issues, tc.Issues, tc.Name)
Expand Down
23 changes: 17 additions & 6 deletions detector/aws_instance_invalid_ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,30 @@ import (
"github.com/wata727/tflint/issue"
)

func (d *Detector) DetectAwsInstanceInvalidAmi(issues *[]*issue.Issue) {
type AwsInstanceInvalidAMIDetector struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [golint] exported type AwsInstanceInvalidAMIDetector should have comment or be unexported

*Detector
}

func (d *Detector) CreateAwsInstanceInvalidAMIDetector() *AwsInstanceInvalidAMIDetector {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [golint] exported method Detector.CreateAwsInstanceInvalidAMIDetector should have comment or be unexported

return &AwsInstanceInvalidAMIDetector{d}
}

func (d *AwsInstanceInvalidAMIDetector) Detect(issues *[]*issue.Issue) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [golint] exported method AwsInstanceInvalidAMIDetector.Detect should have comment or be unexported

if !d.Config.DeepCheck {
d.Logger.Info("skip this rule.")
return
}

validAmi := map[string]bool{}
resp, err := d.AwsClient.Ec2.DescribeImages(&ec2.DescribeImagesInput{})
if err != nil {
d.Logger.Error(err)
d.Error = true
if d.ResponseCache.DescribeImagesOutput == nil {
resp, err := d.AwsClient.Ec2.DescribeImages(&ec2.DescribeImagesInput{})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [gotype] undeclared name: ec2
  • [gotype] cannot use &(ec2.DescribeImagesInput literal) (value of type *invalid type) as *github.com/aws/aws-sdk-go/service/ec2.DescribeImagesInput value in argument to d.AwsClient.Ec2.DescribeImages

if err != nil {
d.Logger.Error(err)
d.Error = true
}
d.ResponseCache.DescribeImagesOutput = resp
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [gotype] cannot use resp (variable of type *github.com/aws/aws-sdk-go/service/ec2.DescribeImagesOutput) as *invalid type value in assignment

}
for _, image := range resp.Images {
for _, image := range d.ResponseCache.DescribeImagesOutput.Images {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [gotype] invalid operation: d.ResponseCache.DescribeImagesOutput (variable of type *invalid type) has no field or method Images
  • [gotype] cannot range over d.ResponseCache.DescribeImagesOutput.Images (invalid operand)

validAmi[*image.ImageId] = true
}

Expand Down
32 changes: 12 additions & 20 deletions detector/aws_instance_invalid_ami_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/golang/mock/gomock"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/hcl/hcl/parser"
"github.com/wata727/tflint/awsmock"
"github.com/wata727/tflint/config"
"github.com/wata727/tflint/evaluator"
"github.com/wata727/tflint/issue"
)

func TestDetectAwsInstanceInvalidAmi(t *testing.T) {
func TestDetectAwsInstanceInvalidAMI(t *testing.T) {
cases := []struct {
Name string
Src string
Expand Down Expand Up @@ -64,31 +61,26 @@ resource "aws_instance" "web" {
}

for _, tc := range cases {
listMap := make(map[string]*ast.ObjectList)
root, _ := parser.Parse([]byte(tc.Src))
list, _ := root.Node.(*ast.ObjectList)
listMap["test.tf"] = list

c := config.Init()
c.DeepCheck = true
evalConfig, _ := evaluator.NewEvaluator(listMap, config.Init())
d := &Detector{
ListMap: listMap,
EvalConfig: evalConfig,
Config: c,
AwsClient: c.NewAwsClient(),
}

awsClient := c.NewAwsClient()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
iammock := awsmock.NewMockEC2API(ctrl)
iammock.EXPECT().DescribeImages(&ec2.DescribeImagesInput{}).Return(&ec2.DescribeImagesOutput{
ec2mock := awsmock.NewMockEC2API(ctrl)
ec2mock.EXPECT().DescribeImages(&ec2.DescribeImagesInput{}).Return(&ec2.DescribeImagesOutput{
Images: tc.Response,
}, nil)
d.AwsClient.Ec2 = iammock
awsClient.Ec2 = ec2mock

var issues = []*issue.Issue{}
d.DetectAwsInstanceInvalidAmi(&issues)
TestDetectByCreatorName(
"CreateAwsInstanceInvalidAMIDetector",
tc.Src,
c,
awsClient,
&issues,
)

if !reflect.DeepEqual(issues, tc.Issues) {
t.Fatalf("Bad: %s\nExpected: %s\n\ntestcase: %s", issues, tc.Issues, tc.Name)
Expand Down
23 changes: 17 additions & 6 deletions detector/aws_instance_invalid_iam_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,30 @@ import (
"github.com/wata727/tflint/issue"
)

func (d *Detector) DetectAwsInstanceInvalidIamProfile(issues *[]*issue.Issue) {
type AwsInstanceInvalidIAMProfileDetector struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [golint] exported type AwsInstanceInvalidIAMProfileDetector should have comment or be unexported

*Detector
}

func (d *Detector) CreateAwsInstanceInvalidIAMProfileDetector() *AwsInstanceInvalidIAMProfileDetector {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [golint] exported method Detector.CreateAwsInstanceInvalidIAMProfileDetector should have comment or be unexported

return &AwsInstanceInvalidIAMProfileDetector{d}
}

func (d *AwsInstanceInvalidIAMProfileDetector) Detect(issues *[]*issue.Issue) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [golint] exported method AwsInstanceInvalidIAMProfileDetector.Detect should have comment or be unexported

if !d.Config.DeepCheck {
d.Logger.Info("skip this rule.")
return
}

validIamProfiles := map[string]bool{}
resp, err := d.AwsClient.Iam.ListInstanceProfiles(&iam.ListInstanceProfilesInput{})
if err != nil {
d.Logger.Error(err)
d.Error = true
if d.ResponseCache.ListInstanceProfilesOutput == nil {
resp, err := d.AwsClient.Iam.ListInstanceProfiles(&iam.ListInstanceProfilesInput{})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [gotype] undeclared name: iam
  • [gotype] cannot use &(iam.ListInstanceProfilesInput literal) (value of type *invalid type) as *github.com/aws/aws-sdk-go/service/iam.ListInstanceProfilesInput value in argument to d.AwsClient.Iam.ListInstanceProfiles

if err != nil {
d.Logger.Error(err)
d.Error = true
}
d.ResponseCache.ListInstanceProfilesOutput = resp
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [gotype] cannot use resp (variable of type *github.com/aws/aws-sdk-go/service/iam.ListInstanceProfilesOutput) as *invalid type value in assignment

}
for _, iamProfile := range resp.InstanceProfiles {
for _, iamProfile := range d.ResponseCache.ListInstanceProfilesOutput.InstanceProfiles {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [gotype] invalid operation: d.ResponseCache.ListInstanceProfilesOutput (variable of type *invalid type) has no field or method InstanceProfiles
  • [gotype] cannot range over d.ResponseCache.ListInstanceProfilesOutput.InstanceProfiles (invalid operand)

validIamProfiles[*iamProfile.InstanceProfileName] = true
}

Expand Down
Loading