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

provider/archive support folders in output_path #8278

Merged
merged 3 commits into from
Aug 18, 2016
Merged
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
37 changes: 23 additions & 14 deletions builtin/providers/archive/resource_archive_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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
}
Expand Down
20 changes: 18 additions & 2 deletions builtin/providers/archive/resource_archive_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
),
},
},
})
}
Expand Down Expand Up @@ -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"
Expand Down