Skip to content

Commit 3442a96

Browse files
authored
Merge pull request #48 from terraform-linters/tweaks_custom_type_checks
Make custom-* valid custom type
2 parents 3a478cc + f336b5a commit 3442a96

8 files changed

+20
-45
lines changed

rules/google_composer_environment_invalid_machine_type.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package rules
22

33
import (
44
"fmt"
5-
"strings"
65

76
hcl "github.com/hashicorp/hcl/v2"
87
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
@@ -64,11 +63,7 @@ func (r *GoogleComposerEnvironmentInvalidMachineTypeRule) Check(runner tflint.Ru
6463
err := runner.EvaluateExpr(attribute.Expr, &machineType)
6564

6665
err = runner.EnsureNoError(err, func() error {
67-
if validMachineTypes[machineType] ||
68-
strings.HasPrefix(machineType, "e2-custom-") ||
69-
strings.HasPrefix(machineType, "n2-custom-") ||
70-
strings.HasPrefix(machineType, "n2d-custom-") ||
71-
strings.HasPrefix(machineType, "n1-custom-") {
66+
if validMachineTypes[machineType] || isCustomType(machineType) {
7267
return nil
7368
}
7469

rules/google_compute_instance_invalid_machine_type.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package rules
22

33
import (
44
"fmt"
5-
"strings"
65

76
hcl "github.com/hashicorp/hcl/v2"
87
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
98
)
109

1110
// GoogleComputeInstanceInvalidMachineTypeRule checks whether the machine type is invalid
12-
type GoogleComputeInstanceInvalidMachineTypeRule struct {}
11+
type GoogleComputeInstanceInvalidMachineTypeRule struct{}
1312

1413
// NewGoogleComputeInstanceInvalidMachineTypeRule returns a new rule
1514
func NewGoogleComputeInstanceInvalidMachineTypeRule() *GoogleComputeInstanceInvalidMachineTypeRule {
@@ -43,11 +42,7 @@ func (r *GoogleComputeInstanceInvalidMachineTypeRule) Check(runner tflint.Runner
4342
err := runner.EvaluateExpr(attribute.Expr, &machineType)
4443

4544
return runner.EnsureNoError(err, func() error {
46-
if validMachineTypes[machineType] ||
47-
strings.HasPrefix(machineType, "e2-custom-") ||
48-
strings.HasPrefix(machineType, "n2-custom-") ||
49-
strings.HasPrefix(machineType, "n2d-custom-") ||
50-
strings.HasPrefix(machineType, "n1-custom-") {
45+
if validMachineTypes[machineType] || isCustomType(machineType) {
5146
return nil
5247
}
5348

rules/google_compute_instance_template_invalid_machine_type.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package rules
22

33
import (
44
"fmt"
5-
"strings"
65

76
hcl "github.com/hashicorp/hcl/v2"
87
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
98
)
109

1110
// GoogleComputeInstanceTemplateInvalidMachineTypeRule checks whether the machine type is invalid
12-
type GoogleComputeInstanceTemplateInvalidMachineTypeRule struct {}
11+
type GoogleComputeInstanceTemplateInvalidMachineTypeRule struct{}
1312

1413
// NewGoogleComputeInstanceTemplateInvalidMachineTypeRule returns a new rule
1514
func NewGoogleComputeInstanceTemplateInvalidMachineTypeRule() *GoogleComputeInstanceTemplateInvalidMachineTypeRule {
@@ -43,11 +42,7 @@ func (r *GoogleComputeInstanceTemplateInvalidMachineTypeRule) Check(runner tflin
4342
err := runner.EvaluateExpr(attribute.Expr, &machineType)
4443

4544
return runner.EnsureNoError(err, func() error {
46-
if validMachineTypes[machineType] ||
47-
strings.HasPrefix(machineType, "e2-custom-") ||
48-
strings.HasPrefix(machineType, "n2-custom-") ||
49-
strings.HasPrefix(machineType, "n2d-custom-") ||
50-
strings.HasPrefix(machineType, "n1-custom-") {
45+
if validMachineTypes[machineType] || isCustomType(machineType) {
5146
return nil
5247
}
5348

rules/google_compute_reservation_invalid_machine_type.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package rules
22

33
import (
44
"fmt"
5-
"strings"
65

76
hcl "github.com/hashicorp/hcl/v2"
87
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
@@ -64,11 +63,7 @@ func (r *GoogleComputeReservationInvalidMachineTypeRule) Check(runner tflint.Run
6463
err := runner.EvaluateExpr(attribute.Expr, &machineType)
6564

6665
err = runner.EnsureNoError(err, func() error {
67-
if validMachineTypes[machineType] ||
68-
strings.HasPrefix(machineType, "e2-custom-") ||
69-
strings.HasPrefix(machineType, "n2-custom-") ||
70-
strings.HasPrefix(machineType, "n2d-custom-") ||
71-
strings.HasPrefix(machineType, "n1-custom-") {
66+
if validMachineTypes[machineType] || isCustomType(machineType) {
7267
return nil
7368
}
7469

rules/google_container_cluster_invalid_machine_type.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package rules
22

33
import (
44
"fmt"
5-
"strings"
65

76
hcl "github.com/hashicorp/hcl/v2"
87
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
@@ -53,11 +52,7 @@ func (r *GoogleContainerClusterInvalidMachineTypeRule) Check(runner tflint.Runne
5352
err := runner.EvaluateExpr(attribute.Expr, &machineType)
5453

5554
return runner.EnsureNoError(err, func() error {
56-
if validMachineTypes[machineType] ||
57-
strings.HasPrefix(machineType, "e2-custom-") ||
58-
strings.HasPrefix(machineType, "n2-custom-") ||
59-
strings.HasPrefix(machineType, "n2d-custom-") ||
60-
strings.HasPrefix(machineType, "n1-custom-") {
55+
if validMachineTypes[machineType] || isCustomType(machineType) {
6156
return nil
6257
}
6358

rules/google_container_node_pool_invalid_machine_type.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package rules
22

33
import (
44
"fmt"
5-
"strings"
65

76
hcl "github.com/hashicorp/hcl/v2"
87
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
@@ -53,11 +52,7 @@ func (r *GoogleContainerNodePoolInvalidMachineTypeRule) Check(runner tflint.Runn
5352
err := runner.EvaluateExpr(attribute.Expr, &machineType)
5453

5554
return runner.EnsureNoError(err, func() error {
56-
if validMachineTypes[machineType] ||
57-
strings.HasPrefix(machineType, "e2-custom-") ||
58-
strings.HasPrefix(machineType, "n2-custom-") ||
59-
strings.HasPrefix(machineType, "n2d-custom-") ||
60-
strings.HasPrefix(machineType, "n1-custom-") {
55+
if validMachineTypes[machineType] || isCustomType(machineType) {
6156
return nil
6257
}
6358

rules/google_dataflow_job_invalid_machine_type.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package rules
22

33
import (
44
"fmt"
5-
"strings"
65

76
hcl "github.com/hashicorp/hcl/v2"
87
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
98
)
109

1110
// GoogleDataflowJobInvalidMachineTypeRule checks whether the machine type is invalid
12-
type GoogleDataflowJobInvalidMachineTypeRule struct {}
11+
type GoogleDataflowJobInvalidMachineTypeRule struct{}
1312

1413
// NewGoogleDataflowJobInvalidMachineTypeRule returns a new rule
1514
func NewGoogleDataflowJobInvalidMachineTypeRule() *GoogleDataflowJobInvalidMachineTypeRule {
@@ -43,11 +42,7 @@ func (r *GoogleDataflowJobInvalidMachineTypeRule) Check(runner tflint.Runner) er
4342
err := runner.EvaluateExpr(attribute.Expr, &machineType)
4443

4544
return runner.EnsureNoError(err, func() error {
46-
if validMachineTypes[machineType] ||
47-
strings.HasPrefix(machineType, "e2-custom-") ||
48-
strings.HasPrefix(machineType, "n2-custom-") ||
49-
strings.HasPrefix(machineType, "n2d-custom-") ||
50-
strings.HasPrefix(machineType, "n1-custom-") {
45+
if validMachineTypes[machineType] || isCustomType(machineType) {
5146
return nil
5247
}
5348

rules/utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package rules
22

3+
import "strings"
4+
35
var validMachineTypes = map[string]bool{
46
"e2-standard-2": true,
57
"e2-standard-4": true,
@@ -114,3 +116,11 @@ var validMachineTypes = map[string]bool{
114116
"f1-micro": true,
115117
"g1-small": true,
116118
}
119+
120+
func isCustomType(machineType string) bool {
121+
return strings.HasPrefix(machineType, "e2-custom-") ||
122+
strings.HasPrefix(machineType, "n2-custom-") ||
123+
strings.HasPrefix(machineType, "n2d-custom-") ||
124+
strings.HasPrefix(machineType, "n1-custom-") ||
125+
strings.HasPrefix(machineType, "custom-")
126+
}

0 commit comments

Comments
 (0)