c# - 如何使用 Microsoft.Azure.Management.Fluent 列出资源组中的资源?
问题描述
我目前正在尝试使用 Microsoft.Azure.Management.Fluent 列出资源组中的所有资源,但我无法弄清楚。我做到了这一点:
var azure Microsoft.Azure.Management.Fluent.Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(mycredentials)
.WithDefaultSubscription();
var resourceGroup = azure.ResourceGroups.GetByName("MyResourceGroup");
但是现在我被困住了,因为我似乎只能从资源组(ID、名称等)中获取基本数据。但是,如果我想要组中所有资源的名称/资源类型?
我发现这个扩展方法似乎可以做我想做的事情:
但我不知道从哪里获得 IResourceGroupsOperations 对象。
有些人似乎也在谈论 a ResourceManagementClient
,但它只是RestClient
在它的构造函数中使用 a ,所以感觉它应该是一种更简单的方法。
解决方案
根据我的测试,我们可以ResourceManagementClient
在 SDK中使用Microsoft.Azure.Management.ResourceManager.Fluent
来列出一个资源组中的所有资源。详细步骤如下
az login
az ad sp create-for-rbac --name <ServicePrincipalName>
az role assignment create --assignee <ServicePrincipalName> --role Contributor
- 代码
var tenantId = "<your tenant id>";
var clientId = "<your sp app id> ";
var clientSecret = "<your sp passowrd>";
var subscriptionId = "<your subscription id>";
AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
clientId,
clientSecret,
tenantId,
AzureEnvironment.AzureGlobalCloud);
RestClient restClient = RestClient.Configure()
.WithEnvironment(AzureEnvironment.AzureGlobalCloud)
.WithCredentials(credentials)
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Build();
ResourceManagementClient client = new ResourceManagementClient(restClient);
client.SubscriptionId = subscriptionId;
foreach (var resource in await client.Resources.ListByResourceGroupAsync("<your resource group name>")) {
Console.WriteLine("Name:"+ resource.Name );
}
推荐阅读
- mongodb - 如何在mongodb中将字符串转换为日期?
- purescript - purescript 中的模式匹配
- python - 如何根据游戏重播视频检测 FPS 游戏中的相机移动?
- c++ - 为什么第一行会出现除零以外的其他值?
- nuxt.js - 使用 Koa 作为 Nuxt 服务器中间件
- node.js - 无法连接到 MongoDb (MongoNetworkError: connect ETIMEDOUT)
- vb.net - 禁用的标签不会变灰
- python - Python - Outlook 仅在特定范围内搜索电子邮件
- javascript - TypeError : ...map 不是函数 - react.js
- r - 从行到前的最后两个非 NA 观测值