pythonazureazure-sdk-python

Azure Python SDK - how can I know the latest API version for a resource ID?


I'm using the python Azure SDK, and for things like subnets, ipConfiguration objects will have a generic resource ID for the attached device - sometimes it's a NIC, or a PEP or something else. I know I can use the azure-mgmt-resources.ResourceManagementClient to get a generic resource object for one of these, BUT it requires an api_version which must be a valid version for the specific type of object!

Is there an API somewhere in the SDK to find these? e.g. API version for "Microsoft.Network/privateEndpoints" is "2024-01-01"

I have a lookup table for ones I use commonly, but it feels dirty.


Solution

  • To get latest API version of resource provider via Azure Python SDK, make use of below sample python code:

    from azure.identity import ClientSecretCredential
    from azure.mgmt.resource import ResourceManagementClient
    
    tenant_id = 'tenantId'
    client_id = 'appId'
    client_secret = 'secret'
    
    subscription_id = 'subId'
    
    credentials = ClientSecretCredential(tenant_id, client_id, client_secret)
    resource_client = ResourceManagementClient(credentials, subscription_id)
    
    provider_namespace = 'Microsoft.Network'
    resource_type_name = 'privateEndpoints'
    
    provider = resource_client.providers.get(provider_namespace)
    
    resource_type = next(rt for rt in provider.resource_types if rt.resource_type == resource_type_name)
    api_versions = resource_type.api_versions
    
    latest_api_version = api_versions[0]  
    
    print(f"The latest API version for {provider_namespace}/{resource_type_name} is: {latest_api_version}")
    

    Response:

    enter image description here

    You can replace resource_type_name value accordingly based on your requirement to get latest API versions of those.

    In my case, I used client credentials flow for authentication by granting Reader access to service principal under subscription level.