Skip to content

Commit 325c30a

Browse files
committed
Allow preserving files on destroy
This change introduces the `preserve_on_destroy` option for the local_file resource. This defaults to `false`, but when set to `true` will ensure that the file is preserved when the resource is destroyed in Terraform. Resolves: #7
1 parent daf17aa commit 325c30a

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

internal/provider/resource_local_file.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ func resourceLocalFile() *schema.Resource {
6767
ForceNew: true,
6868
ConflictsWith: []string{"content", "sensitive_content", "content_base64"},
6969
},
70+
"preserve_on_destroy": {
71+
Type: schema.TypeBool,
72+
Description: "If true, the file will be preserved on destroy.",
73+
Optional: true,
74+
Default: false,
75+
ForceNew: true,
76+
},
7077
},
7178
}
7279
}
@@ -145,6 +152,8 @@ func resourceLocalFileCreate(d *schema.ResourceData, _ interface{}) error {
145152
}
146153

147154
func resourceLocalFileDelete(d *schema.ResourceData, _ interface{}) error {
148-
os.Remove(d.Get("filename").(string))
155+
if !d.Get("preserve_on_destroy").(bool) {
156+
os.Remove(d.Get("filename").(string))
157+
}
149158
return nil
150159
}

internal/provider/resource_local_file_test.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ func TestLocalFile_Basic(t *testing.T) {
2323
f = strings.ReplaceAll(f, `\`, `\\`)
2424

2525
var cases = []struct {
26-
path string
27-
content string
28-
config string
26+
path string
27+
preserve bool
28+
content string
29+
config string
2930
}{
3031
{
3132
f,
33+
false,
3234
"This is some content", fmt.Sprintf(`
3335
resource "local_file" "file" {
3436
content = "This is some content"
@@ -37,14 +39,27 @@ resource "local_file" "file" {
3739
},
3840
{
3941
f,
42+
true,
43+
"This is some content", fmt.Sprintf(`
44+
resource "local_file" "file" {
45+
content = "This is some content"
46+
filename = "%s"
47+
preserve_on_destroy = true
48+
}`, f),
49+
},
50+
{
51+
f,
52+
false,
4053
"This is some sensitive content", fmt.Sprintf(`
4154
resource "local_file" "file" {
4255
sensitive_content = "This is some sensitive content"
4356
filename = "%s"
57+
preserve_on_destroy = false
4458
}`, f),
4559
},
4660
{
4761
f,
62+
false,
4863
"This is some sensitive content", fmt.Sprintf(`
4964
resource "local_file" "file" {
5065
content_base64 = "VGhpcyBpcyBzb21lIHNlbnNpdGl2ZSBjb250ZW50"
@@ -53,6 +68,7 @@ resource "local_file" "file" {
5368
},
5469
{
5570
f,
71+
false,
5672
"This is some sensitive content", fmt.Sprintf(`
5773
resource "local_file" "file" {
5874
content_base64 = base64encode("This is some sensitive content")
@@ -82,6 +98,12 @@ resource "local_file" "file" {
8298
},
8399
CheckDestroy: func(*terraform.State) error {
84100
if _, err := os.Stat(tt.path); os.IsNotExist(err) {
101+
if !tt.preserve {
102+
return nil
103+
}
104+
return errors.New("local_file was destroyed when 'preserve_on_destroy' was set to true")
105+
} else if tt.preserve {
106+
os.Remove(tt.path)
85107
return nil
86108
}
87109
return errors.New("local_file did not get destroyed")

website/docs/r/file.html.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ The following arguments are supported:
4141

4242
* `directory_permission` - (Optional) The permission to set for any directories created. Expects a string. The default value is `"0777"`.
4343

44+
* `preserve_on_destroy` - (Optional) Determines if the file created will be removed when this resource is destroyed in Terraform. Defaults to `false`.
45+
4446
Any required parent directories will be created automatically, and any existing file with the given name will be overwritten.

0 commit comments

Comments
 (0)