简体   繁体   English

如何使用 Python 使用“Cloud SQL Admin API”获取新创建的备份的 ID?

[英]How to get the ID of newly created backup using "Cloud SQL Admin API" with Python?

I am writing a script using Cloud SQL Admin API 's backupRuns instance which has the following methods:我正在使用Cloud SQL Admin APIbackupRuns实例编写脚本,该实例具有以下方法:

1. delete(project=*, instance=*, id=*)
2. get(project=*, instance=*, id=*)
3. insert(project=*, instance=*, body=*)
4. list(project=*, instance=*, maxResults=None, pageToken=None) 
5. list_next(previous_request=*, previous_response=*)

Here's the link to the API: https://developers.google.com/resources/api-libraries/documentation/sqladmin/v1beta4/python/latest/sqladmin_v1beta4.backupRuns.html这是 API 的链接: https ://developers.google.com/resources/api-libraries/documentation/sqladmin/v1beta4/python/latest/sqladmin_v1beta4.backupRuns.html

What I find strange in the API is the insert() method, when we insert/create a new backup calling this method of the API, it returns the following response:我在 API 中发现奇怪的是insert()方法,当我们插入/创建一个新的备份调用 API 的这个方法时,它返回以下响应:

{'insertTime': '2018-12-26T06:48:35.675Z',
 'kind': 'sql#operation',
 'name': 'some-random-string,
 'operationType': 'BACKUP_VOLUME',
 'selfLink': 'https://www.googleapis.com/sql/v1beta4/projects/project-name/operations/some-random-string',
 'status': 'PENDING',
 'targetId': 'cloud-instance-name',
 'targetLink': 'https://www.googleapis.com/sql/v1beta4/projects/project-name/instances/cloud-instance-name',
 'targetProject': 'project-name',
 'user': 'some-user@project.iam.gserviceaccount.com'}

This seems like an asynchronous call in which status changes from PENDING to SUCCESSFUL after a few seconds.这看起来像是一个异步调用,其中状态在几秒钟后从PENDING变为SUCCESSFUL Now if I want to keep on checking the instance until its status is SUCCESSFUL , I'll need the _id_ of the newly created instance (to call the get() method).现在,如果我想继续检查实例直到其状态为SUCCESSFUL ,我将需要新创建实例的_id_ (以调用get()方法)。

The only way I could figure out is calling the list() method and checking the enqueuedTime of the list items with the insertTime of the response above and get the id, then call the get() method for the status.我能弄清楚的唯一方法是调用list()方法并使用上面响应的insertTime检查列表项的enqueuedTime并获取 id,然后调用get()方法获取状态。 It feels like a hack to me, is there a better way to watch for the status until it is SUCCESSFUL ?对我来说感觉像是一个黑客,有没有更好的方法来观察状态直到它成功

I found this API also confusing.我发现这个 API 也很混乱。 The insert method returns a sql#operation object and it has a GUID-type id -field. insert方法返回一个sql#operation对象,它有一个 GUID 类型的id字段。 The list and get methods use sql#backupRun object and they have id with datatype long. listget方法使用sql#backupRun对象,它们的id数据类型为 long。 I have not found any way to map these.我还没有找到任何方法来映射这些。

You could use the description field to identify your backupRun.您可以使用description字段来标识您的 backupRun。 It is not optimal, but could work for you.它不是最佳的,但可以为你工作。 Specify a unique value in description field in your insert method body parameter.在插入方法主体参数的description字段中指定一个唯一值。 You can then filter the list method result with the description and possibly also filter type='ON_DEMAND' in addition to the enqueuedTime.然后,您可以使用描述过滤列表方法结果,除了 enqueuedTime 之外,还可能过滤 type='ON_DEMAND'。 The list result is in reverse chronological order so you should find the right item in the beginning of the list.列表结果按时间倒序排列,因此您应该在列表的开头找到正确的项目。

