4

I'd like to create virtual networks in every location in Azure that can support them, using Azure python SDK. In the code below I'm limiting only to location germanynorth, but that is just to help reproduce the issue.

from azure.common.client_factory import get_client_from_auth_file
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.subscription import SubscriptionClient
from azure.mgmt.network import NetworkManagementClient

get_client_from_auth_file(ComputeManagementClient)
for location in get_client_from_auth_file(SubscriptionClient).subscriptions.list_locations(get_subscription_id()):
    if location.name == 'germanynorth':
        get_client_from_auth_file(NetworkManagementClient).virtual_networks.create_or_update(
            resource_group_name=RESOURCE_GROUP_NAME,
            virtual_network_name='test-network',
            parameters={'location': location.name, 'address_space': {'address_prefixes': ['10.0.0.0/16']}, }
        )

When running this I get the error:

msrestazure.azure_exceptions.CloudError: Azure Error: LocationNotAvailableForResourceType
Message: The provided location 'germanynorth' is not available for resource type 'Microsoft.Network/virtualNetworks'. List of available regions for the resource type is 'westus,eastus,northeurope,westeurope,eastasia,southeastasia,northcentralus,southcentralus,centralus,eastus2,japaneast,japanwest,brazilsouth,australiaeast,australiasoutheast,centralindia,southindia,westindia,canadacentral,canadaeast,westcentralus,westus2,ukwest,uksouth,koreacentral,koreasouth,francecentral,australiacentral,southafricanorth,uaenorth,switzerlandnorth,germanywestcentral,norwayeast'.

Very helpfully, the error includes a list of all the regions where virtualNetworks could be created, but of course this list will change over time.

What API in Azure can I use to figure out what locations (regions?) support virtual networks?

Thanks!

RoeyPrat
  • 73
  • 5

3 Answers3

1

You can use Azure resource providers and types. You can refer the Microsoft article on the resource provider.

PowerShell script to get all supported azure regions to create Azure key Vault.

$locations = (((Get-AzResourceProvider -ProviderNamespace Microsoft.KeyVault)| Where-Object RegistrationState -eq "Registered").ResourceTypes | Where-Object ResourceTypeName -eq vaults).Locations
Santhosh
  • 671
  • 6
  • 7
0

There is an API with which we can list out all the available location under a given subscription id (I am not sure if there is an equivalent version of this API that can filter by resource type) -

API - GET https://management.azure.com/subscriptions/{subscriptionId}/locations?api-version=2020-01-01

Quoting the documentation

This operation provides all the locations that are available for resource providers; however, each resource provider may support a subset of this list.

Perhaps, you can iterate through the list of available locations in your subscription and put it in a try/except block to create Vnet against all available regions in your subscription?

EDIT: Apologies, I realized it late, you are already iterating through the list of locations under your subscription id. I guess it's a matter of writing the code beneath the for loop in a try/except block, wherein you can except that particular error type and continue with your loop?

Zoe
  • 27,060
  • 21
  • 118
  • 148
factorThis
  • 74
  • 5
  • Thanks! I mean, it's possible to try and except all, but I was hoping for a more elegant solution. The fact that they have the list in the error text means they have the needed info in a single place, but I can't access it – RoeyPrat Mar 02 '21 at 15:12
0

I actually figured out my own bounty.

https://learn.microsoft.com/en-us/rest/api/resources/providers/get

"Gets the specified resource provider."
I don't think this describes what it actually does, which is why I didn't find it. I had to just basically test a bunch of APIs to see what returned what.
This API will return a list of available locations for the provided resource type (in your subscription).

I just wish it didn't only return a list (East US) but also with the with a short code (code:location), for example (eastus:East US).

So to answer the actual question, if you can't use your python library for this, an option would be to use this REST API:
GET https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Network?api-version=2021-04-01

Your list of locations will be under the json path: {response}.resourceTypes[0].locations, where resourceType eq "virtualNetworks"

To actually get the locationCode (short location code) you can query this API:
https://learn.microsoft.com/en-us/rest/api/resources/subscriptions/list-locations
then map your location from above with this response to get the short code ('East US' -> 'eastus'), which can be used in other rest APIs to create for example a virtual network.

HoverCatz
  • 65
  • 1
  • 5
  • 19