From 77105d85df203de94c2671edb2c9abbae0c7c0be Mon Sep 17 00:00:00 2001 From: kt Date: Thu, 27 Jun 2019 23:46:02 -0700 Subject: [PATCH 1/5] azurerm_cosmosdb_account: ignore 500 responses from CheckNameExists() call --- azurerm/resource_arm_cosmosdb_account.go | 5 ++++- azurerm/utils/response.go | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/azurerm/resource_arm_cosmosdb_account.go b/azurerm/resource_arm_cosmosdb_account.go index cd82d2a204b4..4ca50767d046 100644 --- a/azurerm/resource_arm_cosmosdb_account.go +++ b/azurerm/resource_arm_cosmosdb_account.go @@ -328,7 +328,10 @@ func resourceArmCosmosDbAccountCreate(d *schema.ResourceData, meta interface{}) r, err := client.CheckNameExists(ctx, name) if err != nil { - return fmt.Errorf("Error checking if CosmosDB Account %q already exists (Resource Group %q): %+v", name, resourceGroup, err) + // todo remove when https://github.com/Azure/azure-sdk-for-go/issues/5157 is fixed + if !utils.ResponseWasStatusCode(r, 500) { + return fmt.Errorf("Error checking if CosmosDB Account %q already exists (Resource Group %q): %+v", name, resourceGroup, err) + } } if !utils.ResponseWasNotFound(r) { return fmt.Errorf("CosmosDB Account %s already exists, please import the resource via terraform import", name) diff --git a/azurerm/utils/response.go b/azurerm/utils/response.go index 716ffa8449f7..8742b6fa924c 100644 --- a/azurerm/utils/response.go +++ b/azurerm/utils/response.go @@ -8,7 +8,7 @@ import ( ) func ResponseWasNotFound(resp autorest.Response) bool { - return responseWasStatusCode(resp, http.StatusNotFound) + return ResponseWasStatusCode(resp, http.StatusNotFound) } func ResponseErrorIsRetryable(err error) bool { @@ -26,7 +26,7 @@ func ResponseErrorIsRetryable(err error) bool { return false } -func responseWasStatusCode(resp autorest.Response, statusCode int) bool { // nolint: unparam +func ResponseWasStatusCode(resp autorest.Response, statusCode int) bool { // nolint: unparam if r := resp.Response; r != nil { if r.StatusCode == statusCode { return true From 627b5a33d4f7ff861c9757a4abdb9907a4e02796 Mon Sep 17 00:00:00 2001 From: kt Date: Fri, 28 Jun 2019 00:02:29 -0700 Subject: [PATCH 2/5] fix logic --- azurerm/resource_arm_cosmosdb_account.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/azurerm/resource_arm_cosmosdb_account.go b/azurerm/resource_arm_cosmosdb_account.go index 4ca50767d046..4135c54af241 100644 --- a/azurerm/resource_arm_cosmosdb_account.go +++ b/azurerm/resource_arm_cosmosdb_account.go @@ -332,10 +332,12 @@ func resourceArmCosmosDbAccountCreate(d *schema.ResourceData, meta interface{}) if !utils.ResponseWasStatusCode(r, 500) { return fmt.Errorf("Error checking if CosmosDB Account %q already exists (Resource Group %q): %+v", name, resourceGroup, err) } + } else { + if !utils.ResponseWasNotFound(r) { + return fmt.Errorf("CosmosDB Account %s already exists, please import the resource via terraform import", name) + } } - if !utils.ResponseWasNotFound(r) { - return fmt.Errorf("CosmosDB Account %s already exists, please import the resource via terraform import", name) - } + //hacky, todo fix up once deprecated field 'failover_policy' is removed var geoLocations []documentdb.Location From ad485f783ac44ecc6c0a9a93f78c97b9ebaaa49a Mon Sep 17 00:00:00 2001 From: kt Date: Fri, 28 Jun 2019 09:44:30 -0700 Subject: [PATCH 3/5] Update azurerm/resource_arm_cosmosdb_account.go Co-Authored-By: Tom Harvey --- azurerm/resource_arm_cosmosdb_account.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/resource_arm_cosmosdb_account.go b/azurerm/resource_arm_cosmosdb_account.go index 4135c54af241..b8cd8c729488 100644 --- a/azurerm/resource_arm_cosmosdb_account.go +++ b/azurerm/resource_arm_cosmosdb_account.go @@ -329,7 +329,7 @@ func resourceArmCosmosDbAccountCreate(d *schema.ResourceData, meta interface{}) r, err := client.CheckNameExists(ctx, name) if err != nil { // todo remove when https://github.com/Azure/azure-sdk-for-go/issues/5157 is fixed - if !utils.ResponseWasStatusCode(r, 500) { + if !utils.ResponseWasStatusCode(r, http.StatusInternalServerError) { return fmt.Errorf("Error checking if CosmosDB Account %q already exists (Resource Group %q): %+v", name, resourceGroup, err) } } else { From 708c72ccb1d57d3081ae5e2bec35fb5b65c77567 Mon Sep 17 00:00:00 2001 From: kt Date: Fri, 28 Jun 2019 10:30:52 -0700 Subject: [PATCH 4/5] fix build --- azurerm/resource_arm_cosmosdb_account.go | 1 + 1 file changed, 1 insertion(+) diff --git a/azurerm/resource_arm_cosmosdb_account.go b/azurerm/resource_arm_cosmosdb_account.go index b8cd8c729488..5d0c35869a35 100644 --- a/azurerm/resource_arm_cosmosdb_account.go +++ b/azurerm/resource_arm_cosmosdb_account.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "log" + `net/http` "regexp" "strings" "time" From c69e213a6b35b43a56cfb29950c3e807e48703d4 Mon Sep 17 00:00:00 2001 From: kt Date: Fri, 28 Jun 2019 10:32:04 -0700 Subject: [PATCH 5/5] make fmt --- azurerm/resource_arm_cosmosdb_account.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/azurerm/resource_arm_cosmosdb_account.go b/azurerm/resource_arm_cosmosdb_account.go index 5d0c35869a35..26f6bf271d66 100644 --- a/azurerm/resource_arm_cosmosdb_account.go +++ b/azurerm/resource_arm_cosmosdb_account.go @@ -5,7 +5,7 @@ import ( "context" "fmt" "log" - `net/http` + "net/http" "regexp" "strings" "time" @@ -339,7 +339,6 @@ func resourceArmCosmosDbAccountCreate(d *schema.ResourceData, meta interface{}) } } - //hacky, todo fix up once deprecated field 'failover_policy' is removed var geoLocations []documentdb.Location if _, ok := d.GetOk("geo_location"); ok {