Skip to content

Commit

Permalink
r/cosmosdb: handling when the resource has been deleted (#2702)
Browse files Browse the repository at this point in the history
Fixes #2697
  • Loading branch information
tombuildsstuff authored and katbyte committed Jan 17, 2019
1 parent 878dd8c commit 9b104e4
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions azurerm/resource_arm_cosmos_db_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,22 +601,41 @@ 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)
d.Set("secondary_master_key", keys.SecondaryMasterKey)

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)
d.Set("secondary_readonly_master_key", readonlyKeys.SecondaryReadonlyMasterKey)

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))
Expand Down

0 comments on commit 9b104e4

Please sign in to comment.