Skip to content

Commit 79ece45

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 be1ef01 commit 79ece45

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

local/resource_local_file.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ func resourceLocalFile() *schema.Resource {
3636
Required: true,
3737
ForceNew: true,
3838
},
39+
"preserve_on_destroy": {
40+
Type: schema.TypeBool,
41+
Description: "If true, the file will be preserved on destroy.",
42+
Optional: true,
43+
Default: false,
44+
ForceNew: true,
45+
},
3946
},
4047
}
4148
}
@@ -98,6 +105,8 @@ func resourceLocalFileCreate(d *schema.ResourceData, _ interface{}) error {
98105
}
99106

100107
func resourceLocalFileDelete(d *schema.ResourceData, _ interface{}) error {
101-
os.Remove(d.Get("filename").(string))
108+
if !d.Get("preserve_on_destroy").(bool) {
109+
os.Remove(d.Get("filename").(string))
110+
}
102111
return nil
103112
}

local/resource_local_file_test.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import (
1313

1414
func TestLocalFile_Basic(t *testing.T) {
1515
var cases = []struct {
16-
path string
17-
content string
18-
config string
16+
path string
17+
preserve bool
18+
content string
19+
config string
1920
}{
2021
{
2122
"local_file",
23+
false,
2224
"This is some content",
2325
`resource "local_file" "file" {
2426
content = "This is some content"
@@ -27,10 +29,22 @@ func TestLocalFile_Basic(t *testing.T) {
2729
},
2830
{
2931
"local_file",
32+
false,
3033
"This is some sensitive content",
3134
`resource "local_file" "file" {
3235
sensitive_content = "This is some sensitive content"
3336
filename = "local_file"
37+
preserve_on_destroy = false
38+
}`,
39+
},
40+
{
41+
"local_file",
42+
true,
43+
"This is some content",
44+
`resource "local_file" "file" {
45+
content = "This is some content"
46+
filename = "local_file"
47+
preserve_on_destroy = true
3448
}`,
3549
},
3650
}
@@ -55,6 +69,12 @@ func TestLocalFile_Basic(t *testing.T) {
5569
},
5670
CheckDestroy: func(*terraform.State) error {
5771
if _, err := os.Stat(tt.path); os.IsNotExist(err) {
72+
if !tt.preserve {
73+
return nil
74+
}
75+
return errors.New("local_file was destroyed when 'preserve_on_destroy' was set to true")
76+
} else if tt.preserve {
77+
os.Remove(tt.path)
5878
return nil
5979
}
6080
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
@@ -35,5 +35,7 @@ The following arguments are supported:
3535

3636
* `filename` - (Required) The path of the file to create.
3737

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

0 commit comments

Comments
 (0)