【发布时间】:2020-05-22 18:24:23
【问题描述】:
我正在尝试编写一个 Google Cloud 函数来设置上限以禁用超过特定限制的使用。我按照这里的说明进行操作:https://cloud.google.com/billing/docs/how-to/notify#cap_disable_billing_to_stop_usage。
这就是我的云功能的样子(我只是从上面链接的 Google Cloud 文档页面复制和粘贴):
import base64
import json
import os
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
PROJECT_ID = os.getenv('GCP_PROJECT')
PROJECT_NAME = f'projects/{PROJECT_ID}'
def stop_billing(data, context):
pubsub_data = base64.b64decode(data['data']).decode('utf-8')
pubsub_json = json.loads(pubsub_data)
cost_amount = pubsub_json['costAmount']
budget_amount = pubsub_json['budgetAmount']
if cost_amount <= budget_amount:
print(f'No action necessary. (Current cost: {cost_amount})')
return
billing = discovery.build(
'cloudbilling',
'v1',
cache_discovery=False,
credentials=GoogleCredentials.get_application_default()
)
projects = billing.projects()
if __is_billing_enabled(PROJECT_NAME, projects):
print(__disable_billing_for_project(PROJECT_NAME, projects))
else:
print('Billing already disabled')
def __is_billing_enabled(project_name, projects):
"""
Determine whether billing is enabled for a project
@param {string} project_name Name of project to check if billing is enabled
@return {bool} Whether project has billing enabled or not
"""
res = projects.getBillingInfo(name=project_name).execute()
return res['billingEnabled']
def __disable_billing_for_project(project_name, projects):
"""
Disable billing for a project by removing its billing account
@param {string} project_name Name of project disable billing on
@return {string} Text containing response from disabling billing
"""
body = {'billingAccountName': ''} # Disable billing
res = projects.updateBillingInfo(name=project_name, body=body).execute()
print(f'Billing disabled: {json.dumps(res)}')
还附上 Google Cloud Function UI 上的截图:
我还附上了一个屏幕截图,表明我也将相关内容复制并粘贴到了requirements.txt 文件中。
但是当我去测试代码时,它给了我一个错误:
Expand all | Collapse all{
insertId: "000000-69dce50a-e079-45ed-b949-a241c97fdfe4"
labels: {…}
logName: "projects/stanford-cs-231n/logs/cloudfunctions.googleapis.com%2Fcloud-functions"
receiveTimestamp: "2020-02-06T16:24:26.800908134Z"
resource: {…}
severity: "ERROR"
textPayload: "Traceback (most recent call last):
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 383, in run_background_function
_function_handler.invoke_user_function(event_object)
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 217, in invoke_user_function
return call_user_function(request_or_event)
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 214, in call_user_function
event_context.Context(**request_or_event.context))
File "/user_code/main.py", line 9, in stop_billing
pubsub_data = base64.b64decode(data['data']).decode('utf-8')
KeyError: 'data'
"
timestamp: "2020-02-06T16:24:25.411Z"
trace: "projects/stanford-cs-231n/traces/8e106d5ab629141d5d91b6b68fb30c82"
}
知道为什么吗?
【问题讨论】:
-
您可以尝试打印“数据”吗?
-
您的问题在于调用
stop_billing的代码。您正在尝试访问第 9 行的字典data中没有的键data。由于您的问题中不存在该代码,因此很难回答。注意:不要将您的代码显示为图片。编辑您的问题并将代码包含为格式化的代码块。 -
看起来你的函数被 Pub/Sub 调用了。第一个参数是
event。这通常具有密钥data。我会将字典记录到 Stackdriver 以调试您实际收到的内容。另外,我会添加这样的代码来防止崩溃if 'data' in 'data':。将data重命名为event以使您的变量名有意义。 -
@JohnHanley -- 谢谢,会试一试。编辑原始帖子以显示实际代码;但是,我只是从 Google Cloud 文档中复制粘贴:cloud.google.com/billing/docs/how-to/…(我没有做任何更改)
-
我尝试设置它,但我没有遇到任何问题,您是否将要求添加到文档中提到的功能的 requirements.txt 文件中?
标签: python google-cloud-platform google-cloud-functions google-cloud-billing