Skip to content

[Consumption] az consumption usage list: Enhanced handling of usageStart and usageEnd when missing#31128

Merged
kairu-ms merged 6 commits intoAzure:devfrom
haule21:fix/consumption-usage-start-none-handling
Apr 18, 2025
Merged

[Consumption] az consumption usage list: Enhanced handling of usageStart and usageEnd when missing#31128
kairu-ms merged 6 commits intoAzure:devfrom
haule21:fix/consumption-usage-start-none-handling

Conversation

@haule21
Copy link
Contributor

@haule21 haule21 commented Mar 25, 2025

Handle None UsageStart in consumption usage list and exclude from results

Related command
az consumption usage list: Enhanced handling of usageStart and usageEnd in custom.py:

  • Updated _output function from result = list([transform_usage_output(item) for item in result]) to result = list(filter(None, [transform_usage_output(item) for item in result])) to exclude None values returned by transform_usage_output.
  • Modified transform_usage_output function to:
    • Use result.get('usageStart', None) and result.get('usageEnd', None) for safe attribute access.
    • Add conditional check if usageStart and usageEnd to return None if either is missing, ensuring invalid entries are filtered out.
    • Format valid usageStart and usageEnd to ISO 8601 strings (e.g., "2025-03-01T00:00:00Z") or set to 'None' if parsing fails.
    • Update other fields (usageQuantity, billableQuantity, pretaxCost) to use get() with default 'None' for robustness.

Description
This PR updates the az consumption usage list command to handle cases where the usage_start property in the response is missing or None. Previously, this could lead to potential errors (e.g., TypeError) when processing usage details. Now, usage entries with a None usage_start are filtered out from the results, ensuring a more robust and error-free experience.

  • What is changed: Modified list_usage in custom.py to filter out usage entries where usage_start is None.
  • Why: To prevent errors and ensure only valid usage data is returned.
  • Effect: Users will no longer see usage entries with missing usage_start in the output, improving reliability.

Testing Guide

  1. Run the command with specific start and end dates:
    [Command]
    az consumption usage list --end-date 2025-03-24 --start-date 2025-03-01 --output json
    • Verify that all returned items have a non-null usageStart field.
    • Ensure that entries with missing usageStart or usageEnd are excluded from the results.

History Notes


This checklist is used to make sure that common guidelines for a pull request are followed.

  • [✔] The PR title and description follow the guidelines in Submitting Pull Requests.
    • Details: The title uses [Consumption] for customer-facing changes, and the description includes purpose, changes, and testing steps as per the guideline.
  • [✔] I adhere to the Command Guidelines.
    • Details: The command az consumption usage list maintains consistency with existing patterns, ensures backward compatibility, and handles errors gracefully.
  • [✔] I adhere to the Error Handling Guidelines.
    • Details: Added safe attribute access with get() and filtering of None values to prevent exceptions like TypeError.

@azure-client-tools-bot-prd
Copy link

azure-client-tools-bot-prd bot commented Mar 25, 2025

