|
14 | 14 |
|
15 | 15 | import base64 |
16 | 16 | import json |
17 | | -import os |
18 | | -from googleapiclient import discovery |
19 | | -PROJECT_ID = os.getenv('GCP_PROJECT') |
20 | | -PROJECT_NAME = f'projects/{PROJECT_ID}' |
21 | | -def stop_billing(data, context): |
22 | | - pubsub_data = base64.b64decode(data['data']).decode('utf-8') |
23 | | - pubsub_json = json.loads(pubsub_data) |
24 | | - cost_amount = pubsub_json['costAmount'] |
25 | | - budget_amount = pubsub_json['budgetAmount'] |
26 | | - if cost_amount <= budget_amount: |
27 | | - print(f'No action necessary. (Current cost: {cost_amount})') |
28 | | - return |
29 | 17 |
|
30 | | - if PROJECT_ID is None: |
31 | | - print('No project specified with environment variable') |
32 | | - return |
| 18 | +import google.auth |
| 19 | +from google.cloud import billing |
33 | 20 |
|
34 | | - billing = discovery.build( |
35 | | - 'cloudbilling', |
36 | | - 'v1', |
37 | | - cache_discovery=False, |
38 | | - ) |
39 | 21 |
|
40 | | - projects = billing.projects() |
| 22 | +PROJECT_ID = google.auth.default()[1] |
| 23 | +cloud_billing_client = billing.CloudBillingClient() |
| 24 | + |
| 25 | + |
| 26 | +def stop_billing(data: dict, context): |
| 27 | + pubsub_data = base64.b64decode(data["data"]).decode("utf-8") |
| 28 | + pubsub_json = json.loads(pubsub_data) |
| 29 | + cost_amount = pubsub_json["costAmount"] |
| 30 | + budget_amount = pubsub_json["budgetAmount"] |
| 31 | + if cost_amount <= budget_amount: |
| 32 | + print(f"No action necessary. (Current cost: {cost_amount})") |
| 33 | + return |
41 | 34 |
|
42 | | - billing_enabled = __is_billing_enabled(PROJECT_NAME, projects) |
| 35 | + project_name = cloud_billing_client.common_project_path(PROJECT_ID) |
| 36 | + billing_enabled = _is_billing_enabled(project_name) |
43 | 37 |
|
44 | 38 | if billing_enabled: |
45 | | - __disable_billing_for_project(PROJECT_NAME, projects) |
| 39 | + _disable_billing_for_project(project_name) |
46 | 40 | else: |
47 | | - print('Billing already disabled') |
| 41 | + print("Billing already disabled") |
48 | 42 |
|
49 | 43 |
|
50 | | -def __is_billing_enabled(project_name, projects): |
51 | | - """ |
52 | | - Determine whether billing is enabled for a project |
53 | | - @param {string} project_name Name of project to check if billing is enabled |
54 | | - @return {bool} Whether project has billing enabled or not |
55 | | - """ |
56 | | - try: |
57 | | - res = projects.getBillingInfo(name=project_name).execute() |
58 | | - return res['billingEnabled'] |
59 | | - except KeyError: |
60 | | - # If billingEnabled isn't part of the return, billing is not enabled |
61 | | - return False |
62 | | - except Exception: |
63 | | - print('Unable to determine if billing is enabled on specified project, assuming billing is enabled') |
64 | | - return True |
65 | | - |
66 | | - |
67 | | -def __disable_billing_for_project(project_name, projects): |
| 44 | +def _is_billing_enabled(project_name: str) -> bool: |
| 45 | + """Determine whether billing is enabled for a project |
| 46 | +
|
| 47 | + Args: |
| 48 | + project_name (str): Name of project to check if billing is enabled |
| 49 | +
|
| 50 | + Returns: |
| 51 | + bool: Whether project has billing enabled or not |
68 | 52 | """ |
69 | | - Disable billing for a project by removing its billing account |
70 | | - @param {string} project_name Name of project disable billing on |
| 53 | + request = billing.GetProjectBillingInfoRequest(name=project_name) |
| 54 | + project_billing_info = cloud_billing_client.get_project_billing_info(request) |
| 55 | + |
| 56 | + return project_billing_info.billing_enabled |
| 57 | + |
| 58 | + |
| 59 | +def _disable_billing_for_project(project_name: str) -> None: |
| 60 | + """Disable billing for a project by removing its billing account |
| 61 | +
|
| 62 | + Args: |
| 63 | + project_name (str): Name of project disable billing on |
71 | 64 | """ |
72 | | - body = {'billingAccountName': ''} # Disable billing |
73 | | - try: |
74 | | - res = projects.updateBillingInfo(name=project_name, body=body).execute() |
75 | | - print(f'Billing disabled: {json.dumps(res)}') |
76 | | - except Exception: |
77 | | - print('Failed to disable billing, possibly check permissions') |
| 65 | + request = billing.UpdateProjectBillingInfoRequest( |
| 66 | + name=project_name, |
| 67 | + project_billing_info=billing.ProjectBillingInfo( |
| 68 | + billing_account_name="" # Disable billing |
| 69 | + ), |
| 70 | + ) |
| 71 | + project_biling_info = cloud_billing_client.update_project_billing_info(request) |
| 72 | + print(f"Billing disabled: {project_biling_info}") |
0 commit comments