azure-sdk-for-python icon indicating copy to clipboard operation
azure-sdk-for-python copied to clipboard

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"

Open gzg200 opened this issue 6 years ago • 10 comments

  • 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 "", 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.

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.

gzg200 avatar Oct 30 '20 18:10 gzg200

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @ms-premp, @ramaganesan-rg.

ghost avatar Oct 30 '20 23:10 ghost

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

cesarnmo avatar Nov 30 '20 16:11 cesarnmo

Can you please try using dimensions instead of dimension as follows:

'filter': {'dimensions': {'name': 'ResourceID',

jochav avatar Dec 04 '20 20:12 jochav

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 image

cesarnmo avatar Dec 09 '20 08:12 cesarnmo

@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 "", 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.

gzg200 avatar Dec 09 '20 11:12 gzg200

@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.

jochav avatar Dec 10 '20 01:12 jochav

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 .

malikbilal27 avatar Feb 08 '21 10:02 malikbilal27

@jochav could you try to help malikbilal27 here?

lilyjma avatar Feb 10 '21 23:02 lilyjma

@malikbilal27 , I believe you are using "dimensions" as the filter type. Try using "dimension" instead.

mrlepat avatar Feb 10 '21 23:02 mrlepat

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

malikbilal27 avatar Feb 11 '21 05:02 malikbilal27