️✔️AzureCLI-FullTest
️✔️acr
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️acs
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️advisor
️✔️latest
️✔️3.12
️✔️3.9
️✔️ams
️✔️latest
️✔️3.12
️✔️3.9
️✔️apim
️✔️latest
️✔️3.12
️✔️3.9
️✔️appconfig
️✔️latest
️✔️3.12
️✔️3.9
️✔️appservice
️✔️latest
️✔️3.12
️✔️3.9
️✔️aro
️✔️latest
️✔️3.12
️✔️3.9
️✔️backup
️✔️latest
️✔️3.12
️✔️3.9
️✔️batch
️✔️latest
️✔️3.12
️✔️3.9
️✔️batchai
️✔️latest
️✔️3.12
️✔️3.9
️✔️billing
️✔️latest
️✔️3.12
️✔️3.9
️✔️botservice
️✔️latest
️✔️3.12
️✔️3.9
️✔️cdn
️✔️latest
️✔️3.12
️✔️3.9
️✔️cloud
️✔️latest
️✔️3.12
️✔️3.9
️✔️cognitiveservices
️✔️latest
️✔️3.12
️✔️3.9
️✔️compute_recommender
️✔️latest
️✔️3.12
️✔️3.9
️✔️computefleet
️✔️latest
️✔️3.12
️✔️3.9
️✔️config
️✔️latest
️✔️3.12
️✔️3.9
️✔️configure
️✔️latest
️✔️3.12
️✔️3.9
️✔️consumption
️✔️latest
️✔️3.12
️✔️3.9
️✔️container
️✔️latest
️✔️3.12
️✔️3.9
️✔️containerapp
️✔️latest
️✔️3.12
️✔️3.9
️✔️core
️✔️2018-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2019-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️cosmosdb
️✔️latest
️✔️3.12
️✔️3.9
️✔️databoxedge
️✔️2019-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️dls
️✔️latest
️✔️3.12
️✔️3.9
️✔️dms
️✔️latest
️✔️3.12
️✔️3.9
️✔️eventgrid
️✔️latest
️✔️3.12
️✔️3.9
️✔️eventhubs
️✔️latest
️✔️3.12
️✔️3.9
️✔️feedback
️✔️latest
️✔️3.12
️✔️3.9
️✔️find
️✔️latest
️✔️3.12
️✔️3.9
️✔️hdinsight
️✔️latest
️✔️3.12
️✔️3.9
️✔️identity
️✔️latest
️✔️3.12
️✔️3.9
️✔️iot
️✔️2019-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️keyvault
️✔️2018-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️lab
️✔️latest
️✔️3.12
️✔️3.9
️✔️managedservices
️✔️latest
️✔️3.12
️✔️3.9
️✔️maps
️✔️latest
️✔️3.12
️✔️3.9
️✔️marketplaceordering
️✔️latest
️✔️3.12
️✔️3.9
️✔️monitor
️✔️latest
️✔️3.12
️✔️3.9
️✔️mysql
️✔️latest
️✔️3.12
️✔️3.9
️✔️netappfiles
️✔️latest
️✔️3.12
️✔️3.9
️✔️network
️✔️2018-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️policyinsights
️✔️latest
️✔️3.12
️✔️3.9
️✔️privatedns
️✔️latest
️✔️3.12
️✔️3.9
️✔️profile
️✔️latest
️✔️3.12
️✔️3.9
️✔️rdbms
️✔️latest
️✔️3.12
️✔️3.9
️✔️redis
️✔️latest
️✔️3.12
️✔️3.9
️✔️relay
️✔️latest
️✔️3.12
️✔️3.9
️✔️resource
️✔️2018-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2019-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️role
️✔️latest
️✔️3.12
️✔️3.9
️✔️search
️✔️latest
️✔️3.12
️✔️3.9
️✔️security
️✔️latest
️✔️3.12
️✔️3.9
️✔️servicebus
️✔️latest
️✔️3.12
️✔️3.9
️✔️serviceconnector
️✔️latest
️✔️3.12
️✔️3.9
️✔️servicefabric
️✔️latest
️✔️3.12
️✔️3.9
️✔️signalr
️✔️latest
️✔️3.12
️✔️3.9
️✔️sql
️✔️latest
️✔️3.12
️✔️3.9
️✔️sqlvm
️✔️latest
️✔️3.12
️✔️3.9
️✔️storage
️✔️2018-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2019-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️synapse
️✔️latest
️✔️3.12
️✔️3.9
️✔️telemetry
️✔️2018-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2019-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9
️✔️util
️✔️latest
️✔️3.12
️✔️3.9
️✔️vm
️✔️2018-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2019-03-01-hybrid
️✔️3.12
️✔️3.9
️✔️2020-09-01-hybrid
️✔️3.12
️✔️3.9
️✔️latest
️✔️3.12
️✔️3.9

@azure-client-tools-bot-prd
Copy link

azure-client-tools-bot-prd bot commented Mar 25, 2025

️✔️AzureCLI-BreakingChangeTest
️✔️Non Breaking Changes

@microsoft-github-policy-service microsoft-github-policy-service bot added the customer-reported Issues that are reported by GitHub users external to the Azure organization. label Mar 25, 2025
@microsoft-github-policy-service
Copy link
Contributor

Thank you for your contribution haule21! We will review the pull request and get back to you soon.

@yonzhan
Copy link
Collaborator

yonzhan commented Mar 25, 2025

Thank you for your contribution! We will review the pull request and get back to you soon.

@github-actions
Copy link

