Using gcloud cli within a cloud function

纵然是瞬间 提交于 2021-01-29 06:21:04

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/55235735/using-gcloud-cli-within-a-cloud-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!