Search code examples
c#asp.netazureazure-sdk-.netazure-container-registry

Get all container registries under a subscription - .NET Azure sdk


Looking out for .NET SDK references to get a specified container registry or all the container registries in a resource group or the subscription.

Need references apart from the REST API available: https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.ContainerRegistry/registries?api-version=2019-05-01

I have seen that the operation is available through Powershell and CLI cmdlet; Get-AzContainerRegistry and az acr list, but looking out for .NET SDK based library references (Azure.ResourceManager preferred) for the same.

I have gone through https://github.com/Azure/azure-sdk-for-net, but there's no extension related to get a specific or list the container registries under a subscription.


Solution

  • Nuget packages:

    // Change these as needed
    ArmClient armClient = new ArmClient(new DefaultAzureCredential());
    Subscription subscription = await armClient.GetDefaultSubscriptionAsync();
    
    // You can also get the resource group first if you know it already
    // subscription.GetResourceGroups().Get("myresourcegroup");
    var registyResources = subscription.GetGenericResourcesAsync("resourceType eq 'Microsoft.ContainerRegistry/registries'").AsPages();
    
    await foreach (var resource in registyResources)
    {
        var id = resource.Values.First().Id;
        Console.WriteLine($"Id: {id}");
        Console.WriteLine($"Resource Group: {id.ResourceGroupName}");
        Console.WriteLine($"Container registry name: {id.Name}");
    }
    
    

    You can also explore the returned resource if you need any other information. You can then continue with the specialized SDK for the specific Container Registry.