-
Notifications
You must be signed in to change notification settings - Fork 504
/
instances_fetch.go
45 lines (41 loc) · 1.1 KB
/
instances_fetch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package ecs
import (
"context"
"fmt"
"net/http"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/cloudquery/cloudquery/plugins/source/alicloud/client"
"github.com/cloudquery/plugin-sdk/v4/schema"
)
var (
maxLimit = 100
)
func fetchEcsInstances(_ context.Context, meta schema.ClientMeta, _ *schema.Resource, res chan<- any) error {
c := meta.(*client.Client)
pageNum := 1
total := 0
req := ecs.CreateDescribeInstancesRequest()
req.PageNumber = requests.NewInteger(pageNum)
req.PageSize = requests.NewInteger(maxLimit)
for {
resp, err := c.Services().ECS.DescribeInstances(req)
if err != nil {
return err
}
if !resp.IsSuccess() {
code := resp.GetHttpStatus()
return fmt.Errorf("got response status code %d (%v)", code, http.StatusText(code))
}
for _, instance := range resp.Instances.Instance {
res <- instance
}
total += len(resp.Instances.Instance)
if len(resp.Instances.Instance) == 0 || total >= resp.TotalCount {
break
}
pageNum++
req.PageNumber = requests.NewInteger(pageNum)
}
return nil
}