I have the following simple piece of code for listing resources by tag from Azure, but I cannot get filter working. What am I doing wrong?
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Veeam.Core.Extensions;
string clientId = "...";
string tenantId = "...";
var clientSecret = "...";
string subscriptionId = "...";
// the code works with and operator but not works with or operator var filterTags = "tagName eq 'asdf' or 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);
The response from server is:
Status: 400 (Bad Request)
ErrorCode: InvalidFilterInQueryString
Content:
{"error":{"code":"InvalidFilterInQueryString","message":"Invalid $filter 'tagName eq 'asdf' and tagValue '1234'' specified in the query string."}}
IntelliSense documentation says:
//For example, to filter for a tag name and value, use $filter=tagName eq 'tag1' and tagValue eq 'Value1'. When you filter by a tag name and value, the tags for each resource are not returned in the results.
Is it really the or operator not supported?
Yes, only
andoperations are allowed in $filter tag to list the resources but notoroperation. Refer MSDOC.Try below operations to list the resources with
tagname/tagvalue:I have implemented the same with the below code:
Console Output: