英文:
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<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;
}
答案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())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论