generated from terraform-linters/tflint-ruleset-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
138 additions
and
265 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,51 @@ | ||
package rules | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/terraform-linters/tflint-plugin-sdk/hclext" | ||
"github.com/terraform-linters/tflint-plugin-sdk/tflint" | ||
) | ||
|
||
const ( | ||
requiredBackendType = "azurerm" | ||
requiredYCBackendType = "s3" | ||
backendTypeMessageTemplate = "backend type should be \"%s\" but defined: \"%s\"" | ||
) | ||
|
||
func NewBackendTypeRule() *Rule { | ||
return NewRule( | ||
"backend_type", | ||
func(runner tflint.Runner, rule tflint.Rule) error { | ||
body, err := runner.GetModuleContent( | ||
&hclext.BodySchema{ | ||
Blocks: []hclext.BlockSchema{ | ||
{Type: "backend", LabelNames: []string{"type"}, Body: &hclext.BodySchema{ | ||
Attributes: []hclext.AttributeSchema{{Name: "key"}}, | ||
}}, | ||
}, | ||
}, | ||
&tflint.GetModuleContentOption{}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, backend := range body.Blocks { | ||
if backend.Type != requiredBackendType || backend.Labels[0] != requiredYCBackendType { | ||
return runner.EmitIssue( | ||
rule, | ||
fmt.Sprintf( | ||
backendTypeMessageTemplate, | ||
requiredBackendType, | ||
backend.Type, | ||
), | ||
backend.DefRange, | ||
) | ||
} | ||
} | ||
|
||
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,70 @@ | ||
package rules | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
hcl "github.com/hashicorp/hcl/v2" | ||
"github.com/stretchr/testify/require" | ||
"github.com/terraform-linters/tflint-plugin-sdk/helper" | ||
) | ||
|
||
func Test_BackendType(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Content string | ||
Expected helper.Issues | ||
}{ | ||
{ | ||
Name: "no issues", | ||
Content: ` | ||
terraform { | ||
backend "azurerm" { | ||
resource_group_name = "rg" | ||
storage_account_name = "sa" | ||
container_name = "tfstate" | ||
key = "path/to/my/key" | ||
} | ||
}`, | ||
Expected: helper.Issues{}, | ||
}, | ||
{ | ||
Name: "issue found", | ||
Content: ` | ||
terraform { | ||
backend "s3" { | ||
bucket = "mybucket" | ||
key = "path/to/my/key" | ||
region = "us-east-1" | ||
} | ||
}`, | ||
Expected: helper.Issues{ | ||
{ | ||
Rule: NewBackendTypeRule(), | ||
Message: fmt.Sprintf( | ||
backendTypeMessageTemplate, | ||
requiredBackendType, | ||
"s3", | ||
), | ||
Range: hcl.Range{ | ||
Filename: filename, | ||
Start: hcl.Pos{Line: 3, Column: 3}, | ||
End: hcl.Pos{Line: 3, Column: 15}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
rule := NewBackendTypeRule() | ||
|
||
for _, tc := range cases { | ||
tc := tc | ||
t.Run(tc.Name, func(t *testing.T) { | ||
runner := helper.TestRunner(t, map[string]string{filename: tc.Content}) | ||
|
||
require.NoError(t, rule.Check(runner)) | ||
helper.AssertIssues(t, tc.Expected, runner.Issues) | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.