-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to use a whole local directory as IaC source
- Loading branch information
Elie
committed
Feb 26, 2021
1 parent
e601ccf
commit f74fe3f
Showing
22 changed files
with
340 additions
and
1 deletion.
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,81 @@ | ||
package enumerator | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/cloudskiff/driftctl/pkg/iac/config" | ||
) | ||
|
||
type FileEnumeratorConfig struct { | ||
Bucket *string | ||
Prefix *string | ||
} | ||
|
||
type FileEnumerator struct { | ||
config config.SupplierConfig | ||
} | ||
|
||
func NewFileEnumerator(config config.SupplierConfig) *FileEnumerator { | ||
return &FileEnumerator{ | ||
config, | ||
} | ||
} | ||
|
||
// File enumeration does not follow symlinks. | ||
// We may use something like this https://pkg.go.dev/github.com/facebookgo/symwalk | ||
// to follow links, but it allows infinite loop so be careful ! | ||
// If a symlink is given as root path, we will follow it, but symlinks under this path | ||
// will not be resolved. | ||
func (s *FileEnumerator) Enumerate() ([]string, error) { | ||
path := s.config.Path | ||
info, err := os.Lstat(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// if we got a symlink, use its destination | ||
if info.Mode()&os.ModeSymlink != 0 { | ||
destination, err := filepath.EvalSymlinks(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
path = destination | ||
info, err = os.Stat(destination) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if info != nil && !info.IsDir() { | ||
return []string{path}, nil | ||
} | ||
|
||
keys := make([]string, 0) | ||
|
||
err = filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error { | ||
// Not tested, we should remove a file or folder after WalkDir has enumerated the whole tree in memory | ||
// This edge case does not really need to be covered by tests | ||
if err != nil { | ||
return err | ||
} | ||
// Skip symlinks | ||
if d.Type()&os.ModeSymlink != 0 { | ||
return nil | ||
} | ||
|
||
// Ignore .backup files generated by terraform | ||
if strings.HasSuffix(path, ".backup") { | ||
return nil | ||
} | ||
|
||
if !d.IsDir() { | ||
keys = append(keys, path) | ||
} | ||
return nil | ||
}) | ||
|
||
return keys, err | ||
} |
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,90 @@ | ||
package enumerator | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/cloudskiff/driftctl/pkg/iac/config" | ||
) | ||
|
||
func TestFileEnumerator_Enumerate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
config config.SupplierConfig | ||
want []string | ||
err string | ||
}{ | ||
{ | ||
name: "subfolder nesting", | ||
config: config.SupplierConfig{ | ||
Path: "testdata/states", | ||
}, | ||
want: []string{ | ||
"testdata/states/route53/directory/route53.state", | ||
"testdata/states/s3/terraform.tfstate", | ||
"testdata/states/terraform.tfstate", | ||
}, | ||
}, | ||
{ | ||
name: "symlinked folder", | ||
config: config.SupplierConfig{ | ||
Path: "testdata/symlink", | ||
}, | ||
want: []string{ | ||
"testdata/states/route53/directory/route53.state", | ||
"testdata/states/s3/terraform.tfstate", | ||
"testdata/states/terraform.tfstate", | ||
}, | ||
}, | ||
{ | ||
name: "single state file", | ||
config: config.SupplierConfig{ | ||
Path: "testdata/states/terraform.tfstate", | ||
}, | ||
want: []string{ | ||
"testdata/states/terraform.tfstate", | ||
}, | ||
}, | ||
{ | ||
name: "single symlink state file", | ||
config: config.SupplierConfig{ | ||
Path: "testdata/states/symlink.tfstate", | ||
}, | ||
want: []string{ | ||
"testdata/states/terraform.tfstate", | ||
}, | ||
}, | ||
{ | ||
name: "invalid folder", | ||
config: config.SupplierConfig{ | ||
Path: "/tmp/dummy-folder/that/does/not/exist", | ||
}, | ||
want: nil, | ||
err: "lstat /tmp/dummy-folder/that/does/not/exist: no such file or directory", | ||
}, | ||
{ | ||
name: "invalid symlink", | ||
config: config.SupplierConfig{ | ||
Path: "testdata/invalid_symlink/invalid", | ||
}, | ||
want: nil, | ||
err: "lstat testdata/invalid_symlink/test: no such file or directory", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := NewFileEnumerator(tt.config) | ||
got, err := s.Enumerate() | ||
if err != nil && err.Error() != tt.err { | ||
t.Fatalf("Expected error '%s', got '%s'", tt.err, err.Error()) | ||
} | ||
if err != nil && tt.err == "" { | ||
t.Fatalf("Expected error '%s' but got nil", tt.err) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Enumerate() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
pkg/iac/terraform/state/enumerator/testdata/invalid_symlink/invalid
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 @@ | ||
test |
Empty file.
Empty file.
1 change: 1 addition & 0 deletions
1
pkg/iac/terraform/state/enumerator/testdata/states/symlink-to-s3-folder
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 @@ | ||
s3 |
1 change: 1 addition & 0 deletions
1
pkg/iac/terraform/state/enumerator/testdata/states/symlink.tfstate
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 @@ | ||
terraform.tfstate |
Empty file.
Empty file.
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 @@ | ||
states |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions
1
pkg/iac/terraform/state/testdata/acc/multiple_states_local/.gitignore
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 @@ | ||
states |
38 changes: 38 additions & 0 deletions
38
pkg/iac/terraform/state/testdata/acc/multiple_states_local/route53/.terraform.lock.hcl
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
pkg/iac/terraform/state/testdata/acc/multiple_states_local/route53/terraform.tf
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,25 @@ | ||
provider "aws" { | ||
region = "us-east-1" | ||
} | ||
|
||
terraform { | ||
required_providers { | ||
aws = { | ||
version = "~> 3.19.0" | ||
} | ||
} | ||
|
||
backend "local" { | ||
path = "../states/route53/terraform.tfstate" | ||
} | ||
} | ||
|
||
resource "random_string" "prefix" { | ||
length = 6 | ||
upper = false | ||
special = false | ||
} | ||
|
||
resource "aws_route53_zone" "foobar" { | ||
name = "${random_string.prefix.result}-example.com" | ||
} |
38 changes: 38 additions & 0 deletions
38
pkg/iac/terraform/state/testdata/acc/multiple_states_local/s3/.terraform.lock.hcl
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
pkg/iac/terraform/state/testdata/acc/multiple_states_local/s3/terraform.tf
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,25 @@ | ||
provider "aws" { | ||
region = "us-east-1" | ||
} | ||
|
||
terraform { | ||
required_providers { | ||
aws = { | ||
version = "~> 3.19.0" | ||
} | ||
} | ||
|
||
backend "local" { | ||
path = "../states/s3/terraform.tfstate" | ||
} | ||
} | ||
|
||
resource "random_string" "prefix" { | ||
length = 6 | ||
upper = false | ||
special = false | ||
} | ||
|
||
resource "aws_s3_bucket" "foobar" { | ||
bucket = "${random_string.prefix.result}.driftctl-test.com" | ||
} |