provider/archive support folders in output_path (#8278)

* provider/archive: use output_path instead of FileInfo

FileInfo.Name() returns the basename of the output path, which forces you to
never place archives in subdirectories

* provider/archive: add test for subdirectory output_path

* provider/archive: camelCase output_path variable
This commit is contained in:
Raphael Randschau 2016-08-18 17:36:27 +02:00 committed by Paul Stack
parent faf0939d68
commit cb491c408e
2 changed files with 41 additions and 16 deletions

View File

@ -4,9 +4,11 @@ import (
"crypto/sha1"
"encoding/hex"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"io/ioutil"
"os"
"path"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceArchiveFile() *schema.Resource {
@ -74,15 +76,15 @@ func resourceArchiveFileCreate(d *schema.ResourceData, meta interface{}) error {
}
func resourceArchiveFileRead(d *schema.ResourceData, meta interface{}) error {
output_path := d.Get("output_path").(string)
fi, err := os.Stat(output_path)
outputPath := d.Get("output_path").(string)
fi, err := os.Stat(outputPath)
if os.IsNotExist(err) {
d.SetId("")
d.MarkNewResource()
return nil
}
sha, err := genFileSha1(fi.Name())
sha, err := genFileSha1(outputPath)
if err != nil {
return fmt.Errorf("could not generate file checksum sha: %s", err)
}
@ -97,6 +99,15 @@ func resourceArchiveFileUpdate(d *schema.ResourceData, meta interface{}) error {
archiveType := d.Get("type").(string)
outputPath := d.Get("output_path").(string)
outputDirectory := path.Dir(outputPath)
if outputDirectory != "" {
if _, err := os.Stat(outputDirectory); err != nil {
if err := os.MkdirAll(outputDirectory, 755); err != nil {
return err
}
}
}
archiver := getArchiver(archiveType, outputPath)
if archiver == nil {
return fmt.Errorf("archive type not supported: %s", archiveType)
@ -120,13 +131,12 @@ func resourceArchiveFileUpdate(d *schema.ResourceData, meta interface{}) error {
}
// Generate archived file stats
output_path := d.Get("output_path").(string)
fi, err := os.Stat(output_path)
fi, err := os.Stat(outputPath)
if err != nil {
return err
}
sha, err := genFileSha1(fi.Name())
sha, err := genFileSha1(outputPath)
if err != nil {
return fmt.Errorf("could not generate file checksum sha: %s", err)
}
@ -138,22 +148,21 @@ func resourceArchiveFileUpdate(d *schema.ResourceData, meta interface{}) error {
}
func resourceArchiveFileDelete(d *schema.ResourceData, meta interface{}) error {
output_path := d.Get("output_path").(string)
fi, err := os.Stat(output_path)
if os.IsNotExist(err) {
outputPath := d.Get("output_path").(string)
if _, err := os.Stat(outputPath); os.IsNotExist(err) {
return nil
}
if err := os.Remove(fi.Name()); err != nil {
return fmt.Errorf("could not delete zip file '%s': %s", fi.Name(), err)
if err := os.Remove(outputPath); err != nil {
return fmt.Errorf("could not delete zip file %q: %s", outputPath, err)
}
return nil
}
func resourceArchiveFileExists(d *schema.ResourceData, meta interface{}) (bool, error) {
output_path := d.Get("output_path").(string)
_, err := os.Stat(output_path)
outputPath := d.Get("output_path").(string)
_, err := os.Stat(outputPath)
if os.IsNotExist(err) {
return false, nil
}

View File

@ -2,10 +2,11 @@ package archive
import (
"fmt"
r "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"os"
"testing"
r "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccArchiveFile_Basic(t *testing.T) {
@ -37,6 +38,12 @@ func TestAccArchiveFile_Basic(t *testing.T) {
r.TestCheckResourceAttrPtr("archive_file.foo", "output_size", &fileSize),
),
},
r.TestStep{
Config: testAccArchiveFileOutputPath,
Check: r.ComposeTestCheckFunc(
testAccArchiveFileExists("example/path/test.zip", &fileSize),
),
},
},
})
}
@ -75,6 +82,15 @@ resource "archive_file" "foo" {
}
`
var testAccArchiveFileOutputPath = `
resource "archive_file" "foo" {
type = "zip"
source_content = "This is some content"
source_content_filename = "content.txt"
output_path = "example/path/test.zip"
}
`
var testAccArchiveFileFileConfig = `
resource "archive_file" "foo" {
type = "zip"