当您禁用某个 API 时,您可能会删除该 API 创建的所有资源,并且您还将禁用依赖于您正在禁用的 API 的其他 API。根据您在提到的特定情况下您在项目中使用的产品,如果您禁用 Pub/Sub API,您可能会禁用以下 API:
cloudbuild.googleapis.com
cloudfunctions.googleapis.com
containerregistry.googleapis.com
run.googleapis.com
...among others
如果您意识到禁用 API(以及所有其他相关 API)可能会对您的项目造成风险和中断,如果您在生产中使用以下代码,请使用服务使用 API 的 disable 方法从Python 客户端库将工作将禁用 Dataflow API:
from googleapiclient import discovery
import google.auth
def hello_world(request):
credentials, project_id = google.auth.default()
name = 'projects/[PROJECT-NUMBER-NOT-ID]/services/dataflow.googleapis.com'
body = {'disableDependentServices': True,}
service = discovery.build('serviceusage', 'v1', credentials=credentials, cache_discovery=False)
request = service.services().disable(name=name, body=body)
try:
response = request.execute()
except Exception as e:
print(e)
return "API disabled"
通过检查您的活动日志,您应该会在触发云功能后看到与以下类似的消息:
9:12 AM Completed: google.api.serviceusage.v1.ServiceUsage.DisableService [PROJECT-ID]@appspot.gserviceaccount.com has executed google.api.serviceusage.v1.ServiceUsage.DisableService on dataflow.googleapis.com
9:12 AM google.api.serviceusage.v1.ServiceUsage.DisableService [PROJECT-ID]@appspot.gserviceaccount.com has executed google.api.serviceusage.v1.ServiceUsage.DisableService on dataflow.googleapis.com
请注意,根据您使用的产品,这种禁用 API 的方法可能会停止与网络流量等相关的所有计费费用,但一般而言,Cloud SQL 实例中的存储定价等费用仍会继续产生。
总的来说,我个人认为这不是最好的方法(因为如果您有一个生产中的应用程序并且其他 API 依赖于该 API,那么禁用 API 可能会非常危险),我通常会考虑使用预算和预算警报(也使用 Cloud Functions 和 Pub/Sub 等其他服务来接收通知)。查找文档中有关预算和预算警报 here 和 here 的所有相关部分。