【问题标题】:List azure Workspaces By Resource Group in C# .NET在 C# .NET 中按资源组列出 azure 工作区
【发布时间】:2018-05-08 16:02:41
【问题描述】:

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

我发现了这个休息电话: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 调用,或者有更简单的方法来发出查询?

【问题讨论】:

    标签: c# rest azure


    【解决方案1】:

    正如您在帖子中提到的两个资源OperationalInsightsDatabricks,不清楚您要使用哪一个,所以我将它们都列出来。

    OperationalInsights可以下载Microsoft.Azure.Management.OperationalInsights使用SDK。

    对于Databricks,我在.NET SDK document 中也没有找到任何SDK。调用 REST API 是一种标准方式,AFAIK 似乎没有更简单的方法。

    使用 SDK 或 REST 都需要您通过注册 AD 应用并为应用分配角色来获取必要的信息(appId、secretKey、tenantId)。请关注此tutorial

    然后使用下面的代码sn-p。记得安装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);
         }
    }
    

    【讨论】:

    • 嗨,杰瑞,谢谢你的回答,你拯救了我的一天。我正在寻找您答案的第一部分,请问您有任何资源来熟悉 OperationalInsights 块包类型吗?
    • @ZackISSER 只得到了offical document,关于这个资源的描述似乎很少。
    • 嗨,杰瑞,是否可以使用 OperationalInsightsManagementClient 类编写跨工作区查询?我一天前发布了这个问题:stackoverflow.com/questions/50334286/…
    • @ZackISSER ,很抱歉我的回复延迟了很长时间,也感谢您邀请我提供建议,已经添加了我的解决方案。
    猜你喜欢
    • 2019-08-09
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多