The git hooks are available for azure-cli and azure-cli-extensions repos. They could help you run required checks before creating the PR.

Please sync the latest code with latest dev branch (for azure-cli) or main branch (for azure-cli-extensions).
After that please run the following commands to enable git hooks:

pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>

@haule21
Copy link
Contributor Author

haule21 commented Mar 25, 2025

@haule21 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

Contribution License Agreement

This Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”), and conveys certain license rights to Microsoft Corporation and its affiliates (“Microsoft”) for Your contributions to Microsoft open source projects. This Agreement is effective as of the latest signature date below.

  1. Definitions.
    “Code” means the computer software code, whether in human-readable or machine-executable form,
    that is delivered by You to Microsoft under this Agreement.
    “Project” means any of the projects owned or managed by Microsoft and offered under a license
    approved by the Open Source Initiative (www.opensource.org).
    “Submit” is the act of uploading, submitting, transmitting, or distributing code or other content to any
    Project, including but not limited to communication on electronic mailing lists, source code control
    systems, and issue tracking systems that are managed by, or on behalf of, the Project for the purpose of
    discussing and improving that Project, but excluding communication that is conspicuously marked or
    otherwise designated in writing by You as “Not a Submission.”
    “Submission” means the Code and any other copyrightable material Submitted by You, including any
    associated comments and documentation.
  2. Your Submission. You must agree to the terms of this Agreement before making a Submission to any
    Project. This Agreement covers any and all Submissions that You, now or in the future (except as
    described in Section 4 below), Submit to any Project.
  3. Originality of Work. You represent that each of Your Submissions is entirely Your original work.
    Should You wish to Submit materials that are not Your original work, You may Submit them separately
    to the Project if You (a) retain all copyright and license information that was in the materials as You
    received them, (b) in the description accompanying Your Submission, include the phrase “Submission
    containing materials of a third party:” followed by the names of the third party and any licenses or other
    restrictions of which You are aware, and (c) follow any other instructions in the Project’s written
    guidelines concerning Submissions.
  4. Your Employer. References to “employer” in this Agreement include Your employer or anyone else
    for whom You are acting in making Your Submission, e.g. as a contractor, vendor, or agent. If Your
    Submission is made in the course of Your work for an employer or Your employer has intellectual
    property rights in Your Submission by contract or applicable law, You must secure permission from Your
    employer to make the Submission before signing this Agreement. In that case, the term “You” in this
    Agreement will refer to You and the employer collectively. If You change employers in the future and
    desire to Submit additional Submissions for the new employer, then You agree to sign a new Agreement
    and secure permission from the new employer before Submitting those Submissions.
  5. Licenses.
  • Copyright License. You grant Microsoft, and those who receive the Submission directly or
    indirectly from Microsoft, a perpetual, worldwide, non-exclusive, royalty-free, irrevocable license in the
    Submission to reproduce, prepare derivative works of, publicly display, publicly perform, and distribute
    the Submission and such derivative works, and to sublicense any or all of the foregoing rights to third
    parties.
  • Patent License. You grant Microsoft, and those who receive the Submission directly or
    indirectly from Microsoft, a perpetual, worldwide, non-exclusive, royalty-free, irrevocable license under
    Your patent claims that are necessarily infringed by the Submission or the combination of the
    Submission with the Project to which it was Submitted to make, have made, use, offer to sell, sell and
    import or otherwise dispose of the Submission alone or with the Project.
  • Other Rights Reserved. Each party reserves all rights not expressly granted in this Agreement.
    No additional licenses or rights whatsoever (including, without limitation, any implied licenses) are
    granted by implication, exhaustion, estoppel or otherwise.
  1. Representations and Warranties. You represent that You are legally entitled to grant the above
    licenses. You represent that each of Your Submissions is entirely Your original work (except as You may
    have disclosed under Section 3). You represent that You have secured permission from Your employer to
    make the Submission in cases where Your Submission is made in the course of Your work for Your
    employer or Your employer has intellectual property rights in Your Submission by contract or applicable
    law. If You are signing this Agreement on behalf of Your employer, You represent and warrant that You
    have the necessary authority to bind the listed employer to the obligations contained in this Agreement.
    You are not expected to provide support for Your Submission, unless You choose to do so. UNLESS
    REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, AND EXCEPT FOR THE WARRANTIES
    EXPRESSLY STATED IN SECTIONS 3, 4, AND 6, THE SUBMISSION PROVIDED UNDER THIS AGREEMENT IS
    PROVIDED WITHOUT WARRANTY OF ANY KIND, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY OF
    NONINFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
  2. Notice to Microsoft. You agree to notify Microsoft in writing of any facts or circumstances of which
    You later become aware that would make Your representations in this Agreement inaccurate in any
    respect.
  3. Information about Submissions. You agree that contributions to Projects and information about
    contributions may be maintained indefinitely and disclosed publicly, including Your name and other
    information that You submit with Your Submission.
  4. Governing Law/Jurisdiction. This Agreement is governed by the laws of the State of Washington, and
    the parties consent to exclusive jurisdiction and venue in the federal courts sitting in King County,
    Washington, unless no federal subject matter jurisdiction exists, in which case the parties consent to
    exclusive jurisdiction and venue in the Superior Court of King County, Washington. The parties waive all
    defenses of lack of personal jurisdiction and forum non-conveniens.
  5. Entire Agreement/Assignment. This Agreement is the entire agreement between the parties, and
    supersedes any and all prior agreements, understandings or communications, written or oral, between
    the parties relating to the subject matter hereof. This Agreement may be assigned by Microsoft.

