在C#.NET中按资源组列出Azure工作区 [英] List azure Workspaces By Resource Group in C# .NET

查看:67
本文介绍了在C#.NET中按资源组列出Azure工作区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何按资源组获取工作区列表?

How do I get the list of workspaces by ressource group ?

我找到了这个Rest调用:按资源组列出工作区

I found this Rest call : List workspace by ressource group

要调用的资源是:

GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Databricks/workspaces?api-version=2018-04-01

在日志分析其余api文档中,但似乎在Azure .NET SDK中没有与此调用等效的内容.

under log analytics rest api documentation, but it seems like there is no equivalent to this call in azure .NET SDK .

我是否必须使用HttpClient之类的代码从C#代码进行Rest调用,还是有一种更简单的方式来发出查询?

Do I have to make the Rest call from my C# code using something like HttpClient or there is a more simple way to issue the query ?

推荐答案

正如您在帖子中提到的两个资源 OperationalInsights Databricks 一样,不清楚要选择哪个资源使用,所以我将它们都列出.

As you mentioned two resource OperationalInsights and Databricks in your post, not clear which one you want to use, so I list both of them.

对于 OperationalInsights ,您可以下载

For OperationalInsights, you can download Microsoft.Azure.Management.OperationalInsights to use the SDK.

虽然用于 Databricks ,但在

While for Databricks, I don't find any SDK either in .NET SDK document. Calling REST API is a standard way, and there seems no easier method AFAIK.

使用SDK或REST都要求您通过注册AD App并将角色分配给应用程序来获取必要的信息(appId,secretKey,tenantId).请按照此教程.

Using SDK or REST both require you to get necessary info(appId, secretKey, tenantId) by registering AD App and assigning role to application. Please follow this tutorial.

然后使用下面的代码段.请记住安装 Microsoft.IdentityModel.Clients.ActiveDirectory 以生成凭据

And then use code snippet below. Remeber to install Microsoft.IdentityModel.Clients.ActiveDirectory to generate credential.

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#.NET中按资源组列出Azure工作区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