Skip to content

Commit

Permalink
fix issue with proxy delete race condition and add a test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
zambien committed Nov 5, 2019
1 parent 8a77f05 commit 5773531
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
22 changes: 18 additions & 4 deletions apigee/resource_api_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"strconv"
"time"

"github.com/gofrs/uuid"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -145,10 +146,23 @@ func resourceApiProxyDelete(d *schema.ResourceData, meta interface{}) error {

client := meta.(*apigee.EdgeClient)

_, _, err := client.Proxies.Delete(d.Get("name").(string))
if err != nil {
log.Printf("[ERROR] resourceApiProxyDelete error deleting api_proxy: %s", err.Error())
return fmt.Errorf("[ERROR] resourceApiProxyDelete error deleting api_proxy: %s", err.Error())
//We have to handle retries in a special way here since this is a DELETE. Note this used to work fine without retries.
deleted := false
tries := 0
for !deleted && tries < 3 {
_, _, err := client.Proxies.Delete(d.Get("name").(string))
if err != nil {
//This is a race condition with Apigee APIs. Wait and try again.
if strings.Contains(err.Error(), "Undeploy the ApiProxy and try again") {
log.Printf("[ERROR] resourceApiProxyDelete proxy still exists. We will wait and try again.")
time.Sleep(5 * time.Second)
} else {
log.Printf("[ERROR] resourceApiProxyDelete error deleting api_proxy: %s", err.Error())
return fmt.Errorf("[ERROR] resourceApiProxyDelete error deleting api_proxy: %s", err.Error())
}
}
deleted = true
tries += tries
}

return nil
Expand Down
25 changes: 19 additions & 6 deletions apigee/resource_api_proxy_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func TestAccProxyDeployment_Updated(t *testing.T) {
resource.TestStep{
Config: testAccCheckProxyDeploymentConfigRequired,
Check: resource.ComposeTestCheckFunc(
testAccCheckProxyDeploymentExists("apigee_api_proxy_deployment.foo_api_proxy_deployment", "helloworld"),
testAccCheckProxyDeploymentExists("apigee_api_proxy_deployment.foo_api_proxy_deployment", "foo_proxy_terraformed"),
resource.TestCheckResourceAttr(
"apigee_api_proxy_deployment.foo_api_proxy_deployment", "proxy_name", "helloworld"),
"apigee_api_proxy_deployment.foo_api_proxy_deployment", "proxy_name", "foo_proxy_terraformed"),
resource.TestCheckResourceAttr(
"apigee_api_proxy_deployment.foo_api_proxy_deployment", "org", "zambien-trial"),
resource.TestCheckResourceAttr(
Expand All @@ -32,9 +32,9 @@ func TestAccProxyDeployment_Updated(t *testing.T) {
resource.TestStep{
Config: testAccCheckProxyDeploymentConfigUpdated,
Check: resource.ComposeTestCheckFunc(
testAccCheckProxyDeploymentExists("apigee_api_proxy_deployment.foo_api_proxy_deployment", "helloworld"),
testAccCheckProxyDeploymentExists("apigee_api_proxy_deployment.foo_api_proxy_deployment", "foo_proxy_terraformed"),
resource.TestCheckResourceAttr(
"apigee_api_proxy_deployment.foo_api_proxy_deployment", "proxy_name", "helloworld"),
"apigee_api_proxy_deployment.foo_api_proxy_deployment", "proxy_name", "foo_proxy_terraformed"),
resource.TestCheckResourceAttr(
"apigee_api_proxy_deployment.foo_api_proxy_deployment", "org", "zambien-trial"),
resource.TestCheckResourceAttr(
Expand Down Expand Up @@ -71,17 +71,30 @@ func testAccCheckProxyDeploymentExists(n string, name string) resource.TestCheck
}

const testAccCheckProxyDeploymentConfigRequired = `
resource "apigee_api_proxy" "foo_api_proxy" {
name = "foo_proxy_terraformed"
bundle = "test-fixtures/helloworld_proxy.zip"
bundle_sha = "${filebase64sha256("test-fixtures/helloworld_proxy.zip")}"
}
resource "apigee_api_proxy_deployment" "foo_api_proxy_deployment" {
proxy_name = "helloworld"
proxy_name = "${apigee_api_proxy.foo_api_proxy.name}"
org = "zambien-trial"
env = "test"
revision = "1"
}
`

const testAccCheckProxyDeploymentConfigUpdated = `
resource "apigee_api_proxy" "foo_api_proxy" {
name = "foo_proxy_terraformed"
bundle = "test-fixtures/helloworld_proxy2.zip"
bundle_sha = "${filebase64sha256("test-fixtures/helloworld_proxy2.zip")}"
}
resource "apigee_api_proxy_deployment" "foo_api_proxy_deployment" {
proxy_name = "helloworld"
proxy_name = "${apigee_api_proxy.foo_api_proxy.name}"
org = "zambien-trial"
env = "test"
revision = "2"
Expand Down
Binary file added apigee/test-fixtures/helloworld_proxy2.zip
Binary file not shown.

0 comments on commit 5773531

Please sign in to comment.