使用 Google Cloud Function 在 Google Cloud 上禁用计费

Disabling Billing on Google Cloud Using Google Cloud Function (Keyerror: #39;data#39;)(使用 Google Cloud Function 在 Google Cloud 上禁用计费(Keyerror: data))

本文介绍了使用 Google Cloud Function 在 Google Cloud 上禁用计费(Keyerror: 'data')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 Google Cloud 函数来设置上限以禁用超过特定限制的使用.我按照这里的说明操作:

我还附上了一个屏幕截图,以表明我也将相关内容复制并粘贴到 requirements.txt 文件中.

但是当我去测试代码时,它给了我一个错误:

全部展开 |全部收缩{insertId:000000-69dce50a-e079-45ed-b949-a241c97fdfe4"标签: {…}日志名称:项目/stanford-cs-231n/logs/cloudfunctions.googleapis.com%2Fcloud-functions"接收时间戳:2020-02-06T16:24:26.800908134Z"资源:{…}严重性:错误"textPayload:回溯(最近一次通话最后一次):文件/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py",第 383 行,在 run_background_function_function_handler.invoke_user_function(event_object)文件/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py",第 217 行,invoke_user_function返回 call_user_function(request_or_event)call_user_function 中的文件/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py",第 214 行event_context.Context(**request_or_event.context))文件/user_code/main.py",第 9 行,在 stop_billing 中pubsub_data = base64.b64decode(data['data']).decode('utf-8')键错误:数据""时间戳:2020-02-06T16:24:25.411Z"跟踪:项目/stanford-cs-231n/traces/8e106d5ab629141d5d91b6b68fb30c82"}

知道为什么吗?

相关堆栈溢出帖子:https://stackoverflow.com/a/58673874/3507127

解决方案

Google 提供的代码似乎有错误.当我更改 stop_billing 函数时,它开始工作了:

def stop_billing(数据,上下文):如果 data.keys() 中的数据":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']别的:cost_amount = 数据['costAmount']预算金额=数据['预算金额']如果 cost_amount <= budget_amount:print(f'无需任何操作.(当前成本:{cost_amount})')返回如果 PROJECT_ID 为无:print('没有指定环境变量的项目')返回billing = discovery.build('cloudbilling', 'v1', cache_discovery=False, )项目 = billing.projects()billing_enabled = __is_billing_enabled(PROJECT_NAME,项目)如果 billing_enabled:__disable_billing_for_project(项目名称,项目)别的:print('计费已禁用')

问题在于 pub/sub 消息以 json 消息的形式提供输入,其中包含 base64 编码的数据"条目.在测试功能中,您提供的 json 条目没有数据"键,也没有对其进行编码.这在我上面重写的函数中进行了检查.

I am attempting to write a Google Cloud Function to set caps to disable usage above a certain limit. I followed the instructions here: https://cloud.google.com/billing/docs/how-to/notify#cap_disable_billing_to_stop_usage.

This is what my cloud function looks like (I am just copying and pasting from the Google Cloud docs page linked above):

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)}')

Also attaching screenshot of what it looks like on Google Cloud Function UI:

I'm also attaching a screenshot to show that I copied and pasted the relevant things to the requirements.txt file as well.

But when I go to test the code, it gives me an error:

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"  
}

Any idea why?

Relevant Stack Overflow Post: https://stackoverflow.com/a/58673874/3507127

解决方案

There seems to be an error in the code Google provided. I got it working when I changed the stop_billing function:

def stop_billing(data, context):
    if 'data' in data.keys():
        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']
    else:
        cost_amount = data['costAmount']
        budget_amount = data['budgetAmount']
    if cost_amount <= budget_amount:
        print(f'No action necessary. (Current cost: {cost_amount})')
        return    
    if PROJECT_ID is None:
        print('No project specified with environment variable')
        return
    billing = discovery.build('cloudbilling', 'v1', cache_discovery=False, )
    projects = billing.projects()
    billing_enabled = __is_billing_enabled(PROJECT_NAME, projects)
    if billing_enabled:
        __disable_billing_for_project(PROJECT_NAME, projects)
    else:
        print('Billing already disabled')

The problem is that the pub/sub message provides input as a json message with a 'data' entry that is base64 encoded. In the testing functionality you provide the json entry without a 'data' key and without encoding it. This is checked for in the function that I rewrote above.

这篇关于使用 Google Cloud Function 在 Google Cloud 上禁用计费(Keyerror: 'data')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 Google Cloud Function 在 Google Cloud 上禁用计费

基础教程推荐