在云功能中使用gcloud cli [英] Using gcloud cli within a cloud function

查看:55
本文介绍了在云功能中使用gcloud cli的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在云功能SDK中,SDK对GCP API的访问权限非常有限,但数量仍然有限.例如节点.

我想在云函数中调用 gcloud cli.这可能吗?例如

gcloud sql实例修补my-database --activation-policy = NEVER

目标是每晚关闭SQL实例

解决方案

我相信您应该使用 Cloud SQL Admin API .例如,如果您使用的是Python运行时,则您的需求文件和相应的来自googleapiclient.discovery导入构建的

 服务=构建('sqladmin','v1beta4')projid =''#project id Cloud SQL实例所在的位置instance =''#Cloud SQL实例patch = {'settings':{'activationPolicy':'NEVER'}}req = service.instances().patch(项目= projid,instance = instance,body = patch)x = req.execute()打印(x) 

There are great but still limited set of SDK access to GCP APIs within cloud function SDKs. e.g. Node.

I want to call gcloud cli within a cloud function. Is this possible? e.g.

gcloud sql instances patch my-database --activation-policy=NEVER

The goal is nightly shutdown of an SQL instance

解决方案

I believe you should use the Cloud SQL Admin API. If you're using the Python runtime for example you'd had 'google-api-python-client==1.7.8' (for example) to your requirements file and on the respective client library you would use the method instances.patch with the appropriate parameters.

Hope this helps.

Also you have here a working example with the Python runtime, just be sure to edit the 'projid' and 'instance' variables accordingly.

from googleapiclient.discovery import build

service = build('sqladmin', 'v1beta4')

projid = '' #project id where Cloud SQL instance is
instance = '' #Cloud SQL instance

patch = {'settings': {'activationPolicy':'NEVER'}}

req = service.instances().patch(project=projid, instance=instance, body=patch)

x = req.execute()
print(x)

这篇关于在云功能中使用gcloud cli的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