Skip to content

Commit

Permalink
Merge pull request #55 from daniel-ds/fix-proxy-delete
Browse files Browse the repository at this point in the history
Fix api_proxy delete method to only succeed when proxy is deleted
  • Loading branch information
zambien authored Jan 15, 2020
2 parents af7e300 + c58b43f commit 0b98d55
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
8 changes: 6 additions & 2 deletions apigee/resource_api_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,13 @@ func resourceApiProxyDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[ERROR] resourceApiProxyDelete error deleting api_proxy: %s", err.Error())
return fmt.Errorf("[ERROR] resourceApiProxyDelete error deleting api_proxy: %s", err.Error())
}
} else {
deleted = true
}
deleted = true
tries += tries
tries += 1
}
if !deleted {
return fmt.Errorf("[ERROR] unable to delete ApiProxy")
}

return nil
Expand Down
70 changes: 68 additions & 2 deletions apigee/resource_api_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform/terraform"
"github.com/zambien/go-apigee-edge"
"log"
"regexp"
"strings"
"testing"
)
Expand Down Expand Up @@ -45,6 +46,36 @@ func TestAccProxy_Updated(t *testing.T) {
})
}

func TestAccProxy_FailToDeleteWhenDeploymentExists(t *testing.T) {
proxyName := "foo_proxy_terraformed_delete_test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,

CheckDestroy: testAccCheckProxyDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckProxyConfigDeleteTest,
Check: resource.ComposeTestCheckFunc(
testAccCheckProxyExists("apigee_api_proxy.foo_api_proxy", proxyName),
resource.TestCheckResourceAttr("apigee_api_proxy.foo_api_proxy", "name", proxyName),
),
},
{
Config: testAccCheckProxyConfigDeleteTest,
Destroy: true,
PreConfig: deployProxy(t, proxyName),
ExpectError: regexp.MustCompile("unable to delete ApiProxy"),
},
{
Config: testAccCheckProxyConfigDeleteTest,
Destroy: true,
PreConfig: undeployProxy(t, proxyName),
},
},
})
}

func testAccCheckProxyDestroy(s *terraform.State) error {

client := testAccProvider.Meta().(*apigee.EdgeClient)
Expand Down Expand Up @@ -82,16 +113,27 @@ resource "apigee_api_proxy" "foo_api_proxy" {
}
`

const testAccCheckProxyConfigDeleteTest = `
resource "apigee_api_proxy" "foo_api_proxy" {
name = "foo_proxy_terraformed_delete_test"
bundle = "test-fixtures/helloworld_proxy.zip"
bundle_sha = "${filebase64sha256("test-fixtures/helloworld_proxy.zip")}"
}
`

func proxyDestroyHelper(s *terraform.State, client *apigee.EdgeClient) error {

for _, r := range s.RootModule().Resources {
if r.Type != "apigee_api_proxy" {
continue
}
id := r.Primary.ID

if id == "" {
return fmt.Errorf("No proxy ID is set")
}

_, _, err := client.Proxies.Get("foo_proxy")
_, _, err := client.Proxies.Get(r.Primary.Attributes["name"])

if err != nil {
if strings.Contains(err.Error(), "404 ") {
Expand All @@ -101,7 +143,7 @@ func proxyDestroyHelper(s *terraform.State, client *apigee.EdgeClient) error {
}
}

return fmt.Errorf("Proxy still exists")
return nil
}

func proxyExistsHelper(s *terraform.State, client *apigee.EdgeClient, name string) error {
Expand All @@ -122,3 +164,27 @@ func proxyExistsHelper(s *terraform.State, client *apigee.EdgeClient, name strin
}
return nil
}

func deployProxy(t *testing.T, proxyName string) func() {
return func() {
client := testAccProvider.Meta().(*apigee.EdgeClient)
_, _, err := client.Proxies.Deploy(proxyName, "test", 1, 1, false)
if err != nil {
t.Logf("[ERROR] Could not deploy proxy: %s, %s", proxyName, err)
} else {
t.Logf("Deployed proxy: %s", proxyName)
}
}
}

func undeployProxy(t *testing.T, proxyName string) func() {
return func() {
client := testAccProvider.Meta().(*apigee.EdgeClient)
_, _, err := client.Proxies.Undeploy(proxyName, "test", 1)
if err != nil {
t.Logf("[ERROR] Could not undeploy proxy: %s, %s", proxyName, err)
} else {
t.Logf("Undeployed proxy: %s", proxyName)
}
}
}

0 comments on commit 0b98d55

Please sign in to comment.