Menu Close

Creating an API to find an Azure resource abbreviation

In my previous blogpost, I talked about a way to use a bicep module to set a naming convention standard. In this post, I want to look at a specific part of that naming convention. I have created an API to find an Azure resource abbreviation.
In most naming conventions I have seen at companies, the resource type is mentioned in the name of the resource. To save space, an abbreviation is used. It is possible to come up with your own abbreviations, but that requires keeping track of your own standard (and invites people in your company to make up their own).

A solution is the list that is provided by Microsoft. This gives a default abbreviation for all Azure resources. Much easier than keeping track of your own list! You can find the page in the Microsoft Docs.
So this works well, but means you need to check the page to see the correct abbreviation. That sounds like manual work! I decided to see if this could be automated.

The solution: an API to find an Azure resource abbreviation

With the API I came up with, you can get the resource abbreviation based on the Resource Name or the Resource Namespace. It can work with both a body or a query and it supports wildcards.

I have made the code available on GitHub. You can take the code and host the function on your own Azure tenant. For an easy implementation, I have also made my own function publicly available. You are free to use it for your own automation. There is a disclaimer though: This is an as-is solution without guaranties or support.

The function is running on PowerShell. It is on a consumption plan, so initial calls might take a few moments. Let’s look at what is going on.

Module PowerHTML

The function takes information from an HTML table and converts it to a JSON file. In Windows PowerShell, that was a default functionality. But in PowerShell 6+, I had to dig a little deeper. Luckily Justin Grote wrote the PowerHTML module. The Function uses it to get the correct information from the Microsoft Docs page.

Collecting the current abbreviations

This function runs once a day. It takes the information from the Microsoft Docs pages and stores it in a JSON file. That file is stored in a blob container, using the storage blob bindings.

Passing the information to the caller

A caller can make a HTTP call to the function, like demonstrated in this post. A blob input binding is used to get the information from the storage account. The call is compared to that file to return the correct information.

Using the API to find an Azure resource abbreviation

I have stored my API under the following URL: https://resourceabbreviation.azurewebsites.net/api/abbreviation

The supported parameters are:

  • resourcename
  • resourcenamespace
  • resourceabbreviation
  • wildcard

Let’s see some examples.

Get the abbreviation for a virtual machine based on the namespace

To find out what the abbreviation for a virtual machine is, we can use the namespace: Microsoft.Computer/virtualmachines. You can define this with a parameter in the URL:

$URL = "https://resourceabbreviation.azurewebsites.net/api/abbreviation?resourcenamespace=microsoft.compute/virtualmachines"
Invoke-RestMethod $URL

The other option is to define the namespace in a body that you can pass to the api

$Body = @{
    resourcenamespace = "Microsoft.Compute/virtualMachines"
}
$URL = "https://resourceabbreviation.azurewebsites.net/api/abbreviation"
Invoke-RestMethod $URL -Body ( $Body | ConvertTo-Json)

Get the abbreviation for a virtual machine based on the resource name

The resource name for a virtual machine contains a space. That means you can’t create a parameter as part of the URL. But you are able to call it by using a body.

$Body = @{
    resourcename = "virtual Machine"
}
$URL = "https://resourceabbreviation.azurewebsites.net/api/abbreviation"
Invoke-RestMethod $URL -Body ( $Body | ConvertTo-Json)

Get the abbreviation for anything that has to do with storage

The api supports wildcards. For example if you only know part of the name or if you want all abbreviations that have to do with a certain resource. To use them, use the syntax Wildcard=true

$BaseURL = "https://resourceabbreviation.azurewebsites.net/api/abbreviation"

Invoke-RestMethod -URI ($BaseURL + "?resourcename=storage&wildcard=true")

Check if you have the correct abbreviation

It is possible to check what resources are connected to an abbreviation
Let’s see what comes up when you use log.

$BaseURL = "https://resourceabbreviation.azurewebsites.net/api/abbreviation"

Invoke-RestMethod -URI ($BaseURL + "?resourceabbreviation=log")

Conclusion

So this is how I created an API to find an Azure resource abbreviation. I hope you can find some use for it when automating the creating of Azure Resources. If you have any questions, leave them in the comments.

Leave a Reply

Your email address will not be published.