There is an id parameter in documentation for insert methods body parameter but setting it will raise an error. insert方法body参数的文档中有一个id参数,但设置它会引发错误。 The API is still in beta.该 API 仍处于测试阶段。 Hope this API matures and changes so that we can have linking from inserts to get.希望这个 API 成熟和变化,以便我们可以从插入到获取链接。

Google API treats every request as an operation which can be retrieved from the operations() of service built from googleapiclient.discovery . Google API 将每个请求都视为一个操作,可以从googleapiclient.discovery构建的服务的operations()中检索到该操作。 For example:例如:

from googleapiclient import discovery

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

#Just insert a backup for an SQL instance or any other operation
insert_response = service.backupRuns().insert(project=<project-id>,instance=<instance-id>, body={}).execute()

#Get the opepration to check the status
insert_operation = service.operations().get(project=<project-id>,operation=insert_response['name']).execute()

This insert_opertation can be used to check the current status of the operation.insert_opertation可用于检查操作的当前状态。

Here how insert_response and insert_operation look:下面是insert_responseinsert_operation样子:

print(insert_response)
{'insertTime': '2019-01-08T13:04:31.941Z',                                                                                                                              
 'kind': 'sql#operation',                                                                                                                                               
 'name': '<unique-name-of-the-operation>',                                                                                                                        
 'operationType': 'BACKUP_VOLUME',                                                                                                                                      
 'selfLink': 'https://www.googleapis.com/sql/v1beta4/projects/<project-id>/operations/<unique-name-of-the-operation>',                                                
 'startTime': '2019-01-08T13:04:32.052Z',                                                                                                                               
 'status': 'RUNNING',                                                                                                                                                   
 'targetId': '<instance-name>',                                                                                                                                   
 'targetLink': 'https://www.googleapis.com/sql/v1beta4/projects/<project-id>/instances/<instance-name>',                                                              
 'targetProject': '<project-id>',                                                                                                                                           
 'user': '<user>'}

print(insert_operation)
{'endTime': '2018-12-26T13:07:08.746Z',
'enqueuedTime': '2018-12-26T13:06:33.563Z',
'id': '<operation-id>',
'instance': '<instance-name>',
'kind': 'sql#backupRun',
'selfLink': 'https://www.googleapis.com/sql/v1beta4/projects/<project-id>/instances/<instance-name>/backupRuns/<operation-id>',
'startTime': '2018-12-26T13:06:33.563Z',
'status': 'SUCCESSFUL',
'type': 'ON_DEMAND',
'windowStartTime': '2018-12-26T13:06:33.563Z'}

service.operations().get() can be used to get any operation performed using the APIs which return a name in the response. service.operations().get()可用于获取使用在响应中返回name的 API 执行的任何操作。

Refer to this link for more information.有关详细信息,请参阅此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Terraform 获取新创建的实例 ID - How to get newly created instance id using Terraform 如何在 Firestore 中的交易期间获取新创建文档的 ID? - How to get the ID of a newly created document during a Transaction in Firestore? ReactJS/Firestore:如何获取新建文档的 id - ReactJS/Firestore: how to get the id of a newly created document Cloud SQL Admin Api GCF 警告 - Cloud SQL Admin Api Warning in GCF 如何使用 Python 连接到云 SQL? - How to Connect to Cloud SQL using Python? 如何以编程方式在 Google 云运行 api 中获取当前项目 ID - How to programmatically get current project id in Google cloud run api 如何为使用云形成创建的 SNS 主题获取 ARN? - How to get ARN for SNS Topic created using cloud formation? 如何列出所有应用程序以使用 python API 在谷歌云平台的应用程序引擎上获取 appID - How to list all apps to get appID on app engine in google cloud platform using python API 如何导出 Google Storage 中 Cloud SQL 数据库的备份? - How to export backup of Cloud SQL database in Google Storage? 如何获取新创建的 aws_cloudformation_stack 的 arn terraform - How to get the arn of a newly created aws_cloudformation_stack terraform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM