Skip to content

Commit 5e7a799

Browse files
committed
Add Backend() helper
1 parent a67b0ae commit 5e7a799

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

helper/runner.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,42 @@ func (r *Runner) WalkResources(resourceType string, walker func(*terraform.Resou
135135
return nil
136136
}
137137

138+
// Backend returns the terraform backend configuration.
139+
func (r *Runner) Backend() (*terraform.Backend, error) {
140+
for _, file := range r.Files {
141+
tfcfg, _, diags := file.Body.PartialContent(&hcl.BodySchema{
142+
Blocks: []hcl.BlockHeaderSchema{
143+
{Type: "terraform"},
144+
},
145+
})
146+
if diags.HasErrors() {
147+
return nil, diags
148+
}
149+
150+
for _, block := range tfcfg.Blocks {
151+
backendCfg, _, diags := block.Body.PartialContent(&hcl.BodySchema{
152+
Blocks: []hcl.BlockHeaderSchema{
153+
{Type: "backend", LabelNames: []string{"type"}},
154+
},
155+
})
156+
if diags.HasErrors() {
157+
return nil, diags
158+
}
159+
160+
for _, backendBlock := range backendCfg.Blocks {
161+
return &terraform.Backend{
162+
Type: backendBlock.Labels[0],
163+
TypeRange: backendBlock.LabelRanges[0],
164+
Config: backendBlock.Body,
165+
DeclRange: backendBlock.DefRange,
166+
}, nil
167+
}
168+
}
169+
}
170+
171+
return nil, nil
172+
}
173+
138174
// EvaluateExpr returns a value of the passed expression.
139175
// Note that there is no evaluation, no type conversion, etc.
140176
func (r *Runner) EvaluateExpr(expr hcl.Expression, ret interface{}) error {

helper/runner_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import (
1111
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
1212
)
1313

14+
func Test_satisfyRunnerInterface(t *testing.T) {
15+
var runner tflint.Runner
16+
runner = TestRunner(t, map[string]string{})
17+
runner.EnsureNoError(nil, func() error { return nil })
18+
}
19+
1420
func Test_WalkResourceAttributes(t *testing.T) {
1521
src := `
1622
resource "aws_instance" "foo" {
@@ -309,6 +315,49 @@ func parseExpression(t *testing.T, src string, filename string, pos hcl.Pos) hcl
309315
return expr
310316
}
311317

318+
func Test_Backend(t *testing.T) {
319+
src := `
320+
terraform {
321+
backend "s3" {
322+
bucket = "mybucket"
323+
key = "path/to/my/key"
324+
region = "us-east-1"
325+
}
326+
}`
327+
328+
runner := TestRunner(t, map[string]string{"main.tf": src})
329+
330+
backend, err := runner.Backend()
331+
if err != nil {
332+
t.Fatal(err)
333+
}
334+
335+
expected := &terraform.Backend{
336+
Type: "s3",
337+
TypeRange: hcl.Range{Filename: "main.tf", Start: hcl.Pos{Line: 3, Column: 11}, End: hcl.Pos{Line: 3, Column: 15}},
338+
Config: parseBody(
339+
t,
340+
`bucket = "mybucket"
341+
key = "path/to/my/key"
342+
region = "us-east-1"`,
343+
"main.tf",
344+
hcl.Pos{Line: 4, Column: 5},
345+
hcl.Range{Filename: "main.tf", Start: hcl.Pos{Line: 3, Column: 16}, End: hcl.Pos{Line: 7, Column: 4}},
346+
hcl.Range{Filename: "main.tf", Start: hcl.Pos{Line: 7, Column: 4}, End: hcl.Pos{Line: 7, Column: 4}},
347+
),
348+
DeclRange: hcl.Range{Filename: "main.tf", Start: hcl.Pos{Line: 3, Column: 3}, End: hcl.Pos{Line: 3, Column: 15}},
349+
}
350+
351+
opts := cmp.Options{
352+
cmpopts.IgnoreFields(hclsyntax.LiteralValueExpr{}, "Val"),
353+
cmpopts.IgnoreFields(hcl.Pos{}, "Byte"),
354+
cmpopts.IgnoreUnexported(hclsyntax.Body{}),
355+
}
356+
if !cmp.Equal(expected, backend, opts...) {
357+
t.Fatalf("Diff: %s", cmp.Diff(expected, backend, opts...))
358+
}
359+
}
360+
312361
func Test_EvaluateExpr(t *testing.T) {
313362
src := `
314363
resource "aws_instance" "foo" {

0 commit comments

Comments
 (0)