Skip to content

Commit cb491c4

Browse files
nicolai86stack72
authored andcommitted
provider/archive support folders in output_path (hashicorp#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
1 parent faf0939 commit cb491c4

2 files changed

Lines changed: 41 additions & 16 deletions

File tree

builtin/providers/archive/resource_archive_file.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"crypto/sha1"
55
"encoding/hex"
66
"fmt"
7-
"github.com/hashicorp/terraform/helper/schema"
87
"io/ioutil"
98
"os"
9+
"path"
10+
11+
"github.com/hashicorp/terraform/helper/schema"
1012
)
1113

1214
func resourceArchiveFile() *schema.Resource {
@@ -74,15 +76,15 @@ func resourceArchiveFileCreate(d *schema.ResourceData, meta interface{}) error {
7476
}
7577

7678
func resourceArchiveFileRead(d *schema.ResourceData, meta interface{}) error {
77-
output_path := d.Get("output_path").(string)
78-
fi, err := os.Stat(output_path)
79+
outputPath := d.Get("output_path").(string)
80+
fi, err := os.Stat(outputPath)
7981
if os.IsNotExist(err) {
8082
d.SetId("")
8183
d.MarkNewResource()
8284
return nil
8385
}
8486

85-
sha, err := genFileSha1(fi.Name())
87+
sha, err := genFileSha1(outputPath)
8688
if err != nil {
8789
return fmt.Errorf("could not generate file checksum sha: %s", err)
8890
}
@@ -97,6 +99,15 @@ func resourceArchiveFileUpdate(d *schema.ResourceData, meta interface{}) error {
9799
archiveType := d.Get("type").(string)
98100
outputPath := d.Get("output_path").(string)
99101

102+
outputDirectory := path.Dir(outputPath)
103+
if outputDirectory != "" {
104+
if _, err := os.Stat(outputDirectory); err != nil {
105+
if err := os.MkdirAll(outputDirectory, 755); err != nil {
106+
return err
107+
}
108+
}
109+
}
110+
100111
archiver := getArchiver(archiveType, outputPath)
101112
if archiver == nil {
102113
return fmt.Errorf("archive type not supported: %s", archiveType)
@@ -120,13 +131,12 @@ func resourceArchiveFileUpdate(d *schema.ResourceData, meta interface{}) error {
120131
}
121132

122133
// Generate archived file stats
123-
output_path := d.Get("output_path").(string)
124-
fi, err := os.Stat(output_path)
134+
fi, err := os.Stat(outputPath)
125135
if err != nil {
126136
return err
127137
}
128138

129-
sha, err := genFileSha1(fi.Name())
139+
sha, err := genFileSha1(outputPath)
130140
if err != nil {
131141
return fmt.Errorf("could not generate file checksum sha: %s", err)
132142
}
@@ -138,22 +148,21 @@ func resourceArchiveFileUpdate(d *schema.ResourceData, meta interface{}) error {
138148
}
139149

140150
func resourceArchiveFileDelete(d *schema.ResourceData, meta interface{}) error {
141-
output_path := d.Get("output_path").(string)
142-
fi, err := os.Stat(output_path)
143-
if os.IsNotExist(err) {
151+
outputPath := d.Get("output_path").(string)
152+
if _, err := os.Stat(outputPath); os.IsNotExist(err) {
144153
return nil
145154
}
146155

147-
if err := os.Remove(fi.Name()); err != nil {
148-
return fmt.Errorf("could not delete zip file '%s': %s", fi.Name(), err)
156+
if err := os.Remove(outputPath); err != nil {
157+
return fmt.Errorf("could not delete zip file %q: %s", outputPath, err)
149158
}
150159

151160
return nil
152161
}
153162

154163
func resourceArchiveFileExists(d *schema.ResourceData, meta interface{}) (bool, error) {
155-
output_path := d.Get("output_path").(string)
156-
_, err := os.Stat(output_path)
164+
outputPath := d.Get("output_path").(string)
165+
_, err := os.Stat(outputPath)
157166
if os.IsNotExist(err) {
158167
return false, nil
159168
}

builtin/providers/archive/resource_archive_file_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package archive
22

33
import (
44
"fmt"
5-
r "github.com/hashicorp/terraform/helper/resource"
6-
"github.com/hashicorp/terraform/terraform"
75
"os"
86
"testing"
7+
8+
r "github.com/hashicorp/terraform/helper/resource"
9+
"github.com/hashicorp/terraform/terraform"
910
)
1011

1112
func TestAccArchiveFile_Basic(t *testing.T) {
@@ -37,6 +38,12 @@ func TestAccArchiveFile_Basic(t *testing.T) {
3738
r.TestCheckResourceAttrPtr("archive_file.foo", "output_size", &fileSize),
3839
),
3940
},
41+
r.TestStep{
42+
Config: testAccArchiveFileOutputPath,
43+
Check: r.ComposeTestCheckFunc(
44+
testAccArchiveFileExists("example/path/test.zip", &fileSize),
45+
),
46+
},
4047
},
4148
})
4249
}
@@ -75,6 +82,15 @@ resource "archive_file" "foo" {
7582
}
7683
`
7784

85+
var testAccArchiveFileOutputPath = `
86+
resource "archive_file" "foo" {
87+
type = "zip"
88+
source_content = "This is some content"
89+
source_content_filename = "content.txt"
90+
output_path = "example/path/test.zip"
91+
}
92+
`
93+
7894
var testAccArchiveFileFileConfig = `
7995
resource "archive_file" "foo" {
8096
type = "zip"

0 commit comments

Comments
 (0)