diff --git a/go.mod b/go.mod index 77322feb..6dcc683b 100644 --- a/go.mod +++ b/go.mod @@ -4,3 +4,5 @@ require ( github.com/hashicorp/hcl v1.0.0 // indirect github.com/hashicorp/terraform-plugin-sdk v1.0.0 ) + +go 1.13 diff --git a/local/data_source_local_file.go b/local/data_source_local_file.go index a0be4c2d..e548a523 100644 --- a/local/data_source_local_file.go +++ b/local/data_source_local_file.go @@ -1,9 +1,7 @@ package local import ( - "crypto/sha1" "encoding/base64" - "encoding/hex" "io/ioutil" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -42,8 +40,7 @@ func dataSourceLocalFileRead(d *schema.ResourceData, _ interface{}) error { d.Set("content", string(content)) d.Set("content_base64", base64.StdEncoding.EncodeToString(content)) - checksum := sha1.Sum([]byte(content)) - d.SetId(hex.EncodeToString(checksum[:])) + d.SetId("-") return nil } diff --git a/local/resource_local_file.go b/local/resource_local_file.go index 25897577..b1f2a2bb 100644 --- a/local/resource_local_file.go +++ b/local/resource_local_file.go @@ -1,9 +1,8 @@ package local import ( - "crypto/sha1" + "bytes" "encoding/base64" - "encoding/hex" "io/ioutil" "os" "path" @@ -14,9 +13,10 @@ import ( func resourceLocalFile() *schema.Resource { return &schema.Resource{ - Create: resourceLocalFileCreate, + Create: resourceLocalFileCreateUpdate, Read: resourceLocalFileRead, Delete: resourceLocalFileDelete, + Update: resourceLocalFileCreateUpdate, Schema: map[string]*schema.Schema{ "content": { @@ -28,7 +28,6 @@ func resourceLocalFile() *schema.Resource { "sensitive_content": { Type: schema.TypeString, Optional: true, - ForceNew: true, Sensitive: true, ConflictsWith: []string{"content", "content_base64"}, }, @@ -80,8 +79,12 @@ func resourceLocalFileRead(d *schema.ResourceData, _ interface{}) error { return err } - outputChecksum := sha1.Sum([]byte(outputContent)) - if hex.EncodeToString(outputChecksum[:]) != d.Id() { + content, err := resourceLocalFileContent(d) + if err != nil { + return err + } + + if !bytes.Equal(outputContent, content) { d.SetId("") return nil } @@ -101,12 +104,11 @@ func resourceLocalFileContent(d *schema.ResourceData) ([]byte, error) { return []byte(content.(string)), nil } -func resourceLocalFileCreate(d *schema.ResourceData, _ interface{}) error { +func resourceLocalFileCreateUpdate(d *schema.ResourceData, _ interface{}) error { content, err := resourceLocalFileContent(d) if err != nil { return err } - destination := d.Get("filename").(string) destinationDir := path.Dir(destination) @@ -126,8 +128,7 @@ func resourceLocalFileCreate(d *schema.ResourceData, _ interface{}) error { return err } - checksum := sha1.Sum([]byte(content)) - d.SetId(hex.EncodeToString(checksum[:])) + d.SetId("-") return nil } diff --git a/local/resource_local_file_test.go b/local/resource_local_file_test.go index 43fba769..54e1d3c4 100644 --- a/local/resource_local_file_test.go +++ b/local/resource_local_file_test.go @@ -152,5 +152,57 @@ resource "local_file" "file" { }) defer os.Remove(destinationDirPath) +} + +func TestLocalFile_EmptyContent(t *testing.T) { + var cases = []struct { + path string + content string + config string + }{ + { + "local_file", + "This is some content", + `resource "local_file" "file" { + content = "This is some content" + filename = "local_file" + }`, + }, + { + "local_file", + "", + `resource "local_file" "file" { + content = "" + filename = "local_file" + }`, + }, + } + for _, tt := range cases { + r.UnitTest(t, r.TestCase{ + Providers: testProviders, + Steps: []r.TestStep{ + { + Config: tt.config, + Check: func(s *terraform.State) error { + content, err := ioutil.ReadFile(tt.path) + if err != nil { + return fmt.Errorf("config:\n%s\n,got: %s\n", tt.config, err) + } + if string(content) != tt.content { + return fmt.Errorf("config:\n%s\ngot:\n%s\nwant:\n%s\n", tt.config, content, tt.content) + } + return nil + }, + Destroy: false, + }, + }, + CheckDestroy: func(*terraform.State) error { + if _, err := os.Stat(tt.path); os.IsNotExist(err) { + return nil + } + return errors.New("local_file did not get destroyed") + }, + }) + } }