This article is contributed. See the original author and article here.

 


As you mature in your cloud journey you may start to be intrigued by the Azure REST API. I feel like sometimes our answer to almost anything is “we could do that through the REST API.” This is a true statement but when we are talking to Infrastructure Engineers a REST API solution might be intimidating. What I want to do in this blog is to use Logic App to make an Azure REST API call with the goal of taking away some of the stigma that you need to be a programmer to utilize API calls!   


 


Azure REST API 


Before we jump into Logic App look at our documentation on the Azure REST API: 


 


Azure REST API reference documentation | Microsoft Docs 


 


As you can see this library of APIs is extensive and powerful. That’s why you can do almost anything through the Azure REST API. For this blog I’m going to utilize the Virtual Network API for listing your Virtual Networks: 


 


Virtual Networks – List All (Azure Virtual Networks) | Microsoft Docs 


 


Most everyone has at least one Virtual Network in there subscription so this is a good one to start with. 


 


Virtual Network – List All 


 


We need to understand the API before we can call it. The first thing I always look at is the sample call: 


 


GET https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Network/virtualNetworks?api-version=2020-07-01 


 


Couple thing to note from this: 



  • This is a “GET” operation 

  • The values we need to provide will be in the {} block. So for this particular API we need to just provide the Subcription ID we want to query 


The other section that I always look at too is the “Sample” section which will have a sample response. So for this one we should expect the response to look like this: 


 

{ 
  "value": [ 
    { 
      "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualNetworks/vnet1", 
      "name": "vnet1", 
      "type": "Microsoft.Network/virtualNetworks", 
      "location": "westus", 
      "properties": { 
        "addressSpace": { 
          "addressPrefixes": [ 
            "10.0.0.0/8" 
          ] 
        }, 
        "dhcpOptions": { 
          "dnsServers": [] 
        }, 
        "subnets": [ 
          { 
            "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualNetworks/vnet1/subnets/test-1", 
            "name": "test-1", 
            "properties": { 
              "addressPrefix": "10.0.0.0/24", 
              "provisioningState": "Succeeded" 
            } 
          } 
        ], 
        "virtualNetworkPeerings": [], 
        "provisioningState": "Succeeded" 
      } 
    }, 
    {VNET2}, 
    {VNET3}, 
    {…} 
  ] 
} 

 


Logic App 


So how do we take advantage of Logic App to call this API? Well let’s start with a blank logic app. Here is some info if you’ve never created one: 


 


Quickstart – Create your first Logic Apps workflow – Azure portal – Azure Logic Apps | Microsoft Docs 


 


When you create a Logic App it always brings you to the designer page. You always need a trigger in your logic app and for testing I like to use the “When a HTTP Request is Received” (I can always change it later to something like a schedule). 


 


bwatts670_0-1615226064581.png


This will take you into the designer with the trigger already added. We are going to utilize the managed identity for the Logic App to do the REST API call. This is not enabled by default, so we need to enable it and give it some rights before we go further. Go ahead and save the Logic App in the designer. Then go to “Identity” under the “Settings” for your Logic App. 


bwatts670_1-1615226064623.png


Turn the status to “On” and click on “Save” 


bwatts670_2-1615226064587.png


Once it finishes saving click on the “Azure role assignments”: 


bwatts670_3-1615226064591.png


Click on “Add role assignment” and fill in the following: 



  • Scope: Subscription 

  • Subscription: Whatever subscription you plan to query 



  • Role: Reader 


bwatts670_4-1615226064627.png


 


Click on Save. At this point the Managed Identity has the permissions needed to query the REST API so we can move back to the Logic App Designer. 


 


In the designer, go ahead and click on the “New Step” below the trigger. 


Search for “http” and select the “HTTP” action 


bwatts670_5-1615226064595.png


 


 Then we can choose the basic HTTP Action 


bwatts670_6-1615226064597.png


 


Let’s go ahead and add the authentication before we fill anything out. Click on “Add new parameter” and check the box next to “Authentication”. You’ll need to click somewhere outside of the drop down to add it in so click for instance the “URI” field.  


