-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliyun_ecs.go
88 lines (72 loc) · 1.67 KB
/
aliyun_ecs.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
package aliyun
import (
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
)
type SearchECSInstanceArgs struct {
InstanceId string
InstanceName string
VPCName string
VSwitchName string
ZoneId string
NetworkType string
Tags []Tag
vpcId string
vswitchId string
}
func (p *Aliyun) FindECSInstance(arg *SearchECSInstanceArgs) (inst *ecs.Instance, err error) {
if len(arg.vpcId)+len(arg.vswitchId) == 0 {
if len(arg.VPCName) > 0 && len(arg.VSwitchName) > 0 {
var vSwitch *vpc.VSwitch
vSwitch, err = p.FindVSwitch(arg.VPCName, arg.VSwitchName)
if err != nil {
return
}
if vSwitch != nil {
arg.vpcId = vSwitch.VpcId
arg.vswitchId = vSwitch.VSwitchId
}
}
}
req := ecs.CreateDescribeInstancesRequest()
req.RegionId = p.Region
req.InstanceIds = arg.InstanceId
req.InstanceName = arg.InstanceName
req.InstanceNetworkType = arg.NetworkType
req.ZoneId = arg.ZoneId
req.VSwitchId = arg.vswitchId
req.VpcId = arg.vpcId
for i, tag := range arg.Tags {
switch i {
case 0:
req.Tag1Key = tag.Key
req.Tag1Value = tag.Value
case 1:
req.Tag2Key = tag.Key
req.Tag2Value = tag.Value
case 2:
req.Tag3Key = tag.Key
req.Tag3Value = tag.Value
case 3:
req.Tag4Key = tag.Key
req.Tag4Value = tag.Value
case 4:
req.Tag5Key = tag.Key
req.Tag5Value = tag.Value
}
}
resp, err := p.ECSClient().DescribeInstances(req)
if err != nil {
return
}
if len(resp.Instances.Instance) == 0 {
return
}
if len(resp.Instances.Instance) > 1 {
err = fmt.Errorf("find more than one instance")
return
}
inst = &resp.Instances.Instance[0]
return
}