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

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

Update: 尽管新的3.1 ResourceManagementClient改变了大部分SDK,它仍然使用相同的API版本使用相同的请求发出查询 . (2014年4月1日,预览)

Update 2: 我期待这个,但是这个问题不仅限于工作区,而是任何Provider / ResourceType组合 .


Problem:

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''无效 .


Workaround: 查询所有工作区并过滤客户端

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-preview:

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-preview:

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);
}