Google API 使用过滤器获取快照,
Google API to get Snapshot using filter,
我写了下面的代码,其中 return 我的快照列表,它有特定的字符串,但这不是 return 大小为 0 的快照,
https://cloud.google.com/compute/docs/reference/rest/v1/snapshots/list
this API have following attributes:
- project_id
- 过滤器
I am trying to put some filter based on regular expression which will
match all snapshot which have "intance-snap" in there name.
def snapshotlist():
query = "name eq <string>.*"
snaplist = compute.snapshots().list(project=project,filter=query).execute()
snaplist = ast.literal_eval(json.dumps(snaplist))
for snap in snaplist['items']:
print(snap['name'])
以上代码没有 return 大小为 0 的快照,有没有办法获取所有快照,而不考虑 SIZE?
来自 Snapshot documentation,对于可选参数的 filter
属性:
The expression must specify the field name, a comparison operator, and the value that you want to use for filtering. The value must be a string, a number, or a boolean. The comparison operator must be either =
, !=
, >
, or <
.
虽然这意味着您无法执行与驱动器 API 类似的查询,例如q: "name contains 'some string'"
,Snapshots#list
方法似乎仍然支持通配符匹配。如果您无法成功编写适当的正则表达式,那么解决方案是收集您的 整个 列表 Snapshot
,然后使用您的语言的字符串和正则表达式方法来过滤输出。
def get_matching_snapshots(projectId: str, query='',
fields='id,nextPageToken,warning,items(id,name,description)'):
snaplist = []
params = {project: projectId,
filter: query,
fields: fields}
# Collect all pages of results
request = compute.snapshots().list(**params)
while request:
response = request.execute()
if 'items' in response:
snaplist.extend(response['items'])
if 'warning' in response:
pprint(response['warning'])
request = compute.snapshots().list_next(request, response)
return snaplist
# Use 'filter' parameter to restrict on server side:
name_fragment = "some required part of the name"
q = f'name = "{name_fragment}.*"'
pprint(get_matching_snapshots("some project id", q))
# get all, then apply restrictions after retrieval:
all_snapshots = get_matching_snapshots("some project id")
matches = []
rgx = re.compile('some regex')
for snap in all_snapshots:
# use rgx.match with the relevant snap property
...
您可能会发现查阅计算 API 的 Python 文档或使用 Python 客户端的 API Explorer:
会有所帮助
我写了下面的代码,其中 return 我的快照列表,它有特定的字符串,但这不是 return 大小为 0 的快照,
https://cloud.google.com/compute/docs/reference/rest/v1/snapshots/list
this API have following attributes:
- project_id
- 过滤器
I am trying to put some filter based on regular expression which will match all snapshot which have "intance-snap" in there name.
def snapshotlist():
query = "name eq <string>.*"
snaplist = compute.snapshots().list(project=project,filter=query).execute()
snaplist = ast.literal_eval(json.dumps(snaplist))
for snap in snaplist['items']:
print(snap['name'])
以上代码没有 return 大小为 0 的快照,有没有办法获取所有快照,而不考虑 SIZE?
来自 Snapshot documentation,对于可选参数的 filter
属性:
The expression must specify the field name, a comparison operator, and the value that you want to use for filtering. The value must be a string, a number, or a boolean. The comparison operator must be either
=
,!=
,>
, or<
.
虽然这意味着您无法执行与驱动器 API 类似的查询,例如q: "name contains 'some string'"
,Snapshots#list
方法似乎仍然支持通配符匹配。如果您无法成功编写适当的正则表达式,那么解决方案是收集您的 整个 列表 Snapshot
,然后使用您的语言的字符串和正则表达式方法来过滤输出。
def get_matching_snapshots(projectId: str, query='',
fields='id,nextPageToken,warning,items(id,name,description)'):
snaplist = []
params = {project: projectId,
filter: query,
fields: fields}
# Collect all pages of results
request = compute.snapshots().list(**params)
while request:
response = request.execute()
if 'items' in response:
snaplist.extend(response['items'])
if 'warning' in response:
pprint(response['warning'])
request = compute.snapshots().list_next(request, response)
return snaplist
# Use 'filter' parameter to restrict on server side:
name_fragment = "some required part of the name"
q = f'name = "{name_fragment}.*"'
pprint(get_matching_snapshots("some project id", q))
# get all, then apply restrictions after retrieval:
all_snapshots = get_matching_snapshots("some project id")
matches = []
rgx = re.compile('some regex')
for snap in all_snapshots:
# use rgx.match with the relevant snap property
...
您可能会发现查阅计算 API 的 Python 文档或使用 Python 客户端的 API Explorer:
会有所帮助