azure.mgmt.costmanagement.models.QueryFilter with only 1 dimension yields "on a QueryFilter one and only one of and/or/not/dimension/tag can be set"
- Package Name: azure-mgmt-costmanagement
- Package Version: 0.2.0
- Operating System: Ubuntu 18.04.4 LTS
- Python Version: 3.6.9
Describe the bug
A query with a filter on a single ResourceID fails with:
Traceback (most recent call last):
File "
To Reproduce Steps to reproduce the behavior: 1.
pprint(InstanceQueryDefinition.as_dict()) {'dataset': {'aggregation': {'TotalCost': {'function': 'Sum', 'name': 'Cost'}}, 'filter': {'dimension': {'name': 'ResourceID', 'operator': 'In', 'values': ['/subscriptions/5d8bf858-df27-42fc-ac35-64c493d71179/resourcegroups/basf_rg_sandbox_107/providers/microsoft.compute/virtualmachines/vliaz']}}, 'granularity': 'Daily', 'grouping': [{'name': 'ResourceID', 'type': 'Dimension'}, {'name': 'ResourceGroup', 'type': 'Dimension'}]}, 'time_period': {'from_property': '2020-08-01T00:00:00.000Z', 'to': '2020-08-31T23:59:59.000Z'}, 'timeframe': 'Custom', 'type': 'ActualCost'}
costmanagement_client.query.usage(my_scope, InstanceQueryDefinition) Datetime with no tzinfo will be considered UTC. Datetime with no tzinfo will be considered UTC. Traceback (most recent call last): File "
", line 1, in File "/usr/local/lib/python3.6/dist-packages/azure/mgmt/costmanagement/operations/_query_operations.py", line 109, in usage raise models.ErrorResponseException(self._deserialize, response) azure.mgmt.costmanagement.models._models_py3.ErrorResponseException: (BadRequest) Invalid query definition: Invalid dataset filter; on a QueryFilter one and only one of and/or/not/dimension/tag can be set.
Expected behavior I only have 1 condition for the query filter. And I can create the filter in the following way. from azure.mgmt.costmanagement.models import QueryFilter, QueryComparisonExpression filter = QueryFilter(dimension= QueryComparisonExpression(name='ResourceID', values=list(myResourecIDs)))
I don't need AND, OR or NOT. Therefore I expect that the query also succeeds.
Screenshots If applicable, add screenshots to help explain your problem.
Additional context Add any other context about the problem here.
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @ms-premp, @ramaganesan-rg.
Hello, I have same issue but using REST API : [https://docs.microsoft.com/en-us/rest/api/cost-management/query/usage#querydatasetconfiguration]
POST https://management.azure.com/subscriptions/{subscription_id}/providers/Microsoft.CostManagement/query?api-version=2019-11-01
Authorization: Bearer ***********
Content-type: application/json
body:
{"timeframe":"Custom","type":"ActualCost","timePeriod":{"from":"2019-12-01T00:00:00+00:00","to":"2020-11-30T00:00:00+00:00"},"dataset":{"granularity":"Monthly","filter": {"dimension": {"name": "ResourceGroup","operator": "Contains","values": [ "datalakeop"] }},"aggregation":{"totalCost":{"name":"Cost","function":"Sum"}}}}
Result:
{
"error": {
"code": "BadRequest",
"message": "Invalid query definition: Invalid dataset filter; on a QueryFilter one and only one of and/or/not/dimension/tag can be set.\r\n\r\n (Request ID: 2eeaf348-b54e-430d-86cb-f15efea63729)"
}
}
Can you please try using dimensions instead of dimension as follows:
'filter': {'dimensions': {'name': 'ResourceID',
Thanks @jochav!
It works, but only "In" operator is admitted filtering by ResourceGroup:
"filter":{
"dimensions":{
"name":"ResourceGroup",
"operator":"In",
"values":[
"rg-one","rg-two"
]
}
},
I need to use contains operator, but I'll manage by now

@jochav
Unfortunately for me there is no difference.
I tried:
{'dataset': {'aggregation': {'totalCost': {'function': 'Sum', 'name': 'Cost'}},
'filter': {'dimensions': {'name': 'ResourceID',
'operator': 'Contains',
'values': ['/subscriptions/5d8bf858-df27-42fc-ac35-64c493d71179/resourcegroups/basf_rg_sandbox_107/providers/microsoft.compute/virtualmachines/vliaz']}},
'granularity': 'Daily',
'grouping': [{'name': 'ResourceID', 'type': 'Dimension'}]},
'timePeriod': {'from': '2020-08-01T00:00:00+00:00',
'to': '2020-08-31T23:59:59+00:00'},
'timeframe': 'Custom',
'type': 'ActualCost'}
but this still gives:
Traceback (most recent call last):
File "
@cesarnmo thanks for validating. @gzg200, as mentioned, it looks like there is a disconnect between our documentation and the API. The API is currently supporting only ["In"] as operator as opposed to ["In", "Contains"]. Will check and make sure API and docs are in sync.
Hi, I am trying to use the az costmanagement query in azure powershell with one filter condition "dimensions". But i am still getting this error . "BadRequestError: (BadRequest) Invalid query definition: Invalid dataset filter; on a QueryFilter one and only one of and/or/not/dimension/tag can be set."
The command that i am using is
az costmanagement query --type "ActualCost" --dataset-aggregation "{"totalCost":{"name":"PreTaxCost","function":"Sum"}}" --dataset-grouping name="ResourceGroupName" type="Dimension" name="ChargeType" type="Dimension" --dataset-filter "{"dimensions":{"name":"ResourceGroupName","operator":"In","values":["xx"]}}" --timeframe "MonthToDate" --scope "subscriptions/xxxxxxxxxxxxxxxxxx".
Can anybody help me here .
@jochav could you try to help malikbilal27 here?
@malikbilal27 , I believe you are using "dimensions" as the filter type. Try using "dimension" instead.
Hi , Thank you for your response i have tried to use both dimension and dimensions in the the filter but it did not work out