Checking existance for Azure Dashboard with Python SDK returning always "false"

Javier MARASCO 1 Reputation point
2022-07-22T19:53:50.7+00:00

Hello everyone, I am making a python script that will create an Azure Dashboard (that is working fine) but if another dashboard is requested with the same name as an existing one, the script should tell the user something like "A dashboard with that name already exists", for checking if the dashboard already exist I am using the "check_existence_by_id" for resources (using the Python SDK).

My problem is that no matter if the dashboard exists or not, the method is always returning "false", I tried with multiple APIs (as the API version is required for this method) and of course the ResourceID I am using is correct, I checked it in the Azure Portal.

Here is the code I am using:

from azure.identity import DefaultAzureCredential  
from azure.mgmt.resource import ResourceManagementClient  
credential = DefaultAzureCredential()  
resource_client = ResourceManagementClient(credential=credential, subscription_id='xxxxxxxxx-xxxx-xxxxx-xxxxx-xxxxxxxxxxxxxxxx')  
Dashboard_Resource_Id='/subscriptions/xxxxxxxxx-xxxx-xxxxx-xxxxx-xxxxxxxxxxxxxxxx/resourceGroups/someresourcegroup/providers/Microsoft.Portal/dashboards/MetricDashboard'  
resource_client.resources.check_existence_by_id(resource_id=Dashboard_Resource_Id,api_version='2015-08-01-preview')  

I am passing credentials using environment variables so the authentication works, I checked this because the script actually creates the dashboard, but the second time I run it as it returnd "false" when asking if the dashboard exists, it tries to create the dashboard again.

Could anyone point me in the right direction on this? why is this method not working for the dashboard but seems to work fine for other resources (like storage accounts).

Thank you very much for any information or tips!

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,645 questions
{count} votes

1 answer

Sort by: Most helpful
  1. AnuragSingh-MSFT 21,546 Reputation points Moderator
    2022-07-31T10:26:36.607+00:00

    @Anonymous , I tested with check_existence_by_id and can see similar behavior. Also, as mentioned here and in GitHub Issue here, check_existence_by_id does not work with all resources. For this particular issue, you may create a new issue on GitHub for Azure SDK forPython using this Link

    As a workaround, the following code snippet should help you achieve similar result using get_by_id

    try:  
      result = resource_client.resources.get_by_id(Dashboard_Resource_Id, api_version='2020-09-01-preview')  
      //perform operation, if resource exists.   
    except:  
       //perform operation, if resource does not exist.  
    

    You can use the Try It option as available in the link below, to see the behavior using REST api call for get_by_id.

    Please let me know if you have any questions.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.