Skip to content

Commit

Permalink
fix error when writing file with empty content
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kmoe committed May 10, 2019
1 parent fc6bace commit c507fd8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
5 changes: 1 addition & 4 deletions local/data_source_local_file.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package local

import (
"crypto/sha1"
"encoding/hex"
"io/ioutil"

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

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

checksum := sha1.Sum([]byte(content))
d.SetId(hex.EncodeToString(checksum[:]))
d.SetId("-")

return nil
}
15 changes: 5 additions & 10 deletions local/resource_local_file.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package local

import (
"crypto/sha1"
"encoding/hex"
"io/ioutil"
"os"
"path"
Expand All @@ -12,21 +10,20 @@ import (

func resourceLocalFile() *schema.Resource {
return &schema.Resource{
Create: resourceLocalFileCreate,
Create: resourceLocalFileCreateUpdate,
Read: resourceLocalFileRead,
Delete: resourceLocalFileDelete,
Update: resourceLocalFileCreateUpdate,

Schema: map[string]*schema.Schema{
"content": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"sensitive_content"},
},
"sensitive_content": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Sensitive: true,
ConflictsWith: []string{"content"},
},
Expand Down Expand Up @@ -56,8 +53,7 @@ func resourceLocalFileRead(d *schema.ResourceData, _ interface{}) error {
return err
}

outputChecksum := sha1.Sum([]byte(outputContent))
if hex.EncodeToString(outputChecksum[:]) != d.Id() {
if string(outputContent) != resourceLocalFileContent(d) {
d.SetId("")
return nil
}
Expand All @@ -76,7 +72,7 @@ func resourceLocalFileContent(d *schema.ResourceData) string {
return useContent
}

func resourceLocalFileCreate(d *schema.ResourceData, _ interface{}) error {
func resourceLocalFileCreateUpdate(d *schema.ResourceData, _ interface{}) error {
content := resourceLocalFileContent(d)
destination := d.Get("filename").(string)

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

checksum := sha1.Sum([]byte(content))
d.SetId(hex.EncodeToString(checksum[:]))
d.SetId("-")

return nil
}
Expand Down

0 comments on commit c507fd8

Please sign in to comment.