[英]List azure Workspaces By Resource Group in C# .NET
如何按资源组获取工作区列表?
我找到了这个Rest调用: 按资源组列出工作区
调用的资源是:
GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Databricks/workspaces?api-version=2018-04-01
在日志分析中可以找到api文档,但在Azure .NET SDK中似乎没有等效于此调用的方法。
我是否必须使用HttpClient之类的代码从C#代码进行Rest调用,或者有一种更简单的方式来发出查询?
正如您在帖子中提到了两个资源OperationalInsights
和Databricks
,您不清楚要使用哪个资源,因此我将它们都列出。
对于OperationalInsights
,可以下载Microsoft.Azure.Management.OperationalInsights以使用SDK。
对于Databricks
,我在.NET SDK文档中也找不到任何SDK。 调用REST API是一种标准方法,似乎没有更简单的方法AFAIK。
使用SDK或REST都要求您通过注册AD App并将角色分配给应用程序来获取必要的信息(appId,secretKey,tenantId)。 请遵循本教程 。
然后使用下面的代码段。 请记住安装Microsoft.IdentityModel.Clients.ActiveDirectory以生成凭据。
var appId = "ApplicationID";
var secretKey = "SecretKey";
var tenantId = "TenantID(aka DirectoryID)";
var subscriptionId = "SubscriptionId";
var resourceGroupName = "ResourceGroupName";
var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
ClientCredential clientCredential = new ClientCredential(appId, secretKey);
var tokenResponse = context.AcquireTokenAsync("https://management.azure.com/", clientCredential).Result;
var accessToken = tokenResponse.AccessToken;
//OperationalInsights
var opsClient = new OperationalInsightsManagementClient(new TokenCredentials(accessToken))
{
SubscriptionId = subscriptionId
};
var workspaces = opsClient.Workspaces.ListByResourceGroupAsync(resourceGroupName).Result;
// Databricks
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
client.BaseAddress = new Uri("https://management.azure.com/");
using (var response = await client.GetAsync(
$"subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Databricks/workspaces?api-version=2018-04-01"))
{
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
JObject json = JObject.Parse(content);
Console.WriteLine(json);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.