Azure.ResourceManager.Resources.SubscriptionResources.GetResourceGroups() 返回为空。

huangapple go评论61阅读模式
英文:

Azure.ResourceManager.Resources.SubscriptionResources.GetResourceGroups() returning empty

问题

以下是翻译好的代码部分:

public static async Task<string> GetAdminKeysAsync(IAzureCredentialFactory credentialFactory, string subscriptionId, string serviceName, bool useAzureResourceManagerForAdminKeys)
{
    if (!useAzureResourceManagerForAdminKeys)
    {
        var accessToken = credentialFactory.AcquireToken(Common.SystemConstants.Security.AzureManagementEndpoint);
        SearchManagementClient searchManagementClient = new SearchManagementClient(new TokenCredentials(accessToken))
        {
            SubscriptionId = subscriptionId
        };
        // Service found (see below code GetResourceGroupNameFromServiceName)
        string serviceResourceGroupName = GetResourceGroupNameFromServiceName(searchManagementClient, serviceName);
        var adminKeys = searchManagementClient.AdminKeys.Get(serviceResourceGroupName, serviceName);
        return adminKeys.PrimaryKey;
    }
    else
    {
        var tokenCredential = credentialFactory.AcquireTokenCredential(Common.SystemConstants.Security.AzureManagementEndpoint);
        var armClient = new ArmClient(tokenCredential, subscriptionId);
        var subscription = await armClient.GetDefaultSubscriptionAsync();
        // ResourceGroup not found
        var resourceGroups = subscription.GetResourceGroups();
        foreach (var resourceGroup in resourceGroups)
        {
            var searchServices = resourceGroup.GetSearchServices();
            foreach (var searchService in searchServices)
            {
                if (searchService.Data.Name.Equals(serviceName))
                {
                    var adminKeys = await searchService.GetAdminKeyAsync();
                    return adminKeys.Value.PrimaryKey;
                }
            }
        }
        throw new InvalidOperationException($"Service name {serviceName} can't be found from subscription {subscriptionId}.");
    }
}

public static string GetResourceGroupNameFromServiceName(SearchManagementClient searchManagementClient, string serviceName)
{
    // Service found
    IPage<SearchService> searchCollection = searchManagementClient.Services.ListBySubscription();
    SearchService searchService = searchCollection.SingleOrDefault(azs => azs.Name.Equals(serviceName));
    string searchId = searchService.Id;
    Match matches = ResourceGroupIdPattern.Match(searchId);
    if (matches.Groups.Count < 2)
    {
        throw new ArgumentException($"Could not find resource group name in {searchId}.");
    }
    return matches.Groups[1].Value;
}

请注意,代码中的注释部分并未翻译。

英文:

For the code below, it is getting the search service if using Microsoft.Azure.Management.Search. It is not getting the resource group if using Azure.ResourceManager.Resources.

public static async Task&lt;string&gt; GetAdminKeysAsync(IAzureCredentialFactory credentialFactory, string subscriptionId, string serviceName, bool useAzureResourceManagerForAdminKeys)
    {
        if (!useAzureResourceManagerForAdminKeys)
        {
            var accessToken = credentialFactory.AcquireToken(Common.SystemConstants.Security.AzureManagementEndpoint);
            SearchManagementClient searchManagementClient = new SearchManagementClient(new TokenCredentials(accessToken))
            {
                SubscriptionId = subscriptionId
            };
			// Service found (see below code GetResourceGroupNameFromServiceName)
            string serviceResourceGroupName = GetResourceGroupNameFromServiceName(searchManagementClient, serviceName);
            var adminKeys = searchManagementClient.AdminKeys.Get(serviceResourceGroupName, serviceName);
            return adminKeys.PrimaryKey;
        }
        else
        {
            var tokenCredential = credentialFactory.AcquireTokenCredential(Common.SystemConstants.Security.AzureManagementEndpoint);
            var armClient = new ArmClient(tokenCredential, subscriptionId);
            var subscription = await armClient.GetDefaultSubscriptionAsync();
			// ResourceGroup not found
            var resourceGroups = subscription.GetResourceGroups();
            foreach (var resourceGroup in resourceGroups)
            {
                var searchServices = resourceGroup.GetSearchServices();
                foreach (var searchService in searchServices)
                {
                    if (searchService.Data.Name.Equals(serviceName))
                    {
                        var adminKeys = await searchService.GetAdminKeyAsync();
                        return adminKeys.Value.PrimaryKey;
                    }
                }
            }
            throw new InvalidOperationException($&quot;Service name {serviceName} can&#39;t be found from subscription {subscriptionId}.&quot;);
        }
    }
	
	public static string GetResourceGroupNameFromServiceName(SearchManagementClient searchManagementClient, string serviceName)
    {
		// Service found
        IPage&lt;SearchService&gt; searchCollection = searchManagementClient.Services.ListBySubscription();
        SearchService searchService = searchCollection.SingleOrDefault(azs =&gt; azs.Name.Equals(serviceName));
        string searchId = searchService.Id;
        Match matches = ResourceGroupIdPattern.Match(searchId);
        if (matches.Groups.Count &lt; 2)
        {
            throw new ArgumentException($&quot;Could not find resource group name in {searchId}.&quot;);
        }
        return matches.Groups[1].Value;
    }

答案1

得分: 1

请尝试将以下代码行更改为:

await foreach (var resourceGroup in resourceGroups.GetAllAsync())
英文:

Please try by changing the following line of code:

foreach (var resourceGroup in resourceGroups)

to

await foreach (var resourceGroup in resourceGroups.GetAllAsync())

huangapple
  • 本文由 发表于 2023年6月19日 09:01:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定