GCP列出实例并按最后启动时间戳进行筛选。

huangapple go评论82阅读模式
英文:

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 &quot;cloud.google.com/go/compute/apiv1&quot;
	&quot;context&quot;
	&quot;fmt&quot;
	&quot;google.golang.org/api/iterator&quot;
	_ &quot;google.golang.org/api/option&quot;
	protobuf &quot;google.golang.org/genproto/googleapis/cloud/compute/v1&quot;
)

func List(ctx context.Context, project string) error {

	filter := &quot;lastStartTimestamp &lt; &#39;2021-09-10T00:00:00.000-07:00&#39;&quot;

	req := &amp;protobuf.AggregatedListInstancesRequest{
		Project: project,
		Filter:  &amp;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 &#39;filter&#39;: &#39;lastStartTimestamp &lt; &#39;2021-09-10T00:00:00.000-07:00&#39;&#39;.

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 &lt; &quot;2021-09-18T20:44:14.151-07:00&quot;`

答案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=&quot;2021-09-09T04:48:04.761-07:00&quot;

API can be tested here. Information on quotation in go, including escape characters, can be found here.

huangapple
  • 本文由 发表于 2021年9月18日 12:14:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/69231521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定