@microsoft-github-policy-service agree

@kairu-ms
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

haule21 and others added 3 commits March 27, 2025 11:14
Co-authored-by: kai ru <69238381+kairu-ms@users.noreply.github.com>
Co-authored-by: kai ru <69238381+kairu-ms@users.noreply.github.com>
Co-authored-by: kai ru <69238381+kairu-ms@users.noreply.github.com>
@kairu-ms
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@kairu-ms kairu-ms changed the title [Consumption] Handle None UsageStart in usage list and exclude from results [Consumption] az consumption usage list: Enhanced handling of usageStart and usageEnd when missing Mar 27, 2025
kairu-ms
kairu-ms previously approved these changes Mar 27, 2025
Co-authored-by: kai ru <69238381+kairu-ms@users.noreply.github.com>
@kairu-ms
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

kairu-ms
kairu-ms previously approved these changes Mar 27, 2025
@haule21
Copy link
Contributor Author

haule21 commented Apr 7, 2025

Hello, @kairu-ms

Just wanted to let you know I’ve fixed the whitespace issue that was causing the tests to fail, and I’ve pushed the update.
Could you take a look when you get a chance and either leave a review or close the PR?
Let me know if anything else is needed! Thanks a lot!

Copy link
Contributor Author

@haule21 haule21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove White Space.

@kairu-ms
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@kairu-ms kairu-ms closed this Apr 17, 2025
@kairu-ms kairu-ms reopened this Apr 17, 2025
@kairu-ms
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@kairu-ms kairu-ms merged commit 0140fd7 into Azure:dev Apr 18, 2025
53 checks passed
@TiloGit
Copy link

TiloGit commented May 8, 2025

@haule21 thanks for trying to fix this.
Now the error is gone but no output is provided.
Does the endpoint

https://management.azure.com: "GET /subscriptions/90zzzzzzzzzzzzzzz/providers/Microsoft.Consumption/usageDetails?$filter=properties%2FusageEnd%20ge%20%272025-03-06T00%3A00%3A00Z%27%20and%20properties%2FusageEnd%20le%20%272025-04-05T00%3A00%3A00Z%27&api-version=2023-05-01

even provide usageStart or usageEnd in the JSON response? see discussion in issue #28323

here from DEBUG my json retrun:

