简体   繁体   中英

How to get Azure Resource Health from API using ResourcesManagementClient?

I'm specifically trying to fetch the information described here: https://docs.microsoft.com/en-us/rest/api/resourcehealth/availabilitystatuses/listbysubscriptionid

Is ResourcesManagementClient the way to go?

If so, how can I use it to fetch the availability statuses from the Microsoft.ResourceHealth provider?

Regarding the issue, please refer to the following steps

  1. Create a service principal and assign Azure RBAC role to the sp

  2. Install sdk

    <PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="3.13.0-preview" />
    <PackageReference Include="Microsoft.Identity.Client" Version="4.30.1" />
  1. Code
            var app = ConfidentialClientApplicationBuilder.Create(clientId)
                .WithClientSecret(clientSecret)
                .WithAuthority(String.Format(CultureInfo.InvariantCulture, "https://login.microsoftonline.com/{0}", tenantDomain))
                .Build();
            string[] scopes = new string[] { "https://management.azure.com/.default" };
            var result = await app.AcquireTokenForClient(scopes)
                     .ExecuteAsync();

            var cred = new TokenCredentials(result.AccessToken);

            ResourceManagementClient resourceManagementClient = new ResourceManagementClient(cred);
            resourceManagementClient.SubscriptionId = subscription;
            ///For example
            ///var resource = await resourceManagementClient.Resources.GetAsync(
            ///           "testdata",
            ///           "Microsoft.Compute",
            ///           "virtualMachines/testdocker" + "/providers/Microsoft.ResourceHealth",
            ///           "availabilityStatuses",
            ///           "current",
            ///           "2020-05-01"
            ///    );
            ///
            ///
            ///
            var resource = await resourceManagementClient.Resources.GetAsync(
                   "<the resource group of the resource your want to>",
                   "<the namesapce of the resource your want to>",
                   "<the resource path of the resource your want to>" + "/providers/Microsoft.ResourceHealth",
                   "availabilityStatuses",
                   "current",
                   "2020-05-01"

                );

            Console.WriteLine(resource.Properties);

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM