2
votes

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'
2
after installaion nuget also i'm unable to refer Microsoft.Azure.Management.ResourceManager.Fluent why? unable to use class ResourceManagementClient what to do?Neo

2 Answers

3
votes

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;

enter image description here

1
votes

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