-
Notifications
You must be signed in to change notification settings - Fork 13
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 #11 from pd/walkmodulecalls
Add rule demonstrating use of `WalkModuleCalls`
- Loading branch information
Showing
5 changed files
with
149 additions
and
6 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
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,56 @@ | ||
package rules | ||
|
||
import ( | ||
"github.com/hashicorp/go-version" | ||
hcl "github.com/hashicorp/hcl/v2" | ||
"github.com/terraform-linters/tflint-plugin-sdk/terraform" | ||
"github.com/terraform-linters/tflint-plugin-sdk/tflint" | ||
) | ||
|
||
// ModuleCallValidityRule checks whether ... | ||
type ModuleCallValidityRule struct{} | ||
|
||
// NewModuleCallValidityRule returns a new rule | ||
func NewModuleCallValidityRule() *ModuleCallValidityRule { | ||
return &ModuleCallValidityRule{} | ||
} | ||
|
||
// Name returns the rule name | ||
func (r *ModuleCallValidityRule) Name() string { | ||
return "module_call_validity" | ||
} | ||
|
||
// Enabled returns whether the rule is enabled by default | ||
func (r *ModuleCallValidityRule) Enabled() bool { | ||
return true | ||
} | ||
|
||
// Severity returns the rule severity | ||
func (r *ModuleCallValidityRule) Severity() string { | ||
return tflint.ERROR | ||
} | ||
|
||
// Link returns the rule reference link | ||
func (r *ModuleCallValidityRule) Link() string { | ||
return "" | ||
} | ||
|
||
// Check checks whether ... | ||
func (r *ModuleCallValidityRule) Check(runner tflint.Runner) error { | ||
return runner.WalkModuleCalls(func(call *terraform.ModuleCall) error { | ||
if call.SourceAddr != "acceptable/source" { | ||
return runner.EmitIssue(r, "unacceptable module source", call.SourceAddrRange) | ||
} | ||
|
||
if len(call.Providers) != 0 { | ||
return runner.EmitIssue(r, "must not pass providers", hcl.Range{}) | ||
} | ||
|
||
expectedVersion, _ := version.NewVersion("0.1.0") | ||
if !call.Version.Required.Check(expectedVersion) { | ||
return runner.EmitIssue(r, "must accept version 0.1.0", call.Version.DeclRange) | ||
} | ||
|
||
return nil | ||
}) | ||
} |
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,83 @@ | ||
package rules | ||
|
||
import ( | ||
"testing" | ||
|
||
hcl "github.com/hashicorp/hcl/v2" | ||
"github.com/terraform-linters/tflint-plugin-sdk/helper" | ||
) | ||
|
||
func Test_ModuleCallValidity(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Content string | ||
Expected helper.Issues | ||
}{ | ||
{ | ||
Name: "module source issue", | ||
Content: ` | ||
module "foo" { | ||
source = "in/correct" | ||
}`, | ||
Expected: helper.Issues{ | ||
{ | ||
Rule: NewModuleCallValidityRule(), | ||
Message: "unacceptable module source", | ||
Range: hcl.Range{ | ||
Filename: "resource.tf", | ||
Start: hcl.Pos{Line: 3, Column: 12}, | ||
End: hcl.Pos{Line: 3, Column: 24}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
{ | ||
Name: "providers issue", | ||
Content: ` | ||
module "foo" { | ||
source = "acceptable/source" | ||
providers = { aws.dns = aws.east } | ||
}`, | ||
Expected: helper.Issues{ | ||
{ | ||
Rule: NewModuleCallValidityRule(), | ||
Message: "must not pass providers", | ||
Range: hcl.Range{}, | ||
}, | ||
}, | ||
}, | ||
|
||
{ | ||
Name: "version constraint", | ||
Content: ` | ||
module "foo" { | ||
source = "acceptable/source" | ||
version = ">= 1.0.0" | ||
}`, | ||
Expected: helper.Issues{ | ||
{ | ||
Rule: NewModuleCallValidityRule(), | ||
Message: "must accept version 0.1.0", | ||
Range: hcl.Range{ | ||
Filename: "resource.tf", | ||
Start: hcl.Pos{Line: 4, Column: 13}, | ||
End: hcl.Pos{Line: 4, Column: 23}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
rule := NewModuleCallValidityRule() | ||
|
||
for _, tc := range cases { | ||
runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content}) | ||
|
||
if err := rule.Check(runner); err != nil { | ||
t.Fatalf("Unexpected error occurred: %s", err) | ||
} | ||
|
||
helper.AssertIssues(t, tc.Expected, runner.Issues) | ||
} | ||
} |