Azure资源管理 - OperationalInsights / workspaces - 按标记过滤?

时间:2015-12-02 20:08:22

标签: c# rest azure odata azure-resource-manager

在查询ARM API for Workspaces(使用.NET SDK)时,我无法提供TagName或TagValue过滤器。

我将使用v3.1 + ARM SDK的最新预览版本为初始查询提供一个镜头。我还无法将其余的代码库更新到该软件包,因为它充满了重大变化。

更新尽管新的3.1+ ResourceManagementClient更改了SDK的大部分内容,但它仍然使用相同的API版本使用相同的请求发送查询。 (2014年4月1日 - 预览)

更新2:我预料到了这一点,但此问题不仅限于工作区,还包括任何Provider / ResourceType组合。

问题:

http://management.azure.com/subscriptions/{subscriptionId}/resources?$filter=resourceType%20eq%20'Microsoft.OperationalInsights%2Fworkspaces'%20and%20tagvalue%20eq%20'{tagValue}'&api-version=2014-04-01-preview
  

错误:InvalidFilterInQueryString

     

消息:查询字符串中指定的$ filter'resourceType eq'Microsoft.OperationalInsights / workspaces'和tagvalue eq'{tagValue}'无效。

解决方法: 查询所有工作区并过滤客户端

https://management.azure.com/subscriptions/{subscriptionId}/resources?$filter=resourceType%20eq%20'Microsoft.OperationalInsights%2Fworkspaces'&api-version=2014-04-01-preview
{
  "value": [
    {
      "id": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}",
      "name": "{workspaceName}",
      "type": "Microsoft.OperationalInsights/workspaces",
      "location": "{location}",
      "tags": { "Tag1": "TagValue1" }
    }
  ]
}

SDK:

  • Microsoft.Azure.Management.Resources v2.18.11-preview
  • Microsoft.Azure.Management.OperationalInsights v0.10.0-preview
  • Microsoft.Azure.Management.Resources v3.1.1-preview
  • Microsoft.IdentityModel.Clients.ActiveDirectory v3.6.212012344-alpha
  • Microsoft.Rest.ClientRuntime v1.4.1
  • Microsoft.Rest.ClientRuntime.Azure v2.1.0

v2.18.11预览:

TokenCloudCredentials creds = await GetOldTokenCredentials(...);    
using (var armClient = new ResourceManagementClient(creds)
using (var opsClient = new OperationalInsightsManagementClient(creds))
{
    var workspaceQuery = new ResourceListParameters { ResourceType = "Microsoft.OperationalInsights/workspaces" };
    // invalid filter error
    // var workspaceQuery = new ResourceListParameters { ResourceType = "Microsoft.OperationalInsights/workspaces" , TagValue = "TagValue1" }; 
    var workspaces = await armClient.ListRecursiveAsync(workspaceQuery, null);

    if (workspaces.Any())
    {
        var resource = workspaces.FirstOrDefault(x => x.Tags.ContainsKey("TagName1") || x.Tags.Values.Contains("TagValue1"));
        var resourceGroup = MicrosoftResources.GetResourceGroupNameFromId(resource.Id);
        var workspaceResult = await opsClient.Workspaces.GetAsync(resourceGroup, resource.Name);
    }
}

V3.1.1预览:

ServiceClientCredentials newCreds = await GetNewTokenCredentials();
armClient.SubscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
var workspaces = armClient.Resources.List(x => x.ResourceType == "Microsoft.OperationalInsights/workspaces");
// same as v2.18.11

Auth + misc

public class MicrosoftResources
{        
    public static string GetResourceId(string subscriptionId, string resourceGroup, string provider, string resourceName)
    {
        return $"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/{provider}/{resourceName}";
    }

    private readonly static string _rgStr = "resourceGroups/";
    private readonly static string _prStr = "/providers";
    public static string GetResourceGroupNameFromId(string id)
    {
        var rgStrIdx = (id.IndexOf(_rgStr) + _rgStr.Length);
        return id.Substring(rgStrIdx, id.IndexOf(_prStr) - rgStrIdx);
    }
}

async static Task<string> GetAuthToken(string tenantId, Guid clientId, string clientSecret)
{
    var authenticationContext = new AuthenticationContext("https://login.windows.net/"+ tenantId);
    var clientCredential = new ClientCredential(clientId.ToString(), clientSecret);
    var authenticationResult = await authenticationContext.AcquireTokenAsync("https://management.core.windows.net/", clientCredential);

    if (authenticationResult == null)
    {
        throw new InvalidOperationException("Failed to obtain the JWT token");
    }

    return authenticationResult.AccessToken;
}

// v2.18.11
async Task<TokenCloudCredentials> GetOldTokenCredentials(string tenantId, Guid clientId, string clientSecret, string subscriptionId)
{
    var token = await GetAuthToken(tenantId, clientId, clientSecret);
    return new TokenCloudCredentials(subscriptionId, token);
}

// v3.1.1-preview
public static async Task<ServiceClientCredentials> GetNewTokenCredentials(string tenantId, Guid clientId, string clientSecret)
{
    var token = await GetAuthToken(tenantId, clientId, clientSecret);
    return new TokenCredentials(token);
}

0 个答案:

没有答案
相关问题