Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support and array of compression formats #29

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions archive/archiver.go

This file was deleted.

105 changes: 78 additions & 27 deletions archive/data_source_archive_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import (
"io/ioutil"
"os"
"path"
"path/filepath"
"reflect"

"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/karrick/godirwalk"
"github.com/mholt/archiver"
)

func dataSourceFile() *schema.Resource {
Expand All @@ -22,14 +26,13 @@ func dataSourceFile() *schema.Resource {

Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Deprecated: "Archive type is now determined from the output_path's file extenstion.",
},
"source": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"content": {
Expand Down Expand Up @@ -89,6 +92,7 @@ func dataSourceFile() *schema.Resource {
"output_path": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"output_size": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -162,49 +166,96 @@ func expandStringList(configured []interface{}) []string {
return vs
}

func checkMatch(fileName string, excludes []string) (value bool) {
for _, exclude := range excludes {
if exclude == "" {
continue
}

if exclude == fileName {
return true
}
}
return false
}

func getFileList(dirName string, excludes []string) ([]string, error) {
var files []string

err := godirwalk.Walk(dirName, &godirwalk.Options{
Callback: func(osPathname string, de *godirwalk.Dirent) error {
relname, err := filepath.Rel(dirName, osPathname)
if err != nil {
return nil
}

if checkMatch(relname, []string{".", ".."}) {
return nil
}

shouldExclude := checkMatch(relname, excludes)
fullName := filepath.FromSlash(fmt.Sprintf("%s/%s", dirName, relname))

if de.IsDir() {
if shouldExclude {
return filepath.SkipDir
}
} else if shouldExclude {
return nil
}

files = append(files, fullName)
return nil
},
})

return files, err
}

func archive(d *schema.ResourceData) error {
archiveType := d.Get("type").(string)
outputPath := d.Get("output_path").(string)
var filesToArchive []string

archiver := getArchiver(archiveType, outputPath)
if archiver == nil {
return fmt.Errorf("archive type not supported: %s", archiveType)
compressor := archiver.MatchingFormat(outputPath)
if compressor == nil {
return fmt.Errorf("cannot compress unsupported file type: %s", outputPath)
}

var err error

if dir, ok := d.GetOk("source_dir"); ok {
var excludeList []string
if excludes, ok := d.GetOk("excludes"); ok {
excludeList := expandStringList(excludes.(*schema.Set).List())
excludeList = expandStringList(excludes.(*schema.Set).List())
}

if err := archiver.ArchiveDir(dir.(string), excludeList); err != nil {
return fmt.Errorf("error archiving directory: %s", err)
}
} else {
if err := archiver.ArchiveDir(dir.(string), []string{""}); err != nil {
return fmt.Errorf("error archiving directory: %s", err)
}
filesToArchive, err = getFileList(dir.(string), excludeList)
if err != nil {
return fmt.Errorf("could not walk dir: %s", dir.(string))
}
} else if file, ok := d.GetOk("source_file"); ok {
if err := archiver.ArchiveFile(file.(string)); err != nil {
return fmt.Errorf("error archiving file: %s", err)
}
} else if filename, ok := d.GetOk("source_content_filename"); ok {
content := d.Get("source_content").(string)
if err := archiver.ArchiveContent([]byte(content), filename.(string)); err != nil {
return fmt.Errorf("error archiving content: %s", err)
}
filesToArchive = append(filesToArchive, file.(string))
} else if fileName, ok := d.GetOk("source_content_filename"); ok {
return fmt.Errorf("source_content not supported for %s", fileName)
} else if v, ok := d.GetOk("source"); ok {
vL := v.(*schema.Set).List()
content := make(map[string][]byte)
for _, v := range vL {
src := v.(map[string]interface{})
content[src["filename"].(string)] = []byte(src["content"].(string))
}
if err := archiver.ArchiveMultiple(content); err != nil {
return fmt.Errorf("error archiving content: %s", err)
}

keys := reflect.ValueOf(content).MapKeys()
return fmt.Errorf("cannot compress %d source blocks", len(keys))
} else {
return fmt.Errorf("one of 'source_dir', 'source_file', 'source_content_filename' must be specified")
}

check := compressor.Make(outputPath, filesToArchive)
if check != nil {
return fmt.Errorf("could not archive to %s: %s", outputPath, check)
}

return nil
}

Expand Down
70 changes: 38 additions & 32 deletions archive/data_source_archive_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestAccArchiveFile_Basic(t *testing.T) {
func TestAccArchiveFile_SourceContent(t *testing.T) {
var fileSize string
r.Test(t, r.TestCase{
Providers: testProviders,
Steps: []r.TestStep{
{
Config: testAccArchiveFileContentConfig,
Config: testAccArchiveFileOutputPath,
Check: r.ComposeTestCheckFunc(
testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
Expand All @@ -37,40 +37,61 @@ func TestAccArchiveFile_Basic(t *testing.T) {
),
),
},
},
})
}

func TestAccArchiveFile_SourceDir(t *testing.T) {
var fileSize string
r.Test(t, r.TestCase{
Providers: testProviders,
Steps: []r.TestStep{
{
Config: testAccArchiveFileFileConfig,
Config: testAccArchiveFileDirConfig,
Check: r.ComposeTestCheckFunc(
testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
),
},
{
Config: testAccArchiveFileDirConfig,
Config: testAccArchiveFileDirExcludesConfig,
Check: r.ComposeTestCheckFunc(
testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
),
},
},
})
}

func TestAccArchiveFile_SourceFile(t *testing.T) {
var fileSize string
r.Test(t, r.TestCase{
Providers: testProviders,
Steps: []r.TestStep{
{
Config: testAccArchiveFileDirExcludesConfig,
Config: testAccArchiveFileFileConfig,
Check: r.ComposeTestCheckFunc(
testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
),
},
},
})
}

func TestAccArchiveFile_SourceContentBlocks(t *testing.T) {
var fileSize string
r.Test(t, r.TestCase{
Providers: testProviders,
Steps: []r.TestStep{
{
Config: testAccArchiveFileMultiConfig,
Check: r.ComposeTestCheckFunc(
testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
),
},
{
Config: testAccArchiveFileOutputPath,
Check: r.ComposeTestCheckFunc(
testAccArchiveFileExists(fmt.Sprintf("%s/test.zip", tmpDir), &fileSize),
),
},
},
})
}
Expand All @@ -87,57 +108,42 @@ func testAccArchiveFileExists(filename string, fileSize *string) r.TestCheckFunc
}
}

var testAccArchiveFileContentConfig = `
var testAccArchiveFileOutputPath = `
data "archive_file" "foo" {
type = "zip"
source_content = "This is some content"
source_content_filename = "content.txt"
output_path = "zip_file_acc_test.zip"
}
`

var tmpDir = os.TempDir() + "/test"
var testAccArchiveFileOutputPath = fmt.Sprintf(`
data "archive_file" "foo" {
type = "zip"
source_content = "This is some content"
source_content_filename = "content.txt"
output_path = "%s/test.zip"
}
`, tmpDir)

var testAccArchiveFileFileConfig = `
data "archive_file" "foo" {
type = "zip"
source_file = "test-fixtures/test-file.txt"
output_path = "zip_file_acc_test.zip"
}
`

var testAccArchiveFileDirConfig = `
data "archive_file" "foo" {
type = "zip"
source_dir = "test-fixtures/test-dir"
output_path = "zip_file_acc_test.zip"
}
`

var testAccArchiveFileDirExcludesConfig = `
data "archive_file" "foo" {
type = "zip"
source_dir = "../archive/test-fixtures/../test-fixtures/test-dir"
source_dir = "test-fixtures/test-dir"
excludes = ["test-fixtures/test-dir/file2.txt"]
output_path = "zip_file_acc_test.zip"
}
`

var testAccArchiveFileMultiConfig = `
data "archive_file" "foo" {
type = "zip"
output_path = "zip_file_acc_test.zip"
source {
filename = "content.txt"
content = "This is some content"
}
output_path = "zip_file_acc_test.zip"
filename = "content.txt"
content = "This is some content"
}
}
`
Loading