按标签列出来自 Azure 的资源

问题描述 投票:0回答:1

我有以下简单的代码片段,用于按 Azure 中的标签列出资源,但我无法使过滤器正常工作。我做错了什么?

using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Veeam.Core.Extensions;

string clientId = "...";
string tenantId = "...";
var clientSecret = "...";
string subscriptionId = "...";

// 代码适用于 and 运算符,但不适用于 or 运算符 var filterTags = "tagName eq 'asdf' 或 tagValue eq '1234'";

var e = ArmEnvironment.AzurePublicCloud;
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var armClient = new ArmClient(credential);
var subscription = armClient.GetSubscriptionResource(new ResourceIdentifier("/subscriptions/" + subscriptionId));
var resourcesPageable= subscription.GetGenericResourcesAsync(filter: filterTags);
var tempResources = await resourcesPageable.ToListAsync();
Console.WriteLine(tempResources.Count);

服务器的响应是:

状态:400(错误请求)
错误代码:InvalidFilterInQueryString

Content:                                                                                                                                          
{"error":{"code":"InvalidFilterInQueryString","message":"Invalid $filter 'tagName eq 'asdf' and tagValue '1234'' specified in the query string."}}

IntelliSense 文档说:

//例如,要过滤标签名称和值,请使用 $filter=tagName eq 'tag1' 和 tagValue eq 'Value1'。当您按标签名称和值进行过滤时,每个资源的标签不会在结果中返回。

真的不支持or运算符吗?

c azure azure-sdk
1个回答
0
投票

真的不支持or运算符吗?

是的,$filter标签中只允许进行

and
操作来列出资源,但不允许进行
or
操作。请参阅MSDOC

尝试以下操作来列出带有

tagname
/
tagvalue
的资源:

$filter=tagname eq '{tagname}'
$filter=tagname eq '{tagname}' and tagvalue eq '{tagvalue}'
$filter=startswith(tagname, '{tagname prefix}')

我用下面的代码实现了相同的功能:

using System;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Resources.Models;
using System.Data.Entity.Repository;

class Program
{
    static async Task Main(string[] args)
    {
        string clientId = "<Client_ID>";
        string tenantId = "<Tenant_ID>";
        var clientSecret = "<Client_secret>";
        string subscriptionId = "<Subscription_ID>";
        string filterTags = "tagname eq 'abc' and tagvalue eq '123'"; 

        var e = ArmEnvironment.AzurePublicCloud;
        var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
        var armClient = new ArmClient(credential);
        var subscription = armClient.GetSubscriptionResource(new ResourceIdentifier("/subscriptions/" + subscriptionId));
        var resourcesPageable = subscription.GetGenericResourcesAsync(filter: filterTags);
        var tempResources = new List<GenericResource>();

        await foreach (var resource in resourcesPageable)
        {
            tempResources.Add(resource);
        }

        Console.WriteLine($"Number of resources: {tempResources.Count}");

        foreach (var resource in tempResources)
        {
            Console.WriteLine($"Resource ID: {resource.Id}, Resource Name: {resource.Data.Name}, Resource Type: {resource.Data.ResourceType}");
        }

    }
}

控制台输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.