@@ -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
1214func resourceArchiveFile () * schema.Resource {
@@ -74,15 +76,15 @@ func resourceArchiveFileCreate(d *schema.ResourceData, meta interface{}) error {
7476}
7577
7678func 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
140150func 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
154163func 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 }
0 commit comments