From e9e1acb73d545d1fcf5d00154456f6951a967c3c Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Thu, 17 Jan 2019 13:08:29 +0100 Subject: [PATCH] r/cosmosdb: handling when the resource has been deleted Fixes #2697 --- azurerm/resource_arm_cosmos_db_account.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/azurerm/resource_arm_cosmos_db_account.go b/azurerm/resource_arm_cosmos_db_account.go index 7774bd02171a..9673bcb9f6d3 100644 --- a/azurerm/resource_arm_cosmos_db_account.go +++ b/azurerm/resource_arm_cosmos_db_account.go @@ -601,6 +601,12 @@ func resourceArmCosmosDBAccountRead(d *schema.ResourceData, meta interface{}) er // implying that it also returns the read only keys, however this appears to not be the case keys, err := client.ListKeys(ctx, resourceGroup, name) if err != nil { + if utils.ResponseWasNotFound(keys.Response) { + log.Printf("[DEBUG] Keys were not found for CosmosDB Account %q (Resource Group %q) - removing from state!", name, resourceGroup) + d.SetId("") + return nil + } + return fmt.Errorf("[ERROR] Unable to List Write keys for CosmosDB Account %s: %s", name, err) } d.Set("primary_master_key", keys.PrimaryMasterKey) @@ -608,6 +614,12 @@ func resourceArmCosmosDBAccountRead(d *schema.ResourceData, meta interface{}) er readonlyKeys, err := client.ListReadOnlyKeys(ctx, resourceGroup, name) if err != nil { + if utils.ResponseWasNotFound(keys.Response) { + log.Printf("[DEBUG] Read Only Keys were not found for CosmosDB Account %q (Resource Group %q) - removing from state!", name, resourceGroup) + d.SetId("") + return nil + } + return fmt.Errorf("[ERROR] Unable to List read-only keys for CosmosDB Account %s: %s", name, err) } d.Set("primary_readonly_master_key", readonlyKeys.PrimaryReadonlyMasterKey) @@ -615,8 +627,15 @@ func resourceArmCosmosDBAccountRead(d *schema.ResourceData, meta interface{}) er connStringResp, err := client.ListConnectionStrings(ctx, resourceGroup, name) if err != nil { + if utils.ResponseWasNotFound(keys.Response) { + log.Printf("[DEBUG] Connection Strings were not found for CosmosDB Account %q (Resource Group %q) - removing from state!", name, resourceGroup) + d.SetId("") + return nil + } + return fmt.Errorf("[ERROR] Unable to List connection strings for CosmosDB Account %s: %s", name, err) } + var connStrings []string if connStringResp.ConnectionStrings != nil { connStrings = make([]string, len(*connStringResp.ConnectionStrings))