Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

eapl-gemugami
Copy link
Contributor

@eapl-gemugami eapl-gemugami commented May 8, 2025

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

  • I have followed Sample Guidelines from AUTHORING_GUIDE.MD
  • README is updated to include all relevant information <- There is no README, but fixes to documentation will be needed after this PR is accepted.
  • Tests pass: 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.
  • Lint pass: nox -s lint (see Test Environment Setup)
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample <- A subdirectory was created inside functions/billing/ I'm not sure if this step is needed.
  • Please merge this PR for me once it is approved

- Moved to another folder since it requires specific requirements
- replace googleapiclient.discovery with google.cloud.billing_v1
- unify namings with Node sample
- pass lint
@product-auto-label product-auto-label bot added api: cloudfunctions Issues related to the Cloud Run functions API. samples Issues that are directly related to samples. labels May 8, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.* and google-cloud-billing==1.16.2 to requirements.txt, and pytest==8.3.5 to requirements-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.
  • functions/billing/stop_billing/requirements.txt
    • Added functions-framework==3.* and google-cloud-billing==1.16.2 as dependencies.
  • 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.
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

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

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 the stop_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 a projects 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.

Comment on lines 117 to 124
# 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}")
Copy link
Contributor

Choose a reason for hiding this comment

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

high

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.

Suggested change
# 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}")

Copy link
Contributor Author

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.

Comment on lines 33 to 35
if project_id is None:
raise ValueError("PROJECT_ID not found.")

Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
if project_id is None:
raise ValueError("PROJECT_ID not found.")
if project_id is None:
print("PROJECT_ID not found.")
return None

Copy link
Contributor Author

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The projects argument is not used in the function. It should be removed from the docstring and the function signature.

Suggested change
projects: Resource for interacting with the Billing API.
project_name: Name of project to check if billing is enabled.

Copy link
Contributor Author

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The projects argument is not used in the function. It should be removed from the docstring and the function signature.

Suggested change
projects: Resource for interacting with the Billing API.
project_name: Name of project to disable billing.

Copy link
Contributor Author

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

@eapl-gemugami eapl-gemugami requested a review from OremGLG May 8, 2025 20:01
print("No action required. Current cost is within budget.")
return

# Create a Billing client.
Copy link
Contributor Author

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

@eapl-gemugami eapl-gemugami added the snippet-bot:force-run Force snippet-bot runs its logic label May 9, 2025
@snippet-bot snippet-bot bot removed the snippet-bot:force-run Force snippet-bot runs its logic label May 9, 2025
Copy link

snippet-bot bot commented May 9, 2025

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

…g billing

- Style fixes
- Create the billing client at the top of the script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: cloudfunctions Issues related to the Cloud Run functions API. samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant