Leo Lazarus
Leo Lazarus

Reputation: 107

What is the C# equivalent of Get-AzureRmResource in .NET SDK for Azure?

I would like to know if there are any .NET method to get all resources under a subscription. This is available in PowerShell in the cmdlet: Get-AzureRmResource.

Thanks

Upvotes: 1

Views: 2138

Answers (2)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131403

As the British would say, Good Grief!

TL;DR

Try :

using Microsoft.Azure.Management.ResourceManager.Fluent;
...

var rm = ResourceManager.                    
                .Configure()
                .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                .Authenticate(credentials)
                .WithDefaultSubscription();

var resources= rm.GenericResources.List();

foreach(IGenericResource res in resources)
{
    ....
}

TL;DR 2

Some comments point to the ResourceOperationsExtensions.List extension method. That requires an IResourceOpearations instance though. This is available through the IResourceManagementClient.Resources. This is accessible through the ResourceManager.Inner property:

var rmOps=rm.Inner.Resources;
var resources = rmOps.List(someODataQuery);

Long version

Azure's management works by exposing REST interfaces. In the end, both the Azure SDK and the Powershell commandlets will call that REST interface. You could use Fiddler or another debugging proxy to capture and reuse the

The Powershell commandlets are implemented on top of the SDK which means you can look at the source code itself.

The commandlet uses the ResourceManagerSdkClient class's ListResources method with an OData query generated from its parameters:

result = this.ResourceManagerSdkClient.ListResources(odataQuery);

Unfortunately, that doesn't help since the commandlets use their own low-level abstrations, while the Azure RM libraries use different abstractions and interfaces. ListResources tells us that we need to query a GenericResource though.

The Azure Resource Manager libraries repo is hosted on Github. The documentation doesn't show how to get to ResourceManager though. Searching in the repo itself, shows how to ResourceManager is used in the tests.

ResourceManager.GenericResources provides access to the same generic resources that the Powershell command uses. That property's type is IGenericResources which in turn implements ISupportsListing which gives us access to List

The docs don't make it clear whether those interfaces are explicitly implemented. The code may need explicit casting.

IGenericResources implements ISupportsListingByResourceGroup and ISupportsListingInResourceGroupByTag too, which can be used to search by resource groups and tags.

Googling for ResourceManager.GenericResources or GenericResources.List() etc doesn't produce relevant results. The terms are too ... generic.

I think I'll take a break now.

Upvotes: 5

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29985

I think you're looking for Azure Resource Manager libraries for .NET, details in this doc.

And in the GitHub, there is a sample project your can download and use it for testing. In this sample project, you can create/update/list resources(which is equivalent to the get-AzureRmResource)

Upvotes: 0

Related Questions