Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 1 addition & 4 deletions local/data_source_local_file.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package local

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

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -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
}
21 changes: 11 additions & 10 deletions local/resource_local_file.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package local

import (
"crypto/sha1"
"bytes"
"encoding/base64"
"encoding/hex"
"io/ioutil"
"os"
"path"
Expand All @@ -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": {
Expand All @@ -28,7 +28,6 @@ func resourceLocalFile() *schema.Resource {
"sensitive_content": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Sensitive: true,
ConflictsWith: []string{"content", "content_base64"},
},
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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
}
Expand Down
52 changes: 52 additions & 0 deletions local/resource_local_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
},
})
}
}