如何使用 Python3 转换 on/off CloudSQL 实例

How to turn on/off CloudSQL instances using Python3

我正在尝试使用 Python 脚本在 Google 云中转换 on/off CloudSQL 实例。我终于找到了一种方法,使用 GoogleCloudAPI,在 Shell:

### shell script
ACCESS_TOKEN="$(gcloud auth print-access-token)"
ACTIVATION_POLICY="ALWAYS/NEVER" # Use 'ALWAYS' to turn on, 'NEVER' to turn off
curl --header "Authorization: Bearer ${ACCESS_TOKEN}"  --header 'Content-Type: application/json' --data '{"settings" : {"activationPolicy" : "${ACTIVATION_POLICY}"}}' -X PATCH https://www.googleapis.com/sql/v1beta4/projects/${PROJECT_ID}/instances/${INSTANCE_ID}

太好了,问题解决了...除了我无法在机器上使用 'gcloud auth print-access-token' 我在 运行 脚本中,所以什么也解决不了。我发现一个 也试图使用 Python 生成这个 'access-token',这显然也没有用。

我需要能够使用 Python 本身生成此 'access-token'。我一直在查看 Google 的文档,但我仍然没有找到与此相关的任何内容,我发现最接近的是使用 oauth2 和 googleapiclient 来获取实例列表 运行,但似乎无法从那里更改激活策略:

### python3 script
from google.oauth2 import service_account
import googleapiclient.discovery
SCOPES = ['https://www.googleapis.com/auth/sqlservice.admin']
SERVICE_ACCOUNT_FILE = '/path/to/service.json'
credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)
sqladmin = googleapiclient.discovery.build('sqladmin', 'v1beta4', credentials=credentials)
response = sqladmin.instances().get(project=PROJECT_ID, instance=INSTANCE_ID).execute()

该文档没有明确说明如何使用任何一种工具关闭 CloudSQL 实例,或者至少 none 我可以找到关闭 CloudSQL 实例的方法。上面的代码 returns 我有一个 JSON 文件,我可以在设置下看到 'activationPolicy' 。不过我找不到 'change it' 的方法。

设法遵循@norbjd 的建议并找到一个 'patch' 方法,并授予 'SQL Admin' 我的凭据权限,因此它现在可以使用 sqladmin API。尝试使用以下代码对其进行修补:

instance = sqladmin.instances().patch(project=PROJECT_ID, instance=INSTANCE_ID)
instance.data = {"settings" : {"activationPolicy" : "NEVER"}} #also tried with it in a string, like this: instance.data = {"settings" : {"activationPolicy" : "NEVER"}}
instance.headers['Content-Type'] = 'application/json'
instance.execute()

考虑到 instance.data 在此之前不存在,但 instance.headers 存在:

{'accept': 'application/json', 'accept-encoding': 'gzip, deflate', 'user-agent': '(gzip)', 'x-goog-api-client': 'gdcl/1.7.11 gl-python/3.6.9'}

执行之后,似乎什么也没有发生。它没有改变实际的 activationPolicy。

最后,使用 ACCESS_TOKEN 并使用请求模块发出 Python 请求解决了问题。如果您在生成凭据后立即尝试从凭据中获取 ACCESS_TOKEN,您将不会获得任何凭据,但如果您实际将凭据与 googleapiclient.discovery 一起使用,这会使用有效访问更新该对象令牌,然后可以在 Python 请求中使用,如下所示:

from google.oauth2 import service_account
import googleapiclient.discovery
import json
import requests

PROJECT_ID = '{PROJECT_ID_HERE}'
INSTANCE_ID = '{INSTANCE_ID_HERE}'

SCOPES = ['https://www.googleapis.com/auth/sqlservice.admin']
SERVICE_ACCOUNT_FILE = '/path/to/service.json'
credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

sqladmin = googleapiclient.discovery.build('sqladmin', 'v1beta4', credentials=credentials)
response = sqladmin.instances().get(project=PROJECT_ID, instance=INSTANCE_ID).execute()
print(json.dumps(response))

access_token = credentials.token # Now that the credentials were used, they have a valid access_token associated with them!

activation_policy = 'NEVER' # or 'ALWAYS'

url = "https://www.googleapis.com/sql/v1beta4/projects/{PROJECT_ID}/instances/{INSTANCE_ID}".format(PROJECT_ID=PROJECT_ID, INSTANCE_ID=INSTANCE_ID)
header = {
    "Authorization" : "Bearer {}".format(access_token), 
    "Content-Type" : "application/json"
}
data = {
    "settings" : {
        "activationPolicy" : activation_policy
    }
}
response = requests.patch(url, headers=header, data=json.dumps(data))
print(response.text)

所要做的只是在尝试检索 ACCESS_TOKEN

之前实际使用凭证进行某些操作