Skip to content

Commit 824f21b

Browse files
authored
Merge pull request #83 from TelkomIndonesia/fix/inconsistent-plan
Fix inconsistent plan (#81)
2 parents 335cecd + 7e12ee7 commit 824f21b

File tree

2 files changed

+98
-11
lines changed

2 files changed

+98
-11
lines changed

shell/resource_shell_script.go

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package shell
22

33
import (
4+
"fmt"
45
"log"
56
"reflect"
67
"runtime"
@@ -87,6 +88,10 @@ func resourceShellScript() *schema.Resource {
8788
Type: schema.TypeString,
8889
Optional: true,
8990
Default: "",
91+
ValidateFunc: func(i interface{}, s string) ([]string, []error) {
92+
x0 := fmt.Errorf("`read_error` cannot be set from configuration")
93+
return []string{}, []error{x0}
94+
},
9095
},
9196
},
9297
}
@@ -110,7 +115,19 @@ func resourceShellScriptRead(d *schema.ResourceData, meta interface{}) error {
110115
return nil
111116
}
112117

113-
func resourceShellScriptUpdate(d *schema.ResourceData, meta interface{}) error {
118+
func resourceShellScriptUpdate(d *schema.ResourceData, meta interface{}) (err error) {
119+
if d.HasChange("lifecycle_commands.0.read") {
120+
err = read(d, meta, []Action{ActionUpdate})
121+
if err != nil {
122+
_ = restoreOldResourceData(d)
123+
}
124+
return
125+
}
126+
127+
if d.HasChanges("lifecycle_commands", "interpreter") {
128+
return
129+
}
130+
114131
return update(d, meta, []Action{ActionUpdate})
115132
}
116133

@@ -124,6 +141,18 @@ func resourceShellScriptCustomizeDiff(d *schema.ResourceDiff, i interface{}) (er
124141
}
125142

126143
if d.HasChange("lifecycle_commands") || d.HasChange("interpreter") {
144+
for _, k := range []string{
145+
"environment", "sensitive_environment", "working_directory"} {
146+
147+
if d.HasChange(k) {
148+
return fmt.Errorf("changes to `lifecycle_commands` and/or `interpreter`" +
149+
" should not be follwed by changes to other arguments")
150+
}
151+
}
152+
153+
if d.HasChange("lifecycle_commands.0.read") {
154+
_ = d.SetNewComputed("output") // assume output will change
155+
}
127156
return
128157
}
129158

@@ -135,16 +164,20 @@ func resourceShellScriptCustomizeDiff(d *schema.ResourceDiff, i interface{}) (er
135164
}
136165

137166
if _, ok := d.GetOk("lifecycle_commands.0.update"); ok {
167+
for _, k := range []string{
168+
"environment", "sensitive_environment", "working_directory"} {
169+
170+
if d.HasChange(k) {
171+
_ = d.SetNewComputed("output") // assume output will change
172+
}
173+
}
138174
return // updateable
139175
}
140176

141177
// all the other arguments
142178
for _, k := range []string{
143-
"environment",
144-
"sensitive_environment",
145-
"working_directory",
146-
"dirty",
147-
} {
179+
"environment", "sensitive_environment", "working_directory", "dirty"} {
180+
148181
if !d.HasChange(k) {
149182
continue
150183
}
@@ -299,11 +332,6 @@ func restoreOldResourceData(rd *schema.ResourceData, except ...string) (err erro
299332
}
300333

301334
func update(d *schema.ResourceData, meta interface{}, stack []Action) error {
302-
if d.HasChanges("lifecycle_commands", "interpreter") {
303-
_ = restoreOldResourceData(d, "lifecycle_commands", "interpreter", "dirty", "read_error")
304-
return nil
305-
}
306-
307335
log.Printf("[DEBUG] Updating shell script resource...")
308336
d.Set("dirty", false)
309337
printStackTrace(stack)

shell/resource_shell_script_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,62 @@ func testAccShellShellScriptConfig_updateCommands(filename string, bug bool) str
445445
}
446446
`, read, filename)
447447
}
448+
449+
func TestAccShellShellScript_outputDependency(t *testing.T) {
450+
file := "/tmp/test-file-" + acctest.RandString(16)
451+
resource.Test(t, resource.TestCase{
452+
Providers: testAccProviders,
453+
Steps: []resource.TestStep{
454+
{
455+
Config: testAccShellShellScriptConfig_outputDependency(file, true, false),
456+
},
457+
{
458+
Config: testAccShellShellScriptConfig_outputDependency(file, false, true),
459+
Check: resource.TestCheckResourceAttr("shell_script.dependent", "triggers.output", "false"),
460+
},
461+
},
462+
})
463+
464+
}
465+
466+
func testAccShellShellScriptConfig_outputDependency(filename string, value bool, wdependent bool) (conf string) {
467+
conf = fmt.Sprintf(`
468+
resource "shell_script" "shell_script" {
469+
lifecycle_commands {
470+
create = <<-EOF
471+
echo -n '{"value": '"$VALUE"'}' > "$FILE"
472+
EOF
473+
read = <<-EOF
474+
cat "$FILE"
475+
EOF
476+
update = <<-EOF
477+
echo -n '{"value": '"$VALUE"'}' > "$FILE"
478+
EOF
479+
delete = <<-EOF
480+
rm "$FILE"
481+
EOF
482+
}
483+
environment = {
484+
FILE = "%s"
485+
VALUE = "%t"
486+
}
487+
}
488+
`, filename, value)
489+
490+
if wdependent {
491+
conf = conf + `
492+
resource "shell_script" "dependent" {
493+
lifecycle_commands {
494+
create = "echo -n"
495+
read = "echo -n '{}'"
496+
delete = "echo -n"
497+
}
498+
499+
triggers = {
500+
output = shell_script.shell_script.output["value"]
501+
}
502+
}
503+
`
504+
}
505+
return
506+
}

0 commit comments

Comments
 (0)