Skip to content

[CosmosDB] Fix #32608: az cosmosdb restore: Fix "Database Account does not exist" error during polling#32752

Merged
calvinhzy merged 16 commits intoAzure:devfrom
jcassanji-southworks:32608-az-cosmosdb-restore-wrongfully-appends-location-to-name-causing-it-to-fail-fix
Feb 24, 2026
Merged

[CosmosDB] Fix #32608: az cosmosdb restore: Fix "Database Account does not exist" error during polling#32752
calvinhzy merged 16 commits intoAzure:devfrom
jcassanji-southworks:32608-az-cosmosdb-restore-wrongfully-appends-location-to-name-causing-it-to-fail-fix

Conversation

@jcassanji-southworks
Copy link
Contributor

@jcassanji-southworks jcassanji-southworks commented Feb 5, 2026

Related command
az cosmosdb restore

Description
This PR fixes a known issue (GitHub Issue #32608) where az cosmosdb restore command fails with a (Forbidden) Database Account <account-name>-<location> does not exist error.

The Azure backend service occasionally appends the location name to the account name during the polling operation of a restore action. This causes the CLI to poll a non-existent account name (e.g., myaccount-westeurope instead of myaccount), resulting in a 403 Forbidden error.

Changes:

  • Implemented a workaround in custom.py to catch and suppress the specific HttpResponseError (403 Forbidden with "does not exist") during the polling of client.begin_create_or_update.
  • Added a fallback check using client.get to verify if the account was successfully created/restored when this error is encountered.
  • Added comprehensive unit tests in test_cosmosdb_backuprestore_scenario.py to ensure the fix handles the specific error condition correctly without masking other legitimate errors.

Testing Guide
Run the newly added unit tests to verify the fix:

python -m unittest azure.cli.command_modules.cosmosdb.tests.latest.test_cosmosdb_backuprestore_scenario.CosmosDBRestoreUnitTests

History Notes

[CosmosDB] Fix #32608: az cosmosdb restore: Fix "Database Account does not exist" error during polling


This checklist is used to make sure that common guidelines for a pull request are followed.

Copilot AI review requested due to automatic review settings February 5, 2026 22:05
@azure-client-tools-bot-prd
Copy link

azure-client-tools-bot-prd bot commented Feb 5, 2026

️✔️AzureCLI-FullTest
️✔️acr
️✔️latest
️✔️3.12
️✔️3.13
️✔️acs
️✔️latest
️✔️3.12
️✔️3.13
️✔️advisor
️✔️latest
️✔️3.12
️✔️3.13
️✔️ams
️✔️latest
️✔️3.12
️✔️3.13
️✔️apim
️✔️latest
️✔️3.12
️✔️3.13
️✔️appconfig
️✔️latest
️✔️3.12
️✔️3.13
️✔️appservice
️✔️latest
️✔️3.12
️✔️3.13
️✔️aro
️✔️latest
️✔️3.12
️✔️3.13
️✔️backup
️✔️latest
️✔️3.12
️✔️3.13
️✔️batch
️✔️latest
️✔️3.12
️✔️3.13
️✔️batchai
️✔️latest
️✔️3.12
️✔️3.13
️✔️billing
️✔️latest
️✔️3.12
️✔️3.13
️✔️botservice
️✔️latest
️✔️3.12
️✔️3.13
️✔️cdn
️✔️latest
️✔️3.12
️✔️3.13
️✔️cloud
️✔️latest
️✔️3.12
️✔️3.13
️✔️cognitiveservices
️✔️latest
️✔️3.12
️✔️3.13
️✔️compute_recommender
️✔️latest
️✔️3.12
️✔️3.13
️✔️computefleet
️✔️latest
️✔️3.12
️✔️3.13
️✔️config
️✔️latest
️✔️3.12
️✔️3.13
️✔️configure
️✔️latest
️✔️3.12
️✔️3.13
️✔️consumption
️✔️latest
️✔️3.12
️✔️3.13
️✔️container
️✔️latest
️✔️3.12
️✔️3.13
️✔️containerapp
️✔️latest
️✔️3.12
️✔️3.13
️✔️core
️✔️latest
️✔️3.12
️✔️3.13
️✔️cosmosdb
️✔️latest
️✔️3.12
️✔️3.13
️✔️databoxedge
️✔️latest
️✔️3.12
️✔️3.13
️✔️dls
️✔️latest
️✔️3.12
️✔️3.13
️✔️dms
️✔️latest
️✔️3.12
️✔️3.13
️✔️eventgrid
️✔️latest
️✔️3.12
️✔️3.13
️✔️eventhubs
️✔️latest
️✔️3.12
️✔️3.13
️✔️feedback
️✔️latest
️✔️3.12
️✔️3.13
️✔️find
️✔️latest
️✔️3.12
️✔️3.13
️✔️hdinsight
️✔️latest
️✔️3.12
️✔️3.13
️✔️identity
️✔️latest
️✔️3.12
️✔️3.13
️✔️iot
️✔️latest
️✔️3.12
️✔️3.13
️✔️keyvault
️✔️latest
️✔️3.12
️✔️3.13
️✔️lab
️✔️latest
️✔️3.12
️✔️3.13
️✔️managedservices
️✔️latest
️✔️3.12
️✔️3.13
️✔️maps
️✔️latest
️✔️3.12
️✔️3.13
️✔️marketplaceordering
️✔️latest
️✔️3.12
️✔️3.13
️✔️monitor
️✔️latest
️✔️3.12
️✔️3.13
️✔️mysql
️✔️latest
️✔️3.12
️✔️3.13
️✔️netappfiles
️✔️latest
️✔️3.12
️✔️3.13
️✔️network
️✔️latest
️✔️3.12
️✔️3.13
️✔️policyinsights
️✔️latest
️✔️3.12
️✔️3.13
️✔️postgresql
️✔️latest
️✔️3.12
️✔️3.13
️✔️privatedns
️✔️latest
️✔️3.12
️✔️3.13
️✔️profile
️✔️latest
️✔️3.12
️✔️3.13
️✔️rdbms
️✔️latest
️✔️3.12
️✔️3.13
️✔️redis
️✔️latest
️✔️3.12
️✔️3.13
️✔️relay
️✔️latest
️✔️3.12
️✔️3.13
️✔️resource
️✔️latest
️✔️3.12
️✔️3.13
️✔️role
️✔️latest
️✔️3.12
️✔️3.13
️✔️search
️✔️latest
️✔️3.12
️✔️3.13
️✔️security
️✔️latest
️✔️3.12
️✔️3.13
️✔️servicebus
️✔️latest
️✔️3.12
️✔️3.13
️✔️serviceconnector
️✔️latest
️✔️3.12
️✔️3.13
️✔️servicefabric
️✔️latest
️✔️3.12
️✔️3.13
️✔️signalr
️✔️latest
️✔️3.12
️✔️3.13
️✔️sql
️✔️latest
️✔️3.12
️✔️3.13
️✔️sqlvm
️✔️latest
️✔️3.12
️✔️3.13
️✔️storage
️✔️latest
️✔️3.12
️✔️3.13
️✔️synapse
️✔️latest
️✔️3.12
️✔️3.13
️✔️telemetry
️✔️latest
️✔️3.12
️✔️3.13
️✔️util
️✔️latest
️✔️3.12
️✔️3.13
️✔️vm
️✔️latest
️✔️3.12
️✔️3.13

@azure-client-tools-bot-prd
Copy link

azure-client-tools-bot-prd bot commented Feb 5, 2026

️✔️AzureCLI-BreakingChangeTest
️✔️Non Breaking Changes

@yonzhan
Copy link
Collaborator

yonzhan commented Feb 5, 2026

Thank you for your contribution! We will review the pull request and get back to you soon.

@github-actions
Copy link

github-actions bot commented Feb 5, 2026

The git hooks are available for azure-cli and azure-cli-extensions repos. They could help you run required checks before creating the PR.

Please sync the latest code with latest dev branch (for azure-cli) or main branch (for azure-cli-extensions).
After that please run the following commands to enable git hooks:

pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>

@microsoft-github-policy-service microsoft-github-policy-service bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. CosmosDB az cosmosdb Auto-Assign Auto assign by bot labels Feb 5, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR attempts to fix a known issue where az cosmosdb restore fails with a "Database Account does not exist" error during polling. The Azure backend service occasionally appends the location name to the account name during restore operations, causing a 403 Forbidden error. The fix implements error handling in _create_database_account to catch this specific error and fall back to using client.get() to verify account creation.

Changes:

  • Added exception handling in custom.py to catch and suppress specific 403 "does not exist" errors during restore operations
  • Added comprehensive unit tests in test_cosmosdb_backuprestore_scenario.py to verify the error handling logic

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.

File Description
src/azure-cli/azure/cli/command_modules/cosmosdb/custom.py Added try-except block around async_docdb_create.result() to handle the service bug where location is appended to account name, with fallback to client.get()
src/azure-cli/azure/cli/command_modules/cosmosdb/tests/latest/test_cosmosdb_backuprestore_scenario.py Added unit test class CosmosDBRestoreUnitTests with three test cases covering the forbidden error handling, other errors, and normal create operations
Comments suppressed due to low confidence (4)

src/azure-cli/azure/cli/command_modules/cosmosdb/custom.py:422

  • The comment label "# Workaround" on this line is misleading because it applies to all account creation operations, not just the workaround for the specific restore bug. This line should always execute to retrieve the final account state, but the comment suggests it's part of the temporary workaround. Consider clarifying the comment or removing it since this is standard behavior.
    src/azure-cli/azure/cli/command_modules/cosmosdb/custom.py:422
  • Missing error handling: When the specific 403 error is caught and client.get() is called as a fallback, there's no verification that the restore operation actually succeeded. The code should check the provisioning_state or other indicators from the account returned by client.get() to ensure the operation completed successfully. Without this check, a failed restore could be silently treated as successful.
            raise ex
    docdb_account = client.get(resource_group_name, account_name)  # Workaround
    return docdb_account


src/azure-cli/azure/cli/command_modules/cosmosdb/custom.py:422

  • Missing error handling: If the workaround triggers but the account genuinely doesn't exist yet (e.g., the operation is still in progress), client.get() could raise a ResourceNotFoundError. This exception should be caught and handled appropriately, potentially with retry logic or a clearer error message explaining that the restore operation status is unclear.
            raise ex
    docdb_account = client.get(resource_group_name, account_name)  # Workaround
    return docdb_account


src/azure-cli/azure/cli/command_modules/cosmosdb/custom.py:422

  • Critical bug: Line 422 client.get() is now always executed, even when the operation succeeds normally. This changes the behavior for all database account creation operations, not just restore operations with the specific error.

The logic should be:

  1. If no exception occurs, use the result from async_docdb_create.result() and return it
  2. If the specific 403 error occurs during restore, fall back to client.get()

Current implementation causes an unnecessary additional API call for every successful account creation, and more importantly, the result from the successful poller.result() is discarded. The variable docdb_account is assigned on line 413 but then immediately overwritten on line 422 regardless of whether an exception occurred.

Fix: Move line 422 inside the exception handler (after line 419) so it only executes when the specific error is caught.

    try:
        docdb_account = async_docdb_create.result()
    except HttpResponseError as ex:
        if is_restore_request and ex.status_code == 403 and "does not exist" in str(ex):
            pass
        else:
            raise ex
    docdb_account = client.get(resource_group_name, account_name)  # Workaround
    return docdb_account



💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

jcassanji-southworks and others added 6 commits February 5, 2026 19:16
…test_cosmosdb_backuprestore_scenario.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…test_cosmosdb_backuprestore_scenario.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…o-name-causing-it-to-fail-fix' of https://github.com/jcassanji-southworks/azure-cli into 32608-az-cosmosdb-restore-wrongfully-appends-location-to-name-causing-it-to-fail-fix
@yonzhan yonzhan assigned calvinhzy and unassigned evelyn-ys Feb 5, 2026
@calvinhzy
Copy link
Member

@jcassanji-southworks please fix CI

@jcassanji-southworks jcassanji-southworks changed the title [CosmosDB] Fix #32608: az cosmosdb restore: Fix "Database Account does not exist" error during polling [CosmosDB] Fix #32608: az cosmosdb restore: Fix "Database Account does not exist" error during polling Feb 17, 2026
@jcassanji-southworks
Copy link
Contributor Author

jcassanji-southworks commented Feb 18, 2026

@jcassanji-southworks please fix CI

Done @calvinhzy

@calvinhzy calvinhzy merged commit 2cf7b76 into Azure:dev Feb 24, 2026
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Auto-Assign Auto assign by bot CosmosDB az cosmosdb customer-reported Issues that are reported by GitHub users external to the Azure organization.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

az cosmosdb restore wrongfully appends location to name causing it to fail

5 participants