簡體   English   中英

如何使用 Azure Mgmt SDK fluent 獲取空資源組列表

[英]How to get list of Empty Resource Groups by using of Azure Mgmt SDK fluent

我正在使用https://www.nuget.org/packages/Microsoft.Azure.Management.Fluent 以編程方式獲取 Azure中的資源(C# .NET-Core .NET-Core Web 應用程序)並嘗試通過提供以下主要服務信息獲取主要資源...

 string subscriptionId="XXX"; 
       AzureCredentials cred = new 
                 AzureCredentialsFactory().FromServicePrincipal(UIConstants.ClientID, 
                 UIConstants.Secret, UIConstants.Tenant,AzureEnvironment.AzureGlobalCloud);                      
                
        var azure = Azure.Configure()
                         .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) 
                         .Authenticate(cred) 
                         .WithSubscription(subscriptionId);

如何使用 Azure Mgmt SDK 流利使用 do.netcore,C#.net 獲取空資源組列表 沒有任何 azure 資源的資源組

請就以上建議。

謝謝,

沒有內置方法,需要自己寫代碼檢查某個資源組中是否有資源。

這是示例代碼,它可以列出我這邊所有的空資源組:

using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using System;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            string subscriptionId = "xxx";
            string clientId = "xxx";
            string tenantId = "xxx";
            string clientSecret = "xxx";

            AzureCredentials cred = new AzureCredentialsFactory()
                .FromServicePrincipal(
                clientId,
                clientSecret,
                tenantId,
                AzureEnvironment.AzureGlobalCloud
                );

            var azure = Azure.Configure()
                             .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                             .Authenticate(cred)
                             .WithSubscription(subscriptionId);


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

            ResourceManagementClient client = new ResourceManagementClient(restClient);
            client.SubscriptionId = subscriptionId;          

            //list all resource groups
            var rgs = azure.ResourceGroups.List();

            foreach (var r in rgs)
            {              
                
                var resources = client.Resources.ListByResourceGroupAsync(r.Name).Result;

                //initiate a resource number as 0
                int number_of_resources = 0;

                //check if there are any resources in the resource group
                foreach (var resource in resources)
                {
                    number_of_resources++;
                    break;                   
                }

                //if the resources number is 0 in the resource group, then print out the empty resource group name
                if (number_of_resources == 0)
                {
                    Console.WriteLine($"the resource group: {r.Name} is empty");
                }
            }

            Console.WriteLine("**completed**");
            Console.ReadLine();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM