forked from nimbolus/terraform-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor preview runner to use new backend capability
- Loading branch information
Showing
5 changed files
with
210 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package terraform | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclparse" | ||
) | ||
|
||
var rootSchema = &hcl.BodySchema{ | ||
Blocks: []hcl.BlockHeaderSchema{ | ||
{ | ||
Type: "terraform", | ||
LabelNames: nil, | ||
}, | ||
}, | ||
} | ||
|
||
var terraformBlockSchema = &hcl.BodySchema{ | ||
Blocks: []hcl.BlockHeaderSchema{ | ||
{ | ||
Type: "backend", | ||
LabelNames: []string{"name"}, | ||
}, | ||
}, | ||
} | ||
|
||
var backendSchema = &hcl.BodySchema{ | ||
Attributes: []hcl.AttributeSchema{ | ||
{Name: "address"}, | ||
{Name: "username"}, | ||
{Name: "password"}, | ||
}, | ||
} | ||
|
||
func files(dir string) ([]string, error) { | ||
infos, err := os.ReadDir(dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var files []string | ||
for _, info := range infos { | ||
if info.IsDir() { | ||
continue | ||
} | ||
|
||
name := info.Name() | ||
ext := filepath.Ext(name) | ||
if ext != ".tf" { | ||
continue | ||
} | ||
|
||
fullPath := filepath.Join(dir, name) | ||
files = append(files, fullPath) | ||
} | ||
|
||
return files, nil | ||
} | ||
|
||
type BackendConfig struct { | ||
Address string | ||
Username string | ||
Password string | ||
} | ||
|
||
func readAttribute(attrs hcl.Attributes, name string) (string, error) { | ||
raw, ok := attrs[name] | ||
if !ok { | ||
return "", nil | ||
} | ||
|
||
val, err := raw.Expr.Value(nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return val.AsString(), nil | ||
} | ||
|
||
func FindBackend(dir string) (*BackendConfig, error) { | ||
parser := hclparse.NewParser() | ||
|
||
tfFiles, err := files(dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var file *hcl.File | ||
for _, filename := range tfFiles { | ||
b, err := os.ReadFile(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
file, _ = parser.ParseHCL(b, filename) | ||
if file == nil { | ||
continue | ||
} | ||
|
||
content, _, _ := file.Body.PartialContent(rootSchema) | ||
for _, block := range content.Blocks { | ||
if block.Type != "terraform" { | ||
continue | ||
} | ||
|
||
content, _, _ := block.Body.PartialContent(terraformBlockSchema) | ||
for _, innerBlock := range content.Blocks { | ||
if innerBlock.Type != "backend" { | ||
continue | ||
} | ||
if innerBlock.Labels[0] != "http" { | ||
continue | ||
} | ||
|
||
content, _, _ := innerBlock.Body.PartialContent(backendSchema) | ||
address, err := readAttribute(content.Attributes, "address") | ||
if err != nil { | ||
return nil, err | ||
} | ||
username, err := readAttribute(content.Attributes, "username") | ||
if err != nil { | ||
return nil, err | ||
} | ||
password, err := readAttribute(content.Attributes, "password") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &BackendConfig{ | ||
Address: address, | ||
Username: username, | ||
Password: password, | ||
}, nil | ||
} | ||
} | ||
} | ||
|
||
return nil, errors.New("backend config not found") | ||
} |
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,18 @@ | ||
package terraform_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ffddorf/tf-preview-github/pkg/terraform" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFindBackend(t *testing.T) { | ||
be, err := terraform.FindBackend("./testdata") | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, "https://dummy-backend.example.com/state", be.Address) | ||
assert.Equal(t, "my_user", be.Username) | ||
assert.Empty(t, be.Password) | ||
} |
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,3 @@ | ||
locals { | ||
foo = "bar" | ||
} |
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,8 @@ | ||
terraform { | ||
backend "http" { | ||
address = "https://dummy-backend.example.com/state" | ||
lock_address = "https://dummy-backend.example.com/state" | ||
unlock_address = "https://dummy-backend.example.com/state" | ||
username = "my_user" | ||
} | ||
} |