I am trying to verify the existence of some resources in my tenant.
I use the ResourceManagementClient
to do so, either the check_existence_by_id
method or get_by_id
as described in this issue.
My script uses a constant api_version for now, "2022-09-01", which apparently isn't supported for every resource type, because I sometimes get a NoRegisteredProviderFound
like this:
HttpResponseError: (NoRegisteredProviderFound) No registered resource provider found for location 'westeurope' and API version '2022-03-01' for type 'firewallPolicies'. The supported api-versions are '2019-06-01, 2019-07-01, 2019-08-01, 2019-09-01, 2019-11-01, 2019-12-01, 2020-01-01, 2020-03-01, 2020-04-01, 2020-05-01, 2020-06-01, 2020-07-01, 2020-08-01, 2020-11-01, 2021-01-01, 2021-02-01, 2021-03-01, 2021-04-01, 2021-05-01, 2021-06-01, 2021-08-01, 2021-12-01, 2022-01-01, 2022-05-01, 2022-07-01, 2022-09-01'. The supported locations are 'qatarcentral, uaenorth, australiacentral2, uaecentral, germanynorth, centralindia, koreasouth, switzerlandnorth, switzerlandwest, japanwest, francesouth, southafricawest, westindia, canadaeast, southindia, germanywestcentral, norwayeast, norwaywest, southafricanorth, eastasia, southeastasia, koreacentral, brazilsouth, brazilsoutheast, westus3, jioindiawest, swedencentral, japaneast, ukwest, westus, eastus, northeurope, westeurope, westcentralus, southcentralus, australiaeast, australiacentral, australiasoutheast, uksouth, eastus2, westus2, northcentralus, canadacentral, francecentral, centralus'.
Code: NoRegisteredProviderFound
From what I've seen, the interfaces I use remain similar to the client for each version, but the Azure API service for some reason doesn't support them all.
How can I determine a right version to use for each resource type during runtime? the list of resources I want to query is huge and has many types of resources.
Edit: The answer that @SiddheshDesai provided works perfectly but if you face a similar issue when using the ResourceMangamentClient
I also recommend trying the ResourceGraphClient
from azure.mgmt.resoucegraph
. It provides an interface that lets you query the same information very easily.
You can determine the right version for each resource types in Azure with the Python code below:-
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
# Initialize the Azure credentials
credential = AzureCliCredential()
subscription_id = 'subscription-id'
resource_client = ResourceManagementClient(credential, subscription_id)
provider_namespace = 'Microsoft.Network' # replace with your desired provider namespace
provider = resource_client.providers.get(provider_namespace)
for resource_type in provider.resource_types:
print(f"Resource Type: {resource_type.resource_type}")
print(f"Latest API Version: {resource_type.api_versions[0]}")
print('\n')
Output:-
The above code listed the Resource types and their API versions for the resource provider Microsoft.Network.
Now, To check if the particular resource exists by its Id with the resource type, you can use the python script below:-
I have deployed one V-NET in my account:-
Python code:-
rom azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
# Initialize the Azure credentials
credential = AzureCliCredential()
subscription_id = 'subscription-id'
resource_client = ResourceManagementClient(credential, subscription_id)
# Dictionary to store the API version for each resource type
api_versions = {}
# List of resource types to check existence for
resource_types = ['Microsoft.Network/virtualNetworks', 'Microsoft.Compute/virtualMachines', 'Microsoft.Storage/storageAccounts']
for resource_type in resource_types:
# Split the resource type into its provider namespace and resource type
provider_namespace, type_name = resource_type.split('/', 1)
# Check if the API version for this resource type has already been determined
if type_name in api_versions:
api_version = api_versions[type_name]
else:
# Get the list of API versions for this resource type
provider = resource_client.providers.get(provider_namespace)
api_versions[type_name] = provider.resource_types[0].api_versions[0]
api_version = api_versions[type_name]
# Use the determined API version to check if the resource exists
resource_id = '/subscriptions/{0}/resourceGroups/{1}/providers/{2}/{3}'.format(subscription_id, 'resource-group-name', provider_namespace, type_name)
try:
resource = resource_client.resources.get_by_id(resource_id, api_version)
print(f"{resource_type} exists.")
except:
print(f"{resource_type} does not exist.")
Output:-