英文:
GCP list instances and filter by last start timestamp
问题
我正在尝试列出lastStartTimestamp
小于给定日期的实例。
import (
compute "cloud.google.com/go/compute/apiv1"
"context"
"fmt"
"google.golang.org/api/iterator"
_ "google.golang.org/api/option"
protobuf "google.golang.org/genproto/googleapis/cloud/compute/v1"
)
func List(ctx context.Context, project string) error {
filter := "lastStartTimestamp < '2021-09-10T00:00:00.000-07:00'"
req := &protobuf.AggregatedListInstancesRequest{
Project: project,
Filter: &filter,
}
it := c.client.AggregatedList(ctx, req)
for {
resp, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return err
}
for _, instance := range resp.Value.Instances {
fmt.Println(instance.GetName(), instance.GetLastStartTimestamp())
}
}
return nil
}
然而,它抛出了一个错误
Error 400: Invalid value for field 'filter': 'lastStartTimestamp < '2021-09-10T00:00:00.000-07:00''.
这种方式可行吗?或者我需要在查询后分析lastStartTimestamp
吗?
我尝试过的是
filter := `lastStartTimestamp < "2021-09-18T20:44:14.151-07:00"`
英文:
I'm trying to list instances with a lastStartTimestamp
less than a given date.
import (
compute "cloud.google.com/go/compute/apiv1"
"context"
"fmt"
"google.golang.org/api/iterator"
_ "google.golang.org/api/option"
protobuf "google.golang.org/genproto/googleapis/cloud/compute/v1"
)
func List(ctx context.Context, project string) error {
filter := "lastStartTimestamp < '2021-09-10T00:00:00.000-07:00'"
req := &protobuf.AggregatedListInstancesRequest{
Project: project,
Filter: &filter,
}
it := c.client.AggregatedList(ctx, req)
for {
resp, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return err
}
for _, instance := range resp.Value.Instances {
fmt.Println(instance.GetName(), instance.GetLastStartTimestamp())
}
}
return nil
}
However it's throwing an error
Error 400: Invalid value for field 'filter': 'lastStartTimestamp < '2021-09-10T00:00:00.000-07:00''.
Is this possible and am I going about it the right way? Or do I need to analyse the lastStartTimestamp
post query?
What I've tried
filter := `lastStartTimestamp < "2021-09-18T20:44:14.151-07:00"`
答案1
得分: 1
回顾一下评论:
目前只支持“相等”比较,无法筛选在特定时间戳之后或之前启动的实例。作为一种解决方法,我建议先拉取一个完整的列表,按creationTimestamp desc
排序,然后在代码中筛选实例。
时间戳必须用双引号括起来:
lastStartTimestamp="2021-09-09T04:48:04.761-07:00"
可以在这里测试API。关于在Go语言中使用引号的信息,包括转义字符,可以在这里找到。
英文:
To recap the comments:
Currently only 'equal' comparison is supported, you cannot filter instances started after or before certain timestamp. As a workaround I would suggest pulling a complete list, sorted by creationTimestamp desc
, and then filtering instances in the code.
Timestamp must be in double quotation marks:
lastStartTimestamp="2021-09-09T04:48:04.761-07:00"
API can be tested here. Information on quotation in go, including escape characters, can be found here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论