-
Notifications
You must be signed in to change notification settings - Fork 6.5k
fix(functions): WIP update and fix functions_billing_stop sample #13359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix(functions): WIP update and fix functions_billing_stop sample #13359
Conversation
- Moved to another folder since it requires specific requirements
- replace googleapiclient.discovery with google.cloud.billing_v1 - unify namings with Node sample - pass lint
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @eapl-gemugami, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
Summary of Changes
This pull request updates the functions_billing_stop
sample for a Cloud Run Function, ensuring it meets current Node 20 requirements. The changes include adding necessary dependencies in requirements.txt
and requirements-test.txt
, and updating the main function logic in stop_billing.py
. The author has manually validated the changes in a Cloud Run environment and has performed linting.
Highlights
- Dependency Updates: The pull request adds
functions-framework==3.*
andgoogle-cloud-billing==1.16.2
torequirements.txt
, andpytest==8.3.5
torequirements-test.txt
. - Function Logic: The
stop_billing.py
file is created, containing the core logic for stopping billing based on budget and cost thresholds. It includes functions to check if billing is enabled and to disable billing for a project. - Node 20 Compatibility: The sample is updated and validated to be compatible with Node 20 requirements.
Changelog
- functions/billing/stop_billing/requirements-test.txt
- Added
pytest==8.3.5
to the test requirements.
- Added
- functions/billing/stop_billing/requirements.txt
- Added
functions-framework==3.*
andgoogle-cloud-billing==1.16.2
as dependencies.
- Added
- functions/billing/stop_billing/stop_billing.py
- Created the
stop_billing.py
file with the main function logic for stopping billing. - Implemented functions to get project ID from metadata, check if billing is enabled, and disable billing for a project.
- Added a Cloud Functions entry point
stop_billing
that triggers the billing stop logic based on cost and budget.
- Created the
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
A function's call,
Billing stops before the fall,
Budget's safe and sound.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
The pull request updates the Cloud Run Function sample for disabling billing with notifications to meet current Node 20 requirements. The changes include updating dependencies and simulating the disabling of billing. The absence of automated tests is noted, with manual validations performed in the Cloud Run environment.
Summary of Findings
- Missing Error Handling: The
get_project_id_from_metadata
function raises a ValueError if the project ID is not found, but this exception is not handled in thestop_billing
function. This could lead to the function crashing if the project ID cannot be retrieved. - Simulated Billing Disablement: The code currently simulates disabling billing instead of actually disabling it. This is indicated by the
DEBUG: Simulate disabling billing
print statement and the commented-out code for updating the billing info. This needs to be addressed for the function to work as intended. - Incomplete Argument Documentation: The docstrings for
_is_billing_enabled
and_disable_billing_for_project
refer to aprojects
argument that is not actually used in the function signatures. This is misleading and should be corrected.
Merge Readiness
The pull request requires changes before merging. Specifically, the simulated billing disablement needs to be replaced with actual billing disablement logic, and the exception handling for retrieving the project ID should be improved. Additionally, the argument documentation for the helper functions should be corrected. I am unable to approve this pull request, and recommend that others review and approve this code before merging.
# DEBUG: Simulate disabling billing | ||
print("Billing disabled. (Simulated)") | ||
|
||
# response = billing_client.update_project_billing_info( | ||
# name=project_name, | ||
# project_billing_info=project_billing_info | ||
# ) | ||
# print(f"Billing disabled: {response}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section simulates disabling billing. The commented-out code should be uncommented and the simulation removed to actually disable billing. Ensure that the function has the necessary permissions to update the billing info.
# DEBUG: Simulate disabling billing | |
print("Billing disabled. (Simulated)") | |
# response = billing_client.update_project_billing_info( | |
# name=project_name, | |
# project_billing_info=project_billing_info | |
# ) | |
# print(f"Billing disabled: {response}") | |
# DEBUG: Simulate disabling billing | |
# print("Billing disabled. (Simulated)" | |
response = billing_client.update_project_billing_info( | |
name=project_name, | |
project_billing_info=project_billing_info | |
) | |
print(f"Billing disabled: {response}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be uncommented in the final PR. For now the purpose is to show how the code has been tested in the Cloud Run environment.
if project_id is None: | ||
raise ValueError("PROJECT_ID not found.") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider logging the error instead of raising a ValueError. This would allow the function to continue running and potentially recover from the error, or at least provide more context in the logs.
if project_id is None: | |
raise ValueError("PROJECT_ID not found.") | |
if project_id is None: | |
print("PROJECT_ID not found.") | |
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is to stop execution as the PROJECT_ID is a key part of disabling billing (for the project).
Args: | ||
project_name: Name of project to check if billing is enabled. | ||
projects: Resource for interacting with the Billing API. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, this pydoc is outdated and needs to be consider billing_client: billing_v1.CloudBillingClient
Args: | ||
project_name: Name of project to disable billing. | ||
projects: Resource for interacting with the Billing API. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, this pydoc is outdated and needs to be consider billing_client: billing_v1.CloudBillingClient
print("No action required. Current cost is within budget.") | ||
return | ||
|
||
# Create a Billing client. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: remove the period at the end of the comment to unify
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
…g billing - Style fixes - Create the billing client at the top of the script
Description
This sample for a Cloud Run Function used in disable-billing-with-notifications#create-cloud-run-function is updated and validated to current Python 3.9 requirements.
See the FL for more details.
Checklist
nox -s py-3.9
(see Test Environment Setup) <- There is no automatic tests yet, the validations were made manually in the Cloud Run enviornment. See the FL for more detail.nox -s lint
(see Test Environment Setup)functions/billing/
I'm not sure if this step is needed.