Merge #10851: archive_file md5 hash attribute

This commit is contained in:
Martin Atkins 2016-12-24 11:39:28 -08:00
commit ea93b91a80
2 changed files with 18 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package archive package archive
import ( import (
"crypto/md5"
"crypto/sha1" "crypto/sha1"
"crypto/sha256" "crypto/sha256"
"encoding/base64" "encoding/base64"
@ -68,6 +69,12 @@ func dataSourceFile() *schema.Resource {
ForceNew: true, ForceNew: true,
Description: "Base64 Encoded SHA256 checksum of output file", Description: "Base64 Encoded SHA256 checksum of output file",
}, },
"output_md5": &schema.Schema{
Type: schema.TypeString,
Computed: true,
ForceNew: true,
Description: "MD5 of output file",
},
}, },
} }
} }
@ -94,13 +101,14 @@ func dataSourceFileRead(d *schema.ResourceData, meta interface{}) error {
return err return err
} }
sha1, base64sha256, err := genFileShas(outputPath) sha1, base64sha256, md5, err := genFileShas(outputPath)
if err != nil { if err != nil {
return fmt.Errorf("could not generate file checksum sha256: %s", err) return fmt.Errorf("could not generate file checksum sha256: %s", err)
} }
d.Set("output_sha", sha1) d.Set("output_sha", sha1)
d.Set("output_base64sha256", base64sha256) d.Set("output_base64sha256", base64sha256)
d.Set("output_md5", md5)
d.Set("output_size", fi.Size()) d.Set("output_size", fi.Size())
d.SetId(d.Get("output_sha").(string)) d.SetId(d.Get("output_sha").(string))
@ -136,10 +144,10 @@ func archive(d *schema.ResourceData) error {
return nil return nil
} }
func genFileShas(filename string) (string, string, error) { func genFileShas(filename string) (string, string, string, error) {
data, err := ioutil.ReadFile(filename) data, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
return "", "", fmt.Errorf("could not compute file '%s' checksum: %s", filename, err) return "", "", "", fmt.Errorf("could not compute file '%s' checksum: %s", filename, err)
} }
h := sha1.New() h := sha1.New()
h.Write([]byte(data)) h.Write([]byte(data))
@ -150,5 +158,9 @@ func genFileShas(filename string) (string, string, error) {
shaSum := h256.Sum(nil) shaSum := h256.Sum(nil)
sha256base64 := base64.StdEncoding.EncodeToString(shaSum[:]) sha256base64 := base64.StdEncoding.EncodeToString(shaSum[:])
return sha1, sha256base64, nil md5 := md5.New()
md5.Write([]byte(data))
md5Sum := hex.EncodeToString(md5.Sum(nil))
return sha1, sha256base64, md5Sum, nil
} }

View File

@ -48,3 +48,5 @@ The following attributes are exported:
* `output_sha` - The SHA1 checksum of output archive file. * `output_sha` - The SHA1 checksum of output archive file.
* `output_base64sha256` - The base64-encoded SHA256 checksum of output archive file. * `output_base64sha256` - The base64-encoded SHA256 checksum of output archive file.
* `output_md5` - The MD5 checksum of output archive file.