Azure

How To Get Metadata Data Of An Azure Occasion Utilizing REST API

This text demonstrates how you can retrieve metadata of an occasion in Azure utilizing REST API Name in .NET.

Right here, we are going to retrieve Azure VM metadata particulars, and in addition, we are going to record the sources as a part of useful resource group utilizing REST API.

The belief right here is that we already created a few of the sources in useful resource group. In my case, Useful resource group incorporates,

  1. Azure VM
  2. App Service
  3. Storage Account

Earlier than making a REST API name from .NET code for an Azure occasion, we have to grant the required permissions by assigning the suitable position to the service principal on the applicable scope. The position that’s required to record the metadata of an Azure App Service is the “Reader” position. In any other case, authentication to the Azure Useful resource Supervisor (ARM) API will fail as a result of permissions concern to carry out the “record” motion on the “metadata” useful resource.

Factors to recollect, earlier than doing a REST API name,

  • Azure sources like Azure VM are already created.
  • Register an app with a reputation in Azure Energetic Listing utilizing “App Registrations”. As soon as that’s completed, create a consumer secret. That is required to entry the sources in Azure by way of API Name.
  • Grant position task to service precept created in Azure AD as a part of App Registration.

Step 1 – Register App in Azure Energetic Listing

Go to Azure Energetic Listing => App Registrations => New registration

Detailed steps of the app registration course of may be discovered right here

Step 2 – IAM position task to service principal

Listed here are the steps you need to use to assign the “Reader” position on the useful resource degree utilizing the Azure Portal:

  1. Choose the Azure useful resource for which you wish to grant permissions let’s say Azure VM.
  2. Click on on Entry management (IAM)
  3. Click on on +Add so as to add a brand new position task
  4. Within the Add position task pane, choose the “Reader” position from the Position drop-down record.
  5. Within the Assign entry to drop-down record, choose “Azure AD consumer, group, or service principal”.
  6. Within the Choose part, search and choose the consumer or service principal that you just wish to grant permissions.
  7. Click on on Save to create the position task.

How To Get Metadata Information Of An Azure Instance Using REST API

It could possibly take a couple of minutes for the adjustments to take impact. After that, the consumer or service principal ought to have the required permissions to carry out the “record” motion on the “metadata” useful resource.

Step 3 – REST API name from .NET to get Azure VM Occasion Metadata

NuGet packages to put in,

<PackageReference Embrace="Microsoft.Identification.Shopper" Model="4.49.1" />
<PackageReference Embrace="Newtonsoft.Json" Model="13.0.2" />

C# Code for producing token and doing REST API name,

string tenantId = "{azure-tenantId}";
string clientId = "{clientId}";
string clientSecret = "{consumer secret of registered app}";
string subscriptionId = "{azure-subscriptionId}";
string resourceGroupName = "{azure-resourceGroupName}";
string vmName = "{azure-vm-name}";
var app = ConfidentialClientApplicationBuilder.Create(clientId).WithClientSecret(clientSecret).WithTenantId(tenantId).Construct();
var authResult = await app.AcquireTokenForClient(new string[] {
    "https://administration.azure.com/.default"
}).ExecuteAsync();
// Use the entry token to authenticate the API name
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
// API Name to Get VM Particulars
// https://be taught.microsoft.com/en-us/relaxation/api/compute/virtual-machines/get?tabs=HTTP
var response = await httpClient.GetAsync($ "https://administration.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/suppliers/Microsoft.Compute/virtualMachines/{vmName}?api-version=2022-08-01");
if (response.IsSuccessStatusCode) {
    // Retrieve the response content material as a JSON string
    var jsonString = await response.Content material.ReadAsStringAsync();
    Console.WriteLine("-------------------Get VM Particulars--------------------------");
    Console.WriteLine(JObject.Parse(jsonString));
} else {
    throw new Exception($ "Did not retrieve VM metadata: {response.StatusCode} - {response.ReasonPhrase}");
}

Output JSON metadata of Azure VM occasion in console,

How To Get Metadata Information Of An Azure Instance Using REST API

REST API particulars of Get All Resouces in a Resouce Group,

// API name to Get Sources
var response = await httpClient.GetAsync($ "https://administration.azure.com/subscriptions/{subscriptionId}/sources?api-version=2021-04-01");
if (response.IsSuccessStatusCode) {
    // Retrieve the response content material as a JSON string
    var jsonString = await response.Content material.ReadAsStringAsync();
    Console.WriteLine("---------------------Get Sources---------------------------");
    Console.WriteLine(JObject.Parse(jsonString));
} else {
    throw new Exception($ "Did not retrieve Sources metadata: {response.StatusCode} - {response.ReasonPhrase}");
}

Output JSON metadata of Azure sources within the console,

How To Get Metadata Information Of An Azure Instance Using REST API

Comfortable Studying!

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button