Every object in Azure is identified by a unique, hierarchy-based Resource ID.
Every object type has an associated range of different API versions that act as different schemas for that object type.
If you can derive the latest API version for a particular object type you can create, modify and delete objects using Azure’s REST API interfaces. The easiest way to find the latest version details about an Azure object type is by looking it up online with the Azure API reference: https://docs.microsoft.com/en-us/rest/api/azure/ .
Retrieving current API versions is also straightforward to do programmatically. This post will discuss how to use PowerShell and REST to create a dictionary / hash table of Azure object types and their latest API versions.
With any Azure object we need to be able to identify the ‘Provider’ and ‘Resource Types’ of the object. Thankfully, as Azure’s object ID structure is standardised, we can find that information at set places within the object ID string.
With the example above, the provider is obviously ‘Microsoft.Automation’ (the sixth element of the Object ID). The resource type is the 7th element of the Object ID and may be potentially overloaded with a 9th element (if it exists). These three aspects are combined to determine an object type that will correspond to specific API versions.
This becomes really useful when we know that using a GET as an API call against the root ‘provider’ namespace with a subscription will dump all information about namespaces, resource types, locations and API versions as a response.
"https://management.azure.com/subscriptions/
It becomes possible to parse the output into a dictionary or lookup table that can be used with automation elsewhere. I’m going to use two simple functions for this purpose.
I am going to use one function to find the latest version of dates when dates are passed from the PowerShell pipeline.
function Get-Latest {
Begin { $latest = $null }
Process {
if ($_ -gt $latest) { $latest = $_ }
}
End { $latest }
}
The second function needs an authorised header to be able to make requests against the Azure management point. It also needs a valid Subscription ID that corresponds to the header. This function is going to take an up-to-date dump of Provider information to create a lookup table / dictionary.
function Get-AzureAPIVersions(){
param(
[parameter( Mandatory = $true)]
[string]$header,
[parameter( Mandatory = $true)]
[string]$SubscriptionID
)
$dict = @{}
$uri = "https://management.azure.com/subscriptions/$($SubscriptionID)/providers/?api-version=2015-01-01"
$result = Invoke-RestMethod -Uri $uri -Method GET -Headers $authHeader
$namespaces = $result.value
foreach ($namespace in $namespaces){
foreach ($resource in $namespace.resourceTypes){
#Add Provider Plus Resource Type
$dict.Add("$($namespace.namespace)/$($resource.resourceType)",$($resource.apiVersions | Get-latest) )
}
}
#return dictionary
$dict
}
I’m creating an authorised header using PowerShell and a module I have shared here.
This allows me to use the produced hash table to provide the latest API version for that particular object.
Being able to programmatically determine the latest API version for an object will let me use generic commands to create, modify and delete Azure objects using just their Id.
- Log in to post comments