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

run_trigger: retry when a "locked" error is returned #178

Merged
merged 2 commits into from
Sep 21, 2020
Merged
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
21 changes: 18 additions & 3 deletions tfe/resource_tfe_run_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/customdiff"
"log"
"strings"
"time"

tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

Expand Down Expand Up @@ -90,13 +93,25 @@ func resourceTFERunTriggerCreate(d *schema.ResourceData, meta interface{}) error
}

log.Printf("[DEBUG] Create run trigger on workspace %s with sourceable %s", workspaceID, sourceableID)
runTrigger, err := tfeClient.RunTriggers.Create(ctx, workspaceID, options)
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
runTrigger, err := tfeClient.RunTriggers.Create(ctx, workspaceID, options)
if err == nil {
d.SetId(runTrigger.ID)
return nil
}

if strings.Contains(err.Error(), "Run Trigger creation locked") {
log.Printf("[DEBUG] Run triggers are locked for workspace %s, will retry", workspaceID)
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
})

if err != nil {
return fmt.Errorf("Error creating run trigger on workspace %s with sourceable %s: %v", workspaceID, sourceableID, err)
}

d.SetId(runTrigger.ID)

return resourceTFERunTriggerRead(d, meta)
}

Expand Down
44 changes: 44 additions & 0 deletions tfe/resource_tfe_run_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ func TestAccTFERunTrigger_updateWorkspaceExternalIDToWorkspaceID(t *testing.T) {
})
}

func TestAccTFERunTrigger_many(t *testing.T) {
checks := make([]resource.TestCheckFunc, 10)
for i := range checks {
checks[i] = resource.TestCheckResourceAttrSet(fmt.Sprintf("tfe_run_trigger.foobar.%d", i), "id")
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFERunTriggerDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFERunTrigger_many,
Check: resource.ComposeTestCheckFunc(checks...),
},
},
})
}

func TestAccTFERunTriggerImport(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -202,3 +221,28 @@ resource "tfe_run_trigger" "foobar" {
workspace_id = "${tfe_workspace.workspace.id}"
sourceable_id = "${tfe_workspace.sourceable.id}"
}`

const testAccTFERunTrigger_many = `
resource "tfe_organization" "foobar" {
name = "tst-terraform"
email = "[email protected]"
}

resource "tfe_workspace" "workspace" {
name = "workspace-test"
organization = "${tfe_organization.foobar.id}"
}

resource "tfe_workspace" "sourceable" {
count = 10

name = "sourceable-test-${count.index}"
organization = "${tfe_organization.foobar.id}"
}

resource "tfe_run_trigger" "foobar" {
count = 10

workspace_external_id = "${tfe_workspace.workspace.external_id}"
sourceable_id = "${tfe_workspace.sourceable[count.index].external_id}"
}`