-
Notifications
You must be signed in to change notification settings - Fork 12
/
instances.go
130 lines (123 loc) · 3.59 KB
/
instances.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package ecs
import (
"context"
"fmt"
"math"
"github.com/404tk/cloudtoolkit/pkg/schema"
"github.com/404tk/cloudtoolkit/utils/logger"
"github.com/404tk/cloudtoolkit/utils/processbar"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
)
type Driver struct {
Cred *credentials.StsTokenCredential
Region string
}
func (d *Driver) NewClient() (*ecs.Client, error) {
region := d.Region
if region == "all" || region == "" {
region = "cn-hangzhou"
}
return ecs.NewClientWithOptions(region, sdk.NewConfig(), d.Cred)
}
// GetResource returns all the resources in the store for a provider.
func (d *Driver) GetResource(ctx context.Context) ([]schema.Host, error) {
list := []schema.Host{}
logger.Info("Start enumerating ECS ...")
defer func() { CacheHostList = list }()
client, err := d.NewClient()
if err != nil {
return list, err
}
// check permission
vpc_client, _ := vpc.NewClientWithOptions("cn-hangzhou", sdk.NewConfig(), d.Cred)
req_vpc := vpc.CreateDescribeVpcsRequest()
_, err = vpc_client.DescribeVpcs(req_vpc)
if err != nil {
logger.Error("Describe vpcs failed.")
return list, err
}
var regions []string
if d.Region == "all" {
req := ecs.CreateDescribeRegionsRequest()
resp, err := client.DescribeRegions(req)
if err != nil {
logger.Error("Describe regions failed.")
return list, err
}
for _, r := range resp.Regions.Region {
regions = append(regions, r.RegionId)
}
} else {
regions = append(regions, d.Region)
}
flag := false
prevLength := 0
count := 0
for _, r := range regions {
page := 1
for {
request := ecs.CreateDescribeInstancesRequest()
request.PageSize = requests.NewInteger(100)
request.PageNumber = requests.NewInteger(page)
request.RegionId = r
// Getting a list of instances
response, err := client.DescribeInstances(request)
if err != nil {
break
}
pageCount := int(math.Ceil(float64(response.TotalCount) / 100))
for _, instance := range response.Instances.Instance {
// Getting Host Information
var ipv4, privateIPv4 string
if len(instance.PublicIpAddress.IpAddress) > 0 {
ipv4 = instance.PublicIpAddress.IpAddress[0]
}
if len(instance.NetworkInterfaces.NetworkInterface) > 0 && len(instance.NetworkInterfaces.NetworkInterface[0].PrivateIpSets.PrivateIpSet) > 0 {
privateIPv4 = instance.NetworkInterfaces.NetworkInterface[0].PrivateIpSets.PrivateIpSet[0].PrivateIpAddress
}
if privateIPv4 == "" {
// Get the primary and private IP addresses from the network adapter configuration
for _, net := range instance.NetworkInterfaces.NetworkInterface {
if net.PrimaryIpAddress != "" {
privateIPv4 = net.PrimaryIpAddress
}
}
}
if ipv4 == "" {
// Get the public IP address from the Eip
ipv4 = instance.EipAddress.IpAddress
}
_host := schema.Host{
HostName: instance.HostName,
ID: instance.InstanceId,
PublicIPv4: ipv4,
PrivateIpv4: privateIPv4,
OSType: instance.OSType, // windows or linux
Public: ipv4 != "",
Region: r,
}
list = append(list, _host)
}
if page == pageCount || pageCount == 0 {
break
}
page++
}
select {
case <-ctx.Done():
goto done
default:
prevLength, flag = processbar.RegionPrint(r, len(list)-count, prevLength, flag)
count = len(list)
}
}
done:
if !flag {
fmt.Printf("\n\033[F\033[K")
}
return list, nil
}