Skip to content

Commit 2d2be90

Browse files
authored
Merge pull request #85 from TelkomIndonesia/fix/blank-previous-output
fix empty previous output (#84)
2 parents 824f21b + c0452a6 commit 2d2be90

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

shell/resource_shell_script.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ func read(d *schema.ResourceData, meta interface{}, stack []Action) error {
259259
sensitiveEnvironment := formatEnvironmentVariables(sensitiveEnvVariables)
260260
interpreter := getInterpreter(client, d)
261261
workingDirectory := d.Get("working_directory").(string)
262-
previousOutput := expandOutput(d.Get("output"))
262+
o, _ := d.GetChange("output")
263+
previousOutput := expandOutput(o)
263264
enableParallelism := client.config.EnableParallelism
264265

265266
commandConfig := &CommandConfig{
@@ -346,7 +347,8 @@ func update(d *schema.ResourceData, meta interface{}, stack []Action) error {
346347
sensitiveEnvironment := formatEnvironmentVariables(sensitiveEnvVariables)
347348
interpreter := getInterpreter(client, d)
348349
workingDirectory := d.Get("working_directory").(string)
349-
previousOutput := expandOutput(d.Get("output"))
350+
o, _ := d.GetChange("output")
351+
previousOutput := expandOutput(o)
350352
enableParallelism := client.config.EnableParallelism
351353

352354
commandConfig := &CommandConfig{
@@ -395,7 +397,8 @@ func delete(d *schema.ResourceData, meta interface{}, stack []Action) error {
395397
sensitiveEnvironment := formatEnvironmentVariables(sensitiveEnvVariables)
396398
interpreter := getInterpreter(client, d)
397399
workingDirectory := d.Get("working_directory").(string)
398-
previousOutput := expandOutput(d.Get("output"))
400+
o, _ := d.GetChange("output")
401+
previousOutput := expandOutput(o)
399402
enableParallelism := client.config.EnableParallelism
400403

401404
commandConfig := &CommandConfig{

shell/resource_shell_script_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,3 +504,62 @@ func testAccShellShellScriptConfig_outputDependency(filename string, value bool,
504504
}
505505
return
506506
}
507+
508+
func TestAccShellShellScript_previousOutput(t *testing.T) {
509+
file := "/tmp/test-file-" + acctest.RandString(16)
510+
resource.Test(t, resource.TestCase{
511+
Providers: testAccProviders,
512+
Steps: []resource.TestStep{
513+
{
514+
Config: testAccShellShellScriptConfig_previousOutput(file, true),
515+
Check: resource.TestCheckResourceAttr("shell_script.previous", "output.value", ""),
516+
},
517+
{
518+
Config: testAccShellShellScriptConfig_previousOutput(file, false),
519+
Check: resource.TestCheckResourceAttr("shell_script.previous", "output.value", "true"),
520+
},
521+
},
522+
})
523+
}
524+
525+
func testAccShellShellScriptConfig_previousOutput(filename string, value bool) (conf string) {
526+
return fmt.Sprintf(`
527+
resource "shell_script" "shell_script" {
528+
lifecycle_commands {
529+
create = <<-EOF
530+
echo -n '{"value": '"$VALUE"'}' > "$FILE"
531+
EOF
532+
read = <<-EOF
533+
cat "$FILE"
534+
EOF
535+
update = <<-EOF
536+
cat > "$FILE.previous" && echo -n '{"value": '"$VALUE"'}' > "$FILE"
537+
EOF
538+
delete = <<-EOF
539+
rm "$FILE"
540+
EOF
541+
}
542+
environment = {
543+
FILE = "%s"
544+
VALUE = "%t"
545+
}
546+
}
547+
548+
resource "shell_script" "previous" {
549+
lifecycle_commands {
550+
create = "true"
551+
read = <<-EOF
552+
cat $FILE || echo -n '{"value": null}'
553+
EOF
554+
update = "true"
555+
delete = "true"
556+
}
557+
environment = {
558+
FILE = "${shell_script.shell_script.environment.FILE}.previous"
559+
}
560+
triggers = {
561+
VALUE = shell_script.shell_script.environment.VALUE
562+
}
563+
}
564+
`, filename, value)
565+
}

0 commit comments

Comments
 (0)