簡體   English   中英

在C#.NET中按資源組列出Azure工作區

[英]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調用,或者有一種更簡單的方式來發出查詢?

正如您在帖子中提到了兩個資源OperationalInsightsDatabricks ,您不清楚要使用哪個資源,因此我將它們都列出。

對於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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM