如何获取在 python 中停止的所有云 SQL ( GCP ) 实例的列表,为此我正在使用 google 云 api
How can I get list of all cloud SQL ( GCP ) instances which are stopped in python, I am using google cloud api for this purpose
from googleapiclient import discovery
PROJECT = gcp-test-1234
sql_client = discovery.build('sqladmin', 'v1beta4')
resp = sql_client.instances().list(project=PROJECT).execute()
print(resp)
但作为回应,我得到的已停止实例的状态为“RUNNABLE”,因此我如何验证该实例是 运行 还是以编程方式停止
我也检查了 gcloud sql instances describe gcp-test-1234-test-db
,它提供的状态为“已停止”
如何使用 python
以编程方式实现此目的
在 Rest API 中,state 字段的 RUNNABLE 表示实例正在 运行ning,或者已被所有者停止,如 here 所述。
您需要从 activationPolicy 字段中读取,其中 ALWAYS
表示您的实例正在 运行ning,NEVER
表示它已停止。像下面这样的东西会起作用:
from pprint import pprint
from googleapiclient import discovery
service = discovery.build('sqladmin', 'v1beta4')
project = 'gcp-test-1234'
instance = 'gcp-test-1234-test-db'
request = service.instances().get(project=project,instance=instance)
response = request.execute()
pprint(response['settings']['activationPolicy'])
另一种选择是直接从您的 python 文件使用 Cloud SDK 命令:
import os
os.system("gcloud sql instances describe gcp-test-1234-test-db | grep state | awk {'print '}")
或子流程:
import subprocess
subprocess.run("gcloud sql instances describe gcp-test-1234-test-db | grep state | awk {'print '}", shell=True)
请注意,当您在停止的实例上 运行 gcloud sql instances describe you-instance --log-http
时,在 API 的响应中,您会看到 "state": "RUNNABLE"
,但是,gcloud
命令将显示状态 STOPPED
。这是因为命令的输出从 API 响应的 activationPolicy 获取状态而不是状态,如果状态是 RUNNABLE
.
如果你想查看将activationPolicy转换为状态的那段代码,你可以在SDK中看到它。 gcloud工具写在python:
cat $(gcloud info --format "value(config.paths.sdk_root)")/lib/googlecloudsdk/api_lib/sql/instances.py|grep "class DatabaseInstancePresentation(object)" -A 17
您将看到以下内容:
class DatabaseInstancePresentation(object):
"""Represents a DatabaseInstance message that is modified for user visibility."""
def __init__(self, orig):
for field in orig.all_fields():
if field.name == 'state':
if orig.settings and orig.settings.activationPolicy == messages.Settings.ActivationPolicyValueValuesEnum.NEVER:
self.state = 'STOPPED'
else:
self.state = orig.state
else:
value = getattr(orig, field.name)
if value is not None and not (isinstance(value, list) and not value):
if field.name in ['currentDiskSize', 'maxDiskSize']:
setattr(self, field.name, six.text_type(value))
else:
setattr(self, field.name, value)
from googleapiclient import discovery
PROJECT = gcp-test-1234
sql_client = discovery.build('sqladmin', 'v1beta4')
resp = sql_client.instances().list(project=PROJECT).execute()
print(resp)
但作为回应,我得到的已停止实例的状态为“RUNNABLE”,因此我如何验证该实例是 运行 还是以编程方式停止
我也检查了 gcloud sql instances describe gcp-test-1234-test-db
,它提供的状态为“已停止”
如何使用 python
以编程方式实现此目的在 Rest API 中,state 字段的 RUNNABLE 表示实例正在 运行ning,或者已被所有者停止,如 here 所述。
您需要从 activationPolicy 字段中读取,其中 ALWAYS
表示您的实例正在 运行ning,NEVER
表示它已停止。像下面这样的东西会起作用:
from pprint import pprint
from googleapiclient import discovery
service = discovery.build('sqladmin', 'v1beta4')
project = 'gcp-test-1234'
instance = 'gcp-test-1234-test-db'
request = service.instances().get(project=project,instance=instance)
response = request.execute()
pprint(response['settings']['activationPolicy'])
另一种选择是直接从您的 python 文件使用 Cloud SDK 命令:
import os
os.system("gcloud sql instances describe gcp-test-1234-test-db | grep state | awk {'print '}")
或子流程:
import subprocess
subprocess.run("gcloud sql instances describe gcp-test-1234-test-db | grep state | awk {'print '}", shell=True)
请注意,当您在停止的实例上 运行 gcloud sql instances describe you-instance --log-http
时,在 API 的响应中,您会看到 "state": "RUNNABLE"
,但是,gcloud
命令将显示状态 STOPPED
。这是因为命令的输出从 API 响应的 activationPolicy 获取状态而不是状态,如果状态是 RUNNABLE
.
如果你想查看将activationPolicy转换为状态的那段代码,你可以在SDK中看到它。 gcloud工具写在python:
cat $(gcloud info --format "value(config.paths.sdk_root)")/lib/googlecloudsdk/api_lib/sql/instances.py|grep "class DatabaseInstancePresentation(object)" -A 17
您将看到以下内容:
class DatabaseInstancePresentation(object):
"""Represents a DatabaseInstance message that is modified for user visibility."""
def __init__(self, orig):
for field in orig.all_fields():
if field.name == 'state':
if orig.settings and orig.settings.activationPolicy == messages.Settings.ActivationPolicyValueValuesEnum.NEVER:
self.state = 'STOPPED'
else:
self.state = orig.state
else:
value = getattr(orig, field.name)
if value is not None and not (isinstance(value, list) and not value):
if field.name in ['currentDiskSize', 'maxDiskSize']:
setattr(self, field.name, six.text_type(value))
else:
setattr(self, field.name, value)