如何按资源组获取工作区列表?
我发现了这个休息电话:List workspace by ressource group
要调用的资源是:
GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Databricks/workspaces?api-version=2018-04-01
在日志分析rest api文档下,但似乎在azure .NET SDK中没有与此调用等效的内容。
我是否必须使用 HttpClient 之类的 C# 代码进行 Rest 调用,还是有更简单的方法来发出查询?
最佳答案
正如您在帖子中提到的两个资源 OperationalInsights
和 Databricks
,不清楚您要使用哪一个,所以我列出了它们。
对于 OperationalInsights
,您可以下载 Microsoft.Azure.Management.OperationalInsights使用SDK。
对于 Databricks
,我在 .NET SDK document 中也没有找到任何 SDK 。调用 REST API 是一种标准方法,据我所知似乎没有更简单的方法。
使用 SDK 或 REST 都需要您通过注册 AD 应用程序并向应用程序分配角色来获取必要的信息(appId、secretKey、tenantId)。请关注此tutorial .
然后使用下面的代码片段。记得安装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);
}
}
关于c# - 在 C# .NET 中按资源组列出 azure 工作区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50237886/