This is part 5 of the series “Create Azure Resource Manager Bot“.
Representational State Transfer (REST) APIs are service endpoints that support sets of HTTP operations (methods), which provide create, retrieve, update, or delete access to the service’s resources.
For more information. Refer Microsoft Documentation.
Pre-requisites
- Visual Studio
- Service Principal in Azure – To know how to create a service principal, go through my post on Creating Service Principal using PowerShell.
Video
Getting Access Token
Follow part 4 of the series How to get Azure Access Token using C#. This is in continuation of part 4. You can also get the source code of part 4 from the download page or GitHub. Clone the code and continue the demo.
Get All Resource Group Details using REST API
Import the following libraries.
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
Add the following method in Program.cs file to get all the resource groups from a subscription.
public static async Task getAllResourceGroupDetails()
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://management.azure.com/subscriptions/" + AzureDetails.SubscriptionID + "/resourcegroups?api-version=2019-10-01");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + AzureDetails.AccessToken);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, client.BaseAddress);
var response = await MakeRequestAsync(request, client);
AzureDetails.Response = response;
}
Add another method to make the HTTP request and get the response.
public static async Task<string> MakeRequestAsync(HttpRequestMessage getRequest, HttpClient client)
{
var response = await client.SendAsync(getRequest).ConfigureAwait(false);
var responseString = string.Empty;
try
{
response.EnsureSuccessStatusCode();
responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
catch (HttpRequestException)
{
}
return responseString;
}
Declare the following fields and property for Subscription ID and Response respectively in AzureDetails.cs along with clientID, tenantID, and clientSecret.
public static string SubscriptionID = "<Subscription ID>";
public static string Response { get; set; }
Remember, never keep your credentials or configuration details in code. You can use Azure vault or appsettings.json file. For demo purposes, I am hard-coding these values. Add your configuration detail in SubscriptionID.
Call the method getAllResourceGroupDetails() asynchronously in Main() and print the Response on console output.
static void Main(string[] args)
{
GetAuthorizationToken();
getAllResourceGroupDetails().GetAwaiter().GetResult();
Console.WriteLine(AzureDetails.Response);
}
Below is the console output with the Response.

Create Resource Group using REST API
Add the following method in Program.cs file to create a resource group.
public static async Task createResourceGroup()
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://management.azure.com/subscriptions/" + AzureDetails.SubscriptionID + "/resourcegroups/" + AzureDetails.ResourceGroupName + "?api-version=2019-10-01");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + AzureDetails.AccessToken);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, client.BaseAddress);
var body = $"{{\"location\": \"{AzureDetails.Location}\"}}";
var content = new StringContent(body, Encoding.UTF8, "application/json");
request.Content = content;
var response = await MakeRequestAsync(request, client);
AzureDetails.Response = response;
}
Declare the following field in AzureDetails.cs with the information of location and resource group name. The resource group will be created with the name and at the location you provided.
public static string Location = "<Location>";
public static string ResourceGroupName = "<Resource Group Name>";
For the list of Azure Locations, click here. Call the method createResourceGroup() asynchronously in Main() and print the Response on the console output.
GetAuthorizationToken();
createResourceGroup().GetAwaiter().GetResult();
Console.WriteLine(AzureDetails.Response);
Below is the console output with the response.

I created a resource group with the name jdBotsRG at location eastus. You can get the source code used in this demo from the Download page or GitHub.
You must be logged in to post a comment.