{
    "value": [
        {
            "id": "/subscriptions/57ab3509-zzzz-zzzz-zzzz-zzzzzzzbad2c/providers/Microsoft.Billing/billingPeriods/202504/providers/Microsoft.Consumption/usageDetails/6f5c04c0-zzzz-zzzz-zzzz-zzzzzzzbecb",
            "kind": "legacy",
            "name": "6f5c04c0-zzzz-zzzz-zzzz-zzzzzzzbecb",
            "properties": {
                "billingCurrency": "USD",
                "billingPeriodEndDate": "2025-04-19T00:00:00.0000000Z",
                "billingPeriodStartDate": "2025-03-20T00:00:00.0000000Z",
                "billingProfileId": "/subscriptions/57ab3509-zzzz-zzzz-zzzz-zzzzzzzbad2c",
                "billingProfileName": "myAZsubName123",
                "chargeType": "Usage",
                "consumedService": "microsoft.network",
                "cost": 0.0864,
                "date": "2025-04-01T00:00:00.0000000Z",
                "effectivePrice": 0.0036,
                "frequency": "UsageBased",
                "isAzureCreditEligible": false,
                "meterDetails": null,
                "meterId": "26ce34b7-67b3-480d-9d1b-54a7fb80f67a",
                "offerId": "MS-AZR-0029P",
                "product": "IP Addresses - Basic IPv4 Static Public IP",
                "publisherType": "Azure",
                "quantity": 24,
                "resourceGroup": "myAZrg123",
                "resourceId": "/subscriptions/57ab3509-zzzz-zzzz-zzzz-zzzzzzzbad2c/resourcegroups/myAZrg123/providers/microsoft.network/publicipaddresses/brava11-ip",
                "resourceLocation": "US West 2",
                "resourceName": "myp-ip",
                "subscriptionId": "57ab3509-zzzz-zzzz-zzzz-zzzzzzzbad2c",
                "subscriptionName": "myAZsubName123",
                "unitPrice": 0
            },
            "tags": {
                "vf-creator": "joedoe@mycomp.com",
                "vf-date-created": "2023-06-14T00:37:22.0315752Z"
            },
            "type": "Microsoft.Consumption/usageDetails"
        },
        {
            "id": "/subscriptions/57ab3509-zzzz-zzzz-zzzz-zzzzzzzbad2c/providers/Microsoft.Billing/billingPeriods/202504/providers/Microsoft.Consumption/usageDetails/e5b711e6-zzzz-zzzz-zzzz-zzzzzzz52c30ab",
            "kind": "legacy",
            "name": "e5b711e6-zzzz-zzzz-zzzz-zzzzzzz52c30ab",
            "properties": {
                "billingCurrency": "USD",
                "billingPeriodEndDate": "2025-04-19T00:00:00.0000000Z",
                "billingPeriodStartDate": "2025-03-20T00:00:00.0000000Z",
                "billingProfileId": "/subscriptions/57ab3509-zzzz-zzzz-zzzz-zzzzzzzbad2c",
                "billingProfileName": "myAZsubName123",
                "chargeType": "Usage",
                "consumedService": "Microsoft.Storage",
                "cost": 0.00231012,
                "date": "2025-03-29T00:00:00.0000000Z",
                "effectivePrice": 0.045,
                "frequency": "UsageBased",
                "isAzureCreditEligible": false,
                "meterDetails": null,
                "meterId": "3f2b1e1c-c886-4ec6-ad6f-dd0ef38819c9",
                "offerId": "MS-AZR-0029P",
                "product": "Tables - LRS Data Stored",
                "publisherType": "Azure",
                "quantity": 0.051336,
                "resourceGroup": "myAZrg123",
                "resourceId": "/subscriptions/57ab3509-zzzz-zzzz-zzzz-zzzzzzzbad2c/resourceGroups/myAZrg123/providers/Microsoft.Storage/storageAccounts/mystordiag",
                "resourceLocation": "US West 2",
                "resourceName": "mystordiag",
                "subscriptionId": "57ab3509-zzzz-zzzz-zzzz-zzzzzzzbad2c",
                "subscriptionName": "myAZsubName123",
                "unitPrice": 0
            },
            "tags": null,
            "type": "Microsoft.Consumption/usageDetails"
        }
    ]
}

@haule21
Copy link
Contributor Author

haule21 commented May 10, 2025

@TiloGit

Hi, I’m not entirely sure either, but I’ll respond based on my analysis of the source code.

From what I can tell, the UsageStart and UsageEnd values appear to be properties that are only included when you have ownership of the Billing Account. If they’re not showing up in your case, it’s likely that your subscription is under an account type that doesn’t have control over the billing account.
(It looks like your JSON data is being queried based on the Subscription rather than the Billing Account.)

Please refer to the link below for more details:

https://learn.microsoft.com/en-us/azure/cost-management-billing/manage/view-all-accounts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

customer-reported Issues that are reported by GitHub users external to the Azure organization.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Comments