简体   繁体   中英

How to get List of resources by resource type and resource group by using Azure Resource management apis

How to get list of resource for a Resource Group using Azure Resource Management API

I have install Microsoft.Azure.Management.ResourceManager.Fluent Nuget package The below script only give me only list of resource groups but not list of resources per resource group.

        var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureGlobalCloud);      
        var azure = Azure.Configure().Authenticate(credentials).WithSubscription(subscriptionID);
        var resourecelist = azure.ResourceGroups.List().ToList();

I am looking for something similar to which is available in powershell

Get-AzureRmResource -ResourceGroupName $batchResourceGroup -ResourceType 'Microsoft.Batch/batchAccounts'

Please have a try to following code to get list of resources. I test it on my side, it works correctly. We also could use the Resources - List By Resource Group Rest API to do that.

  var resouceManagementClient = new ResourceManagementClient(credentials) {SubscriptionId = subscriptionId};
  var resource = resouceManagementClient.ResourceGroups.ListResourcesAsync(resourceGroup,new ODataQuery<GenericResourceFilterInner>(x=>x.ResourceType == "Microsoft.Batch/batchAccounts")).Result;

在此处输入图片说明

The above answer is out-of-date, so here's my code snippet that works in Dec 2020.

Azure.IAuthenticated _azure;
string _subscriptionId;
RestClient _restClient;

async Task Main()
{
   Connect();

   // Get resource groups
   var resourceManagementClient = new ResourceManagementClient(_restClient)
   {
      SubscriptionId = _subscriptionId
   };

   var resourceList = (await resourceManagementClient.ResourceGroups.ListAsync()).ToList().OrderBy(r => r.Name);
   // ...
}

void Connect()
{
   _subscriptionId = "XXX";
   var tenantId = "YYY";
   var clientId = "ZZZ";
   var secret = "QQQ";

   var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
      clientId, secret, tenantId,
      AzureEnvironment.AzureGlobalCloud)
      .WithDefaultSubscription(_subscriptionId);

   _restClient = RestClient
      .Configure()
      .WithEnvironment(AzureEnvironment.AzureGlobalCloud)
      .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
      .WithCredentials(credentials)
      .Build();

   var creds = new AzureCredentialsFactory().FromServicePrincipal(
      clientId, secret, tenantId,
      AzureEnvironment.AzureGlobalCloud
      );
   _azure = Azure.Authenticate(creds);
}

The usings/imports/NuGet. (you do not need all of these...):

Microsoft.Azure.Management.AppService.Fluent
Microsoft.Azure.Management.AppService.Fluent.Models
Microsoft.Azure.Management.Fluent
Microsoft.Azure.Management.ResourceManager.Fluent
Microsoft.Azure.Management.ResourceManager.Fluent.Authentication
Microsoft.Azure.Management.ResourceManager.Fluent.Core
Microsoft.IdentityModel.Clients.ActiveDirectory
Microsoft.Rest
Microsoft.ServiceBus.Messaging
System.Threading.Tasks
Microsoft.Rest.Azure

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