Skip to content

Commit 6d3f712

Browse files
authored
Merge pull request #531 from jonathan-el/check_site
Add site exists check in post destroy test (TF Bug)
2 parents 711bcec + cdea530 commit 6d3f712

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

incapsula/client_delivery_rules_configuration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c *Client) ReadDeliveryRuleConfiguration(siteID string, category string) (
5555
responseBody, err := ioutil.ReadAll(resp.Body)
5656

5757
// Dump JSON
58-
log.Printf("[DEBUG] Incapsula Read Waiting Room JSON response: %s\n", string(responseBody))
58+
log.Printf("[DEBUG] Incapsula Read Delivery Rules JSON response: %s\n", string(responseBody))
5959

6060
if resp.StatusCode != 200 {
6161
diags = append(diags, diag.Diagnostic{

incapsula/resource_delivery_rules_configuration_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package incapsula
33
import (
44
"fmt"
55
"log"
6+
"strconv"
67
"testing"
78

89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -116,6 +117,16 @@ func testAccCheckIncapsulaDeliveryRuleDestroy(state *terraform.State) error {
116117
}
117118

118119
siteID, ok := res.Primary.Attributes["site_id"]
120+
if siteID == "" {
121+
// There is a bug in Terraform: https://github.com/hashicorp/terraform/issues/23635
122+
// Specifically, upgrades/destroys are happening simulatneously and not honoroing
123+
// dependencies. In this case, it's possible that the site has already been deleted,
124+
// which means that all of the subresources will have been cleared out.
125+
// Ordinarily, this should return an error, but until this gets addressed, we're
126+
// going to simply return nil.
127+
// return fmt.Errorf("Incapsula site ID does not exist")
128+
return nil
129+
}
119130
if !ok {
120131
return fmt.Errorf("Incapsula Site ID does not exist")
121132
}
@@ -126,6 +137,17 @@ func testAccCheckIncapsulaDeliveryRuleDestroy(state *terraform.State) error {
126137

127138
deliveryRulesListDTO, diags := client.ReadDeliveryRuleConfiguration(siteID, category)
128139

140+
// If the site has already been deleted then return nil
141+
// Otherwise check the delivery rules list
142+
siteIdInt, err := strconv.Atoi(siteID)
143+
if err != nil {
144+
return fmt.Errorf("Couldn't parse site ID: %s ", siteID)
145+
}
146+
_, siteStatusErr := client.SiteStatus(domain, siteIdInt)
147+
if siteStatusErr != nil {
148+
return nil
149+
}
150+
129151
if diags != nil {
130152
log.Printf("[ERROR] Failed to read delivery rules in category %s for Site ID %s", category, siteID)
131153
return fmt.Errorf("failed to read delivery rules in category %s for Site ID %s\"", category, siteID)

incapsula/resource_simplified_redirect_rules_configuration_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package incapsula
33
import (
44
"fmt"
55
"log"
6+
"strconv"
67
"testing"
78

89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -98,12 +99,33 @@ func testAccCheckIncapsulaDeliverySimplifiedRedirectRuleDestroy(state *terraform
9899
}
99100

100101
siteID, ok := res.Primary.Attributes["site_id"]
102+
if siteID == "" {
103+
// There is a bug in Terraform: https://github.com/hashicorp/terraform/issues/23635
104+
// Specifically, upgrades/destroys are happening simulatneously and not honoroing
105+
// dependencies. In this case, it's possible that the site has already been deleted,
106+
// which means that all of the subresources will have been cleared out.
107+
// Ordinarily, this should return an error, but until this gets addressed, we're
108+
// going to simply return nil.
109+
// return fmt.Errorf("Incapsula site ID does not exist")
110+
return nil
111+
}
101112
if !ok {
102113
return fmt.Errorf("Incapsula Site ID does not exist")
103114
}
104115

105116
deliveryRulesListDTO, diags := client.ReadDeliveryRuleConfiguration(siteID, category)
106117

118+
// If the site has already been deleted then return nil
119+
// Otherwise check the delivery rules list
120+
siteIdInt, err := strconv.Atoi(siteID)
121+
if err != nil {
122+
return fmt.Errorf("Couldn't parse site ID: %s ", siteID)
123+
}
124+
_, siteStatusErr := client.SiteStatus(domain, siteIdInt)
125+
if siteStatusErr != nil {
126+
return nil
127+
}
128+
107129
if diags != nil {
108130
log.Printf("[ERROR] Failed to read simplified redirect rules in category %s for Site ID %s", category, siteID)
109131
return fmt.Errorf("failed to read simplified redirect rules in category %s for Site ID %s\"", category, siteID)

0 commit comments

Comments
 (0)