-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathtencentcloud_discover.go
134 lines (113 loc) · 4.15 KB
/
tencentcloud_discover.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
131
132
133
134
// Package tencentcloud provides node discovery for TencentCloud.
package tencentcloud
import (
"fmt"
"io"
"log"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
)
type Provider struct {
userAgent string
}
func (p *Provider) SetUserAgent(s string) {
p.userAgent = s
}
func (p *Provider) Help() string {
return `TencentCloud:
provider: "tencentcloud"
region: The TencentCloud region
tag_key: The tag key to filter on
tag_value: The tag value to filter on
address_type: "private_v4", "public_v4". (default: "private_v4")
access_key_id: The secret id of TencentCloud
access_key_secret: The secret key of TencentCloud
This required permission to 'cvm:DescribeInstances'.
It is recommended you make a dedicated key used only for auto-joining.
`
}
func (p *Provider) Addrs(args map[string]string, l *log.Logger) ([]string, error) {
if args["provider"] != "tencentcloud" {
return nil, fmt.Errorf("discover-tencentcloud: invalid provider " + args["provider"])
}
if l == nil {
l = log.New(io.Discard, "", 0)
}
region := args["region"]
tagKey := args["tag_key"]
tagValue := args["tag_value"]
addressType := args["address_type"]
accessKeyID := args["access_key_id"]
accessKeySecret := args["access_key_secret"]
l.Printf("[DEBUG] discover-tencentcloud: Using region=%s, tag_key=%s, tag_value=%s", region, tagKey, tagValue)
if accessKeyID == "" {
l.Printf("[DEBUG] discover-tencentcloud: No static credentials provided")
} else {
l.Printf("[DEBUG] discover-tencentcloud: Static credentials provided")
}
if region == "" {
l.Printf("[DEBUG] discover-tencentcloud: Region not provided")
return nil, fmt.Errorf("discover-tencentcloud: region missing")
}
l.Printf("[DEBUG] discover-tencentcloud: region is %s", region)
if addressType == "" {
addressType = "private_v4"
}
if addressType != "private_v4" && addressType != "public_v4" {
l.Printf("[DEBUG] discover-tencentcloud: Address type %s invalid", addressType)
return nil, fmt.Errorf("discover-tencentcloud: invalid address_type " + addressType)
}
l.Printf("[DEBUG] discover-tencentcloud: address type is %s", addressType)
credential := common.NewCredential(
accessKeyID,
accessKeySecret,
)
cpf := profile.NewClientProfile()
cpf.HttpProfile.ReqMethod = "POST"
cpf.HttpProfile.ReqTimeout = 300
cpf.Language = "en-US"
cvmClient, _ := cvm.NewClient(credential, region, cpf)
l.Printf("[DEBUG] discover-tencentcloud: Filter instances with %s=%s", tagKey, tagValue)
request := cvm.NewDescribeInstancesRequest()
request.Filters = []*cvm.Filter{
{
Name: stringToPointer("instance-state"),
Values: []*string{stringToPointer("RUNNING")},
},
{
Name: stringToPointer("tag:" + tagKey),
Values: []*string{stringToPointer(tagValue)},
},
}
response, err := cvmClient.DescribeInstances(request)
if err != nil {
l.Printf("[DEBUG] discover-tencentcloud: DescribeInstances failed, %s", err)
return nil, fmt.Errorf("discover-tencentcloud: DescribeInstances failed, %s", err)
}
l.Printf("[DEBUG] discover-tencentcloud: Found %d instances", len(response.Response.InstanceSet))
var addrs []string
for _, v := range response.Response.InstanceSet {
switch addressType {
case "public_v4":
if len(v.PublicIpAddresses) == 0 {
l.Printf("[DEBUG] discover-tencentcloud: Instance %s has no public_v4", *v.InstanceId)
continue
}
l.Printf("[DEBUG] discover-tencentcloud: Instance %s has public_v4 %v", *v.InstanceId, *v.PublicIpAddresses[0])
addrs = append(addrs, *v.PublicIpAddresses[0])
case "private_v4":
if len(v.PrivateIpAddresses) == 0 {
l.Printf("[DEBUG] discover-tencentcloud: Instance %s has no private_v4", *v.InstanceId)
continue
}
l.Printf("[DEBUG] discover-tencentcloud: Instance %s has private_v4 %v", *v.InstanceId, *v.PrivateIpAddresses[0])
addrs = append(addrs, *v.PrivateIpAddresses[0])
}
}
l.Printf("[DEBUG] discover-tencentcloud: Found address: %v", addrs)
return addrs, nil
}
func stringToPointer(s string) *string {
return &s
}