Skip to content

Commit c2ebc9d

Browse files
committed
fix error when writing file with empty content
Previously, attempting to write a local file with content of "" would set the resource Id to "", since the Id is based on a SHA1 of the content. This caused the file to be deleted during the apply step. The Id is now set to a constant "-" to make it clear that it is not significant. The ForceNew behaviour for content has been replaced with an Update function for more appropriate semantics.
1 parent 6d7c3e7 commit c2ebc9d

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ require (
44
github.com/hashicorp/hcl v1.0.0 // indirect
55
github.com/hashicorp/terraform-plugin-sdk v1.0.0
66
)
7+
8+
go 1.13

local/data_source_local_file.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package local
22

33
import (
4-
"crypto/sha1"
54
"encoding/base64"
6-
"encoding/hex"
75
"io/ioutil"
86

97
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -42,8 +40,7 @@ func dataSourceLocalFileRead(d *schema.ResourceData, _ interface{}) error {
4240
d.Set("content", string(content))
4341
d.Set("content_base64", base64.StdEncoding.EncodeToString(content))
4442

45-
checksum := sha1.Sum([]byte(content))
46-
d.SetId(hex.EncodeToString(checksum[:]))
43+
d.SetId("-")
4744

4845
return nil
4946
}

local/resource_local_file.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package local
22

33
import (
4-
"crypto/sha1"
4+
"bytes"
55
"encoding/base64"
6-
"encoding/hex"
76
"io/ioutil"
87
"os"
98
"path"
@@ -14,9 +13,10 @@ import (
1413

1514
func resourceLocalFile() *schema.Resource {
1615
return &schema.Resource{
17-
Create: resourceLocalFileCreate,
16+
Create: resourceLocalFileCreateUpdate,
1817
Read: resourceLocalFileRead,
1918
Delete: resourceLocalFileDelete,
19+
Update: resourceLocalFileCreateUpdate,
2020

2121
Schema: map[string]*schema.Schema{
2222
"content": {
@@ -28,7 +28,6 @@ func resourceLocalFile() *schema.Resource {
2828
"sensitive_content": {
2929
Type: schema.TypeString,
3030
Optional: true,
31-
ForceNew: true,
3231
Sensitive: true,
3332
ConflictsWith: []string{"content", "content_base64"},
3433
},
@@ -80,8 +79,12 @@ func resourceLocalFileRead(d *schema.ResourceData, _ interface{}) error {
8079
return err
8180
}
8281

83-
outputChecksum := sha1.Sum([]byte(outputContent))
84-
if hex.EncodeToString(outputChecksum[:]) != d.Id() {
82+
content, err := resourceLocalFileContent(d)
83+
if err != nil {
84+
return err
85+
}
86+
87+
if !bytes.Equal(outputContent, content) {
8588
d.SetId("")
8689
return nil
8790
}
@@ -101,12 +104,11 @@ func resourceLocalFileContent(d *schema.ResourceData) ([]byte, error) {
101104
return []byte(content.(string)), nil
102105
}
103106

104-
func resourceLocalFileCreate(d *schema.ResourceData, _ interface{}) error {
107+
func resourceLocalFileCreateUpdate(d *schema.ResourceData, _ interface{}) error {
105108
content, err := resourceLocalFileContent(d)
106109
if err != nil {
107110
return err
108111
}
109-
110112
destination := d.Get("filename").(string)
111113

112114
destinationDir := path.Dir(destination)
@@ -126,8 +128,7 @@ func resourceLocalFileCreate(d *schema.ResourceData, _ interface{}) error {
126128
return err
127129
}
128130

129-
checksum := sha1.Sum([]byte(content))
130-
d.SetId(hex.EncodeToString(checksum[:]))
131+
d.SetId("-")
131132

132133
return nil
133134
}

0 commit comments

Comments
 (0)