From 53fd0f29bb8bcf952e5ccffa1cd67aef52f89095 Mon Sep 17 00:00:00 2001 From: Tyler Kellen Date: Sun, 29 Dec 2019 16:52:56 -0500 Subject: [PATCH 1/3] add support for lifecycle ignore_changes --- local/resource_local_file.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/local/resource_local_file.go b/local/resource_local_file.go index 25897577..150ecdaf 100644 --- a/local/resource_local_file.go +++ b/local/resource_local_file.go @@ -14,8 +14,8 @@ import ( func resourceLocalFile() *schema.Resource { return &schema.Resource{ + Read: resourceLocalFileRead, Create: resourceLocalFileCreate, - Read: resourceLocalFileRead, Delete: resourceLocalFileDelete, Schema: map[string]*schema.Schema{ @@ -71,19 +71,21 @@ func resourceLocalFileRead(d *schema.ResourceData, _ interface{}) error { d.SetId("") return nil } - - // Verify that the content of the destination file matches the content we - // expect. Otherwise, the file might have been modified externally and we - // must reconcile. - outputContent, err := ioutil.ReadFile(outputPath) + // Get actual content from file. + byteContent, err := ioutil.ReadFile(outputPath) if err != nil { return err } - outputChecksum := sha1.Sum([]byte(outputContent)) - if hex.EncodeToString(outputChecksum[:]) != d.Id() { - d.SetId("") - return nil + // Set `content` or `content_base64` to match current value on disk. + var setErr error + if _, exists := d.GetOkExists("content"); exists { + setErr = d.Set("content", string(byteContent)) + } else if _, exists := d.GetOkExists("content_base64"); exists { + setErr = d.Set("content_base64", base64.StdEncoding.EncodeToString(byteContent)) + } + if setErr != nil { + return err } return nil From 0bf35c5990cff9a2fd6eba41201272bba9759fbd Mon Sep 17 00:00:00 2001 From: Tyler Kellen Date: Sun, 29 Dec 2019 22:06:41 -0500 Subject: [PATCH 2/3] add file mode validation --- local/resource_local_file.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/local/resource_local_file.go b/local/resource_local_file.go index 150ecdaf..c8c6511f 100644 --- a/local/resource_local_file.go +++ b/local/resource_local_file.go @@ -8,6 +8,7 @@ import ( "os" "path" "strconv" + "fmt" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) @@ -17,7 +18,13 @@ func resourceLocalFile() *schema.Resource { Read: resourceLocalFileRead, Create: resourceLocalFileCreate, Delete: resourceLocalFileDelete, - + Update: nil, + Exists: func(d *schema.ResourceData, meta interface{}) (bool, error) { + if _, err := os.Stat(d.Get("filename").(string)); os.IsNotExist(err) { + return false, nil + } + return true, nil + }, Schema: map[string]*schema.Schema{ "content": { Type: schema.TypeString, @@ -65,20 +72,23 @@ func resourceLocalFile() *schema.Resource { } func resourceLocalFileRead(d *schema.ResourceData, _ interface{}) error { - // If the output file doesn't exist, mark the resource for creation. - outputPath := d.Get("filename").(string) - if _, err := os.Stat(outputPath); os.IsNotExist(err) { - d.SetId("") - return nil - } // Get actual content from file. - byteContent, err := ioutil.ReadFile(outputPath) + filePath := d.Get("filename").(string) + byteContent, err := ioutil.ReadFile(filePath) if err != nil { return err } - // Set `content` or `content_base64` to match current value on disk. var setErr error + + // Set file_permission to match what is on disk. + stat, _ := os.Stat(filePath) + setErr = d.Set("file_permission", fmt.Sprintf("%04o", stat.Mode().Perm())) + if setErr != nil { + return err + } + + // Set `content` or `content_base64` to match current value on disk. if _, exists := d.GetOkExists("content"); exists { setErr = d.Set("content", string(byteContent)) } else if _, exists := d.GetOkExists("content_base64"); exists { From f67914cece3be51302292cdee1643df380862d32 Mon Sep 17 00:00:00 2001 From: Tyler Kellen Date: Wed, 19 Feb 2020 23:15:28 -0500 Subject: [PATCH 3/3] linting --- local/resource_local_file.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/local/resource_local_file.go b/local/resource_local_file.go index c8c6511f..cfaa247a 100644 --- a/local/resource_local_file.go +++ b/local/resource_local_file.go @@ -4,18 +4,18 @@ import ( "crypto/sha1" "encoding/base64" "encoding/hex" + "fmt" "io/ioutil" "os" "path" "strconv" - "fmt" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) func resourceLocalFile() *schema.Resource { return &schema.Resource{ - Read: resourceLocalFileRead, + Read: resourceLocalFileRead, Create: resourceLocalFileCreate, Delete: resourceLocalFileDelete, Update: nil,