bwatts670_7-1615226064631.png


 


Now we can fill out the required fields to call the REST API 



***Replace {subscriptionId} with the subscription id you want to query*** 



  • Authentication type: Managed identity 



bwatts670_8-1615226064633.png


 


Go ahead and save your Logic App and then click on Run. 


bwatts670_9-1615226064601.png


 


You should see the HTTP call succeed and in the “Body” section you should see your VNets. 


bwatts670_10-1615226064639.png


 


Congrats! You have authenticated to an Azure Rest API, made a request, and successfully gotten results. 


 


Using the Results 


I don’t want to leave you with just the results in the body of the HTTP action. Let’s actually do something with the results in Logic Apps. I would typically do some parsing of the JSON but to keep this simple being the focus here is the REST API and not Logic App we will keep the parsing of the JSON to a minimum. 


 


Go back into the Logic App Designer and click on “New Step” below the HTTP action.  


Search for “parse json” and then under the “Actions” section choose “Parse JSON 


bwatts670_11-1615226064604.png


 


 Click in the “Content” section and then from the “Dynamic content” choose “Body” 


bwatts670_12-1615226064607.png


 


 Click on the option to “Use sample payload to generate schema” 


bwatts670_13-1615226064609.png


 


 In the box that pops up you can paste in the below: 


 


***TIP: You can get the sample payload from the previous run we did. You can just grab the body from the “Output”*** 


 

{
  "value": [ 
    { 
      "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualNetworks/vnet1", 
      "name": "vnet1", 
      "type": "Microsoft.Network/virtualNetworks", 
      "location": "westus", 
      "properties": { 
        "addressSpace": { 
          "addressPrefixes": [ 
            "10.0.0.0/8" 
          ] 
        }, 
        "dhcpOptions": { 
          "dnsServers": [] 
        }, 
        "subnets": [ 
          { 
            "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualNetworks/vnet1/subnets/test-1", 
            "name": "test-1", 
            "properties": { 
              "addressPrefix": "10.0.0.0/24", 
              "provisioningState": "Succeeded" 
            } 
          } 
        ], 
        "virtualNetworkPeerings": [], 
        "provisioningState": "Succeeded" 
      } 
    } 
  ] 
} 

 


Click “Done” and it’ll generate the schema for you. 


 


Click on “New Step” below the “Parse JSON” 


 


Search for “create html table” and then under the “Actions” section choose “Create HTML table 


bwatts670_14-1615226064611.png


 


 Click in the “From” field and in “Dynamic content” search for “value”. Choose the “value” field from “Parse JSON” 


bwatts670_15-1615226064614.png


 


We now have an HTML table lets send an email with it attached. 


 


Click on “+ New Step” below this activity, search for “send an email (v2)”, and choose the Office 365 Outlook action named “Send an email (V2)”  


bwatts670_16-1615226064616.png


 


Rename the Action to “Email HTML Report” and fill out the following:  



  • Body: Whatever you wish for the Body of the email  

  • Subject: Whatever you wish for the Subject of the email  

  • To: Fill out the emails you wish to receive the report  



  • Click on “Add new parameter” and choose “Attachment” 

  • Attachment Content: from the “Dynamic content” choose “Output” under “Create HTML table”  

  • Attachment Name: Something like “VNets.html”  


bwatts670_17-1615226064642.png


 


 That’s it for the Logic App. You should now click on “Save” and once the Logic App is saved click on “Run”. Look for an email with the HTML attachment. 


bwatts670_18-1615226064619.png


Summary 


In this blob we showed how to use Logic App to query the Azure Rest API, take the output and make a HTML report, then email that report as an attachment. The focus was on calling the REST API and not the report (there is more formatting that I’d do with that report but didn’t want to focus on that). So hopefully you see from this example that you don’t need to be a programmer to call Azure REST APIs. Logic App can take care of the authentication and we can focus on just formatting the request correctly! As an Azure Administrator this can open the doors for a lot of automation that you always wanted but never thought was possible. 

Brought to you by Dr. Ware, Microsoft Office 365 Silver Partner, Charleston SC.