From 89c6db7529f7afa7842da6391e31e6dd4ca94f6f Mon Sep 17 00:00:00 2001 From: Raphael Randschau Date: Thu, 18 Aug 2016 08:45:06 +0200 Subject: [PATCH 1/3] 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 --- .../archive/resource_archive_file.go | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/builtin/providers/archive/resource_archive_file.go b/builtin/providers/archive/resource_archive_file.go index 57534eed11ac..3c93f19d54b9 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 { @@ -82,7 +84,7 @@ func resourceArchiveFileRead(d *schema.ResourceData, meta interface{}) error { return nil } - sha, err := genFileSha1(fi.Name()) + sha, err := genFileSha1(output_path) if err != nil { return fmt.Errorf("could not generate file checksum sha: %s", err) } @@ -95,9 +97,9 @@ func resourceArchiveFileRead(d *schema.ResourceData, meta interface{}) error { func resourceArchiveFileUpdate(d *schema.ResourceData, meta interface{}) error { archiveType := d.Get("type").(string) - outputPath := d.Get("output_path").(string) + output_path := d.Get("output_path").(string) - archiver := getArchiver(archiveType, outputPath) + archiver := getArchiver(archiveType, output_path) if archiver == nil { return fmt.Errorf("archive type not supported: %s", archiveType) } @@ -120,13 +122,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) if err != nil { return err } - sha, err := genFileSha1(fi.Name()) + sha, err := genFileSha1(output_path) if err != nil { return fmt.Errorf("could not generate file checksum sha: %s", err) } @@ -139,13 +140,12 @@ 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) { + if _, err := os.Stat(output_path); 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(output_path); err != nil { + return fmt.Errorf("could not delete zip file %q: %s", output_path, err) } return nil From f71d6269114d4f669e071aedcd85d4dd5c9d820d Mon Sep 17 00:00:00 2001 From: Raphael Randschau Date: Thu, 18 Aug 2016 08:45:43 +0200 Subject: [PATCH 2/3] provider/archive: add test for subdirectory output_path --- .../archive/resource_archive_file.go | 9 +++++++++ .../archive/resource_archive_file_test.go | 20 +++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/builtin/providers/archive/resource_archive_file.go b/builtin/providers/archive/resource_archive_file.go index 3c93f19d54b9..4f67b1ffbc22 100644 --- a/builtin/providers/archive/resource_archive_file.go +++ b/builtin/providers/archive/resource_archive_file.go @@ -99,6 +99,15 @@ func resourceArchiveFileUpdate(d *schema.ResourceData, meta interface{}) error { archiveType := d.Get("type").(string) output_path := d.Get("output_path").(string) + outputDirectory := path.Dir(output_path) + if outputDirectory != "" { + if _, err := os.Stat(outputDirectory); err != nil { + if err := os.MkdirAll(outputDirectory, 755); err != nil { + return err + } + } + } + archiver := getArchiver(archiveType, output_path) if archiver == nil { return fmt.Errorf("archive type not supported: %s", archiveType) 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" From a21260a65b44a75e285ee7b59f6b7aa8231b4f4c Mon Sep 17 00:00:00 2001 From: Raphael Randschau Date: Thu, 18 Aug 2016 16:59:05 +0200 Subject: [PATCH 3/3] provider/archive: camelCase output_path variable --- .../archive/resource_archive_file.go | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/builtin/providers/archive/resource_archive_file.go b/builtin/providers/archive/resource_archive_file.go index 4f67b1ffbc22..b2bd551cca05 100644 --- a/builtin/providers/archive/resource_archive_file.go +++ b/builtin/providers/archive/resource_archive_file.go @@ -76,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(output_path) + sha, err := genFileSha1(outputPath) if err != nil { return fmt.Errorf("could not generate file checksum sha: %s", err) } @@ -97,9 +97,9 @@ func resourceArchiveFileRead(d *schema.ResourceData, meta interface{}) error { func resourceArchiveFileUpdate(d *schema.ResourceData, meta interface{}) error { archiveType := d.Get("type").(string) - output_path := d.Get("output_path").(string) + outputPath := d.Get("output_path").(string) - outputDirectory := path.Dir(output_path) + outputDirectory := path.Dir(outputPath) if outputDirectory != "" { if _, err := os.Stat(outputDirectory); err != nil { if err := os.MkdirAll(outputDirectory, 755); err != nil { @@ -108,7 +108,7 @@ func resourceArchiveFileUpdate(d *schema.ResourceData, meta interface{}) error { } } - archiver := getArchiver(archiveType, output_path) + archiver := getArchiver(archiveType, outputPath) if archiver == nil { return fmt.Errorf("archive type not supported: %s", archiveType) } @@ -131,12 +131,12 @@ func resourceArchiveFileUpdate(d *schema.ResourceData, meta interface{}) error { } // Generate archived file stats - fi, err := os.Stat(output_path) + fi, err := os.Stat(outputPath) if err != nil { return err } - sha, err := genFileSha1(output_path) + sha, err := genFileSha1(outputPath) if err != nil { return fmt.Errorf("could not generate file checksum sha: %s", err) } @@ -148,21 +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) - if _, err := os.Stat(output_path); os.IsNotExist(err) { + outputPath := d.Get("output_path").(string) + if _, err := os.Stat(outputPath); os.IsNotExist(err) { return nil } - if err := os.Remove(output_path); err != nil { - return fmt.Errorf("could not delete zip file %q: %s", output_path, 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 }