Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an error_tag optional field to resource.shell_script #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ resource "shell_script" "github_repository" {
DESCRIPTION = "description"
}


//sensitive environment variables are exactly the
//same as environment variables except they don't
//show up in log files
Expand All @@ -178,6 +178,11 @@ resource "shell_script" "github_repository" {
triggers = {
when_value_changed = var.some_value
}

//if your shell_script resource is in a module that is instanciated several
//times, an error_tag can help you figure out which precise resource is impacted
//in case of an error
error_tag = "repo for ${var.username}"
}

output "id" {
Expand All @@ -198,4 +203,3 @@ To run automated tests:
```sh
$ make test
```

13 changes: 13 additions & 0 deletions shell/resource_shell_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ func resourceShellScript() *schema.Resource {
Optional: true,
Default: false,
},
"error_tag": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
},
}
}
Expand Down Expand Up @@ -123,6 +128,7 @@ func create(d *schema.ResourceData, meta interface{}, stack []Action) error {
interpreter := getInterpreter(client, d)
workingDirectory := d.Get("working_directory").(string)
enableParallelism := client.config.EnableParallelism
errorTag := d.Get("error_tag").(string)
d.MarkNewResource()

commandConfig := &CommandConfig{
Expand All @@ -133,6 +139,7 @@ func create(d *schema.ResourceData, meta interface{}, stack []Action) error {
Interpreter: interpreter,
Action: ActionCreate,
EnableParallelism: enableParallelism,
ErrorTag: errorTag,
}
output, err := runCommand(commandConfig)
if err != nil {
Expand Down Expand Up @@ -180,6 +187,7 @@ func read(d *schema.ResourceData, meta interface{}, stack []Action) error {
workingDirectory := d.Get("working_directory").(string)
previousOutput := expandOutput(d.Get("output"))
enableParallelism := client.config.EnableParallelism
errorTag := d.Get("error_tag").(string)

commandConfig := &CommandConfig{
Command: command,
Expand All @@ -190,6 +198,7 @@ func read(d *schema.ResourceData, meta interface{}, stack []Action) error {
Action: ActionRead,
PreviousOutput: previousOutput,
EnableParallelism: enableParallelism,
ErrorTag: errorTag,
}
output, err := runCommand(commandConfig)
if err != nil {
Expand Down Expand Up @@ -242,6 +251,7 @@ func update(d *schema.ResourceData, meta interface{}, stack []Action) error {
workingDirectory := d.Get("working_directory").(string)
previousOutput := expandOutput(d.Get("output"))
enableParallelism := client.config.EnableParallelism
errorTag := d.Get("error_tag").(string)

commandConfig := &CommandConfig{
Command: command,
Expand All @@ -252,6 +262,7 @@ func update(d *schema.ResourceData, meta interface{}, stack []Action) error {
Action: ActionDelete,
PreviousOutput: previousOutput,
EnableParallelism: enableParallelism,
ErrorTag: errorTag,
}
output, err := runCommand(commandConfig)
if err != nil {
Expand Down Expand Up @@ -286,6 +297,7 @@ func delete(d *schema.ResourceData, meta interface{}, stack []Action) error {
workingDirectory := d.Get("working_directory").(string)
previousOutput := expandOutput(d.Get("output"))
enableParallelism := client.config.EnableParallelism
errorTag := d.Get("error_tag").(string)

commandConfig := &CommandConfig{
Command: command,
Expand All @@ -296,6 +308,7 @@ func delete(d *schema.ResourceData, meta interface{}, stack []Action) error {
Action: ActionDelete,
PreviousOutput: previousOutput,
EnableParallelism: enableParallelism,
ErrorTag: errorTag,
}
_, err := runCommand(commandConfig)
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions shell/resource_shell_script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ func TestAccShellShellScript_basic_error(t *testing.T) {
})
}

func TestAccShellShellScript_basic_tagged_error(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckShellScriptDestroy,
Steps: []resource.TestStep{
{
Config: testAccShellScriptConfig_basic_tagged_error(),
ExpectNonEmptyPlan: true,
ExpectError: regexp.MustCompile("Tag: testErrorTag"),
},
},
})
}

func TestAccShellShellScript_create_read_delete(t *testing.T) {
resourceName := "shell_script.crd"
rString := acctest.RandString(8)
Expand Down Expand Up @@ -175,6 +189,26 @@ EOF
`)
}

func testAccShellScriptConfig_basic_tagged_error() string {
return fmt.Sprintf(`
resource "shell_script" "basic" {
lifecycle_commands {
create = <<EOF
echo "Something went wrong!"
exit 1
EOF
delete = "exit 1"
}

environment = {
filename= "create_delete.json"
}

error_tag = "testErrorTag"
}
`)
}

func testAccShellScriptConfig_create_read_delete(outValue string) string {
return fmt.Sprintf(`
resource "shell_script" "crd" {
Expand Down
7 changes: 6 additions & 1 deletion shell/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type CommandConfig struct {
Action Action
PreviousOutput map[string]string
EnableParallelism bool
ErrorTag string
}

func runCommand(c *CommandConfig) (map[string]string, error) {
Expand Down Expand Up @@ -98,7 +99,11 @@ func runCommand(c *CommandConfig) (map[string]string, error) {

// If the script exited with a non-zero code then send the error up to Terraform
if err != nil {
return nil, fmt.Errorf("Error occurred during execution.\n Command: '%s' \n Error: '%s' \n StdOut: \n %s \n StdErr: \n %s", c.Command, err.Error(), stdOutput, stdError)
tagLine := ""
if c.ErrorTag != "" {
tagLine = fmt.Sprintf("Tag: %s\n ", c.ErrorTag)
}
return nil, fmt.Errorf("Error occurred during execution.\n Command: '%s' \n %sError: '%s' \n StdOut: \n %s \n StdErr: \n %s", c.Command, tagLine, err.Error(), stdOutput, stdError)
}

log.Printf("-------------------------")
Expand Down