Skip to content

Commit c507fd8

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 fc6bace commit c507fd8

File tree

2 files changed

+6
-14
lines changed

2 files changed

+6
-14
lines changed

local/data_source_local_file.go

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

33
import (
4-
"crypto/sha1"
5-
"encoding/hex"
64
"io/ioutil"
75

86
"github.com/hashicorp/terraform/helper/schema"
@@ -36,8 +34,7 @@ func dataSourceLocalFileRead(d *schema.ResourceData, _ interface{}) error {
3634

3735
d.Set("content", string(content))
3836

39-
checksum := sha1.Sum([]byte(content))
40-
d.SetId(hex.EncodeToString(checksum[:]))
37+
d.SetId("-")
4138

4239
return nil
4340
}

local/resource_local_file.go

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

33
import (
4-
"crypto/sha1"
5-
"encoding/hex"
64
"io/ioutil"
75
"os"
86
"path"
@@ -12,21 +10,20 @@ import (
1210

1311
func resourceLocalFile() *schema.Resource {
1412
return &schema.Resource{
15-
Create: resourceLocalFileCreate,
13+
Create: resourceLocalFileCreateUpdate,
1614
Read: resourceLocalFileRead,
1715
Delete: resourceLocalFileDelete,
16+
Update: resourceLocalFileCreateUpdate,
1817

1918
Schema: map[string]*schema.Schema{
2019
"content": {
2120
Type: schema.TypeString,
2221
Optional: true,
23-
ForceNew: true,
2422
ConflictsWith: []string{"sensitive_content"},
2523
},
2624
"sensitive_content": {
2725
Type: schema.TypeString,
2826
Optional: true,
29-
ForceNew: true,
3027
Sensitive: true,
3128
ConflictsWith: []string{"content"},
3229
},
@@ -56,8 +53,7 @@ func resourceLocalFileRead(d *schema.ResourceData, _ interface{}) error {
5653
return err
5754
}
5855

59-
outputChecksum := sha1.Sum([]byte(outputContent))
60-
if hex.EncodeToString(outputChecksum[:]) != d.Id() {
56+
if string(outputContent) != resourceLocalFileContent(d) {
6157
d.SetId("")
6258
return nil
6359
}
@@ -76,7 +72,7 @@ func resourceLocalFileContent(d *schema.ResourceData) string {
7672
return useContent
7773
}
7874

79-
func resourceLocalFileCreate(d *schema.ResourceData, _ interface{}) error {
75+
func resourceLocalFileCreateUpdate(d *schema.ResourceData, _ interface{}) error {
8076
content := resourceLocalFileContent(d)
8177
destination := d.Get("filename").(string)
8278

@@ -91,8 +87,7 @@ func resourceLocalFileCreate(d *schema.ResourceData, _ interface{}) error {
9187
return err
9288
}
9389

94-
checksum := sha1.Sum([]byte(content))
95-
d.SetId(hex.EncodeToString(checksum[:]))
90+
d.SetId("-")
9691

9792
return nil
9893
}

0 commit comments

Comments
 (0)