diff --git a/builtin/providers/archive/resource_archive_file.go b/builtin/providers/archive/resource_archive_file.go index 57534eed11ac..b2bd551cca05 100644 --- a/builtin/providers/archive/resource_archive_file.go +++ b/builtin/providers/archive/resource_archive_file.go @@ -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 } diff --git a/builtin/providers/archive/resource_archive_file_test.go b/builtin/providers/archive/resource_archive_file_test.go index eb6c47a9a4b6..d19f8201c57f 100644 --- a/builtin/providers/archive/resource_archive_file_test.go +++ b/builtin/providers/archive/resource_archive_file_test.go @@ -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"