Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add provider implementations of tencentcloud #125

Merged
merged 2 commits into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function.
* Openstack [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/os/os_discover.go#L23-L38)
* Scaleway [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/scaleway/scaleway_discover.go#L14-L22)
* SoftLayer [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/softlayer/softlayer_discover.go#L16-L25)
* TencentCloud [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/tencentcloud/tencentcloud_discover.go#L23-L37)
* Triton [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/triton/triton_discover.go#L17-L27)
* vSphere [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/vsphere/vsphere_discover.go#L148-L155)
* Packet [Config options](https://github.com/hashicorp/go-discover/blob/master/provider/packet/packet_discover.go#L25-L35)
Expand Down Expand Up @@ -78,6 +79,9 @@ provider=scaleway organization=my-org tag_name=consul-server token=... region=..
# SoftLayer
provider=softlayer datacenter=dal06 tag_value=consul username=... api_key=...

# TencentCloud
provider=tencentcloud region=ap-guangzhou tag_key=consul tag_value=... access_key_id=... access_key_secret=...

# Triton
provider=triton account=testaccount url=https://us-sw-1.api.joyentcloud.com key_id=... tag_key=consul-role tag_value=server

Expand Down
2 changes: 2 additions & 0 deletions discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hashicorp/go-discover/provider/packet"
"github.com/hashicorp/go-discover/provider/scaleway"
"github.com/hashicorp/go-discover/provider/softlayer"
"github.com/hashicorp/go-discover/provider/tencentcloud"
"github.com/hashicorp/go-discover/provider/triton"
"github.com/hashicorp/go-discover/provider/vsphere"
)
Expand Down Expand Up @@ -54,6 +55,7 @@ var Providers = map[string]Provider{
"os": &os.Provider{},
"scaleway": &scaleway.Provider{},
"softlayer": &softlayer.Provider{},
"tencentcloud": &tencentcloud.Provider{},
"triton": &triton.Provider{},
"vsphere": &vsphere.Provider{},
"packet": &packet.Provider{},
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/denverdino/aliyungo v0.0.0-20170926055100-d3308649c661
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/digitalocean/godo v1.1.1
github.com/dnaeon/go-vcr v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/gogo/protobuf v1.1.1 // indirect
Expand All @@ -31,6 +32,7 @@ require (
github.com/joyent/triton-go v0.0.0-20180628001255-830d2b111e62
github.com/json-iterator/go v1.1.5 // indirect
github.com/jtolds/gls v4.2.1+incompatible // indirect
github.com/likexian/gokit v0.20.16
github.com/linode/linodego v0.7.1
github.com/mitchellh/go-homedir v1.0.0
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand All @@ -50,6 +52,7 @@ require (
github.com/softlayer/softlayer-go v0.0.0-20180806151055-260589d94c7d
github.com/spf13/pflag v1.0.2 // indirect
github.com/stretchr/testify v1.2.2 // indirect
github.com/tencentcloud/tencentcloud-sdk-go v3.0.83+incompatible
github.com/tent/http-link-go v0.0.0-20130702225549-ac974c61c2f9 // indirect
github.com/vmware/govmomi v0.18.0
golang.org/x/oauth2 v0.0.0-20170807180024-9a379c6b3e95
Expand All @@ -67,4 +70,5 @@ require (
k8s.io/api v0.0.0-20180806132203-61b11ee65332
k8s.io/apimachinery v0.0.0-20180821005732-488889b0007f
k8s.io/client-go v8.0.0+incompatible
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect
)
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/digitalocean/godo v1.1.1 h1:v0A7yF3xmKLjjdJGIeBbINfMufcrrRhqZsxuVQMoT+U=
github.com/digitalocean/godo v1.1.1/go.mod h1:h6faOIcZ8lWIwNQ+DN7b3CgX4Kwby5T+nbpNqkUIozU=
github.com/dnaeon/go-vcr v1.0.1 h1:r8L/HqC0Hje5AXMu1ooW8oyQyOFv4GxqpL0nRP7SLLY=
github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
Expand Down Expand Up @@ -66,6 +68,14 @@ github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswD
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE=
github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/likexian/gokit v0.0.0-20190309162924-0a377eecf7aa/go.mod h1:QdfYv6y6qPA9pbBA2qXtoT8BMKha6UyNbxWGWl/9Jfk=
github.com/likexian/gokit v0.0.0-20190418170008-ace88ad0983b/go.mod h1:KKqSnk/VVSW8kEyO2vVCXoanzEutKdlBAPohmGXkxCk=
github.com/likexian/gokit v0.0.0-20190501133040-e77ea8b19cdc/go.mod h1:3kvONayqCaj+UgrRZGpgfXzHdMYCAO0KAt4/8n0L57Y=
github.com/likexian/gokit v0.20.16 h1:8ypmVXLx8yIvlTwzH8Ybz8LDAfWjdy0W5O354JWPjA4=
github.com/likexian/gokit v0.20.16/go.mod h1:kn+nTv3tqh6yhor9BC4Lfiu58SmH8NmQ2PmEl+uM6nU=
github.com/likexian/simplejson-go v0.0.0-20190409170913-40473a74d76d/go.mod h1:Typ1BfnATYtZ/+/shXfFYLrovhFyuKvzwrdOnIDHlmg=
github.com/likexian/simplejson-go v0.0.0-20190419151922-c1f9f0b4f084/go.mod h1:U4O1vIJvIKwbMZKUJ62lppfdvkCdVd2nfMimHK81eec=
github.com/likexian/simplejson-go v0.0.0-20190502021454-d8787b4bfa0b/go.mod h1:3BWwtmKP9cXWwYCr5bkoVDEfLywacOv0s06OBEDpyt8=
github.com/linode/linodego v0.7.1 h1:4WZmMpSA2NRwlPZcc0+4Gyn7rr99Evk9bnr0B3gXRKE=
github.com/linode/linodego v0.7.1/go.mod h1:ga11n3ivecUrPCHN0rANxKmfWBJVkOXfLMZinAbj2sY=
github.com/miekg/dns v1.0.14 h1:9jZdLNd/P4+SfEJ0TNyxYpsK8N4GtfylBLqtbYN1sbA=
Expand Down Expand Up @@ -106,6 +116,8 @@ github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/tencentcloud/tencentcloud-sdk-go v3.0.83+incompatible h1:8uRvJleFpqLsO77WaAh2UrasMOzd8MxXrNj20e7El+Q=
github.com/tencentcloud/tencentcloud-sdk-go v3.0.83+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4=
github.com/tent/http-link-go v0.0.0-20130702225549-ac974c61c2f9 h1:/Bsw4C+DEdqPjt8vAqaC9LAqpAQnaCQQqmolqq3S1T4=
github.com/tent/http-link-go v0.0.0-20130702225549-ac974c61c2f9/go.mod h1:RHkNRtSLfOK7qBTHaeSX1D6BNpI3qw7NTxsmNr4RvN8=
github.com/vmware/govmomi v0.18.0 h1:f7QxSmP7meCtoAmiKZogvVbLInT+CZx6Px6K5rYsJZo=
Expand All @@ -114,6 +126,8 @@ golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3 h1:KYQXGkl6vs02hK7pK4eIbw
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519 h1:x6rhz8Y9CjbgQkccRGmELH6K+LJj7tOoh3XWeC1yaQM=
golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3 h1:eH6Eip3UpmR+yM/qI9Ijluzb1bNv/cAU/n+6l8tRSis=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/oauth2 v0.0.0-20170807180024-9a379c6b3e95 h1:RS+wSrhdVci7CsPwJaMN8exaP3UTuQU0qB34R/E/JD0=
golang.org/x/oauth2 v0.0.0-20170807180024-9a379c6b3e95/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
Expand Down Expand Up @@ -150,3 +164,5 @@ k8s.io/apimachinery v0.0.0-20180821005732-488889b0007f h1:V0PkbgaYp5JqCmzLyRmssD
k8s.io/apimachinery v0.0.0-20180821005732-488889b0007f/go.mod h1:ccL7Eh7zubPUSh9A3USN90/OzHNSVN6zxzde07TDCL0=
k8s.io/client-go v8.0.0+incompatible h1:tTI4hRmb1DRMl4fG6Vclfdi6nTM82oIrTT7HfitmxC4=
k8s.io/client-go v8.0.0+incompatible/go.mod h1:7vJpHMYJwNQCWgzmNV+VYUl1zCObLyodBc8nIyt8L5s=
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 h1:Izowp2XBH6Ya6rv+hqbceQyw/gSGoXfH/UPoTGduL54=
launchpad.net/gocheck v0.0.0-20140225173054-000000000087/go.mod h1:hj7XX3B/0A+80Vse0e+BUHsHMTEhd0O4cpUHr/e/BUM=
138 changes: 138 additions & 0 deletions provider/tencentcloud/tencentcloud_discover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Package tencentcloud provides node discovery for TencentCloud.
package tencentcloud

import (
"fmt"
"io/ioutil"
"log"

"github.com/likexian/gokit/assert"
"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(ioutil.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 !assert.IsContains([]string{"private_v4", "public_v4"}, addressType) {
likexian marked this conversation as resolved.
Show resolved Hide resolved
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)
if p.userAgent != "" {
}
likexian marked this conversation as resolved.
Show resolved Hide resolved

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
}
37 changes: 37 additions & 0 deletions provider/tencentcloud/tencentcloud_discover_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package tencentcloud_test

import (
"log"
"os"
"testing"

discover "github.com/hashicorp/go-discover"
"github.com/hashicorp/go-discover/provider/tencentcloud"
"github.com/likexian/gokit/assert"
)

var _ discover.Provider = (*tencentcloud.Provider)(nil)
var _ discover.ProviderWithUserAgent = (*tencentcloud.Provider)(nil)

func TestAddrs(t *testing.T) {
args := discover.Config{
"provider": "tencentcloud",
"access_key_id": os.Getenv("TENCENTCLOUD_SECRET_ID"),
"access_key_secret": os.Getenv("TENCENTCLOUD_SECRET_KEY"),
"region": os.Getenv("TENCENTCLOUD_REGION"),
"tag_key": "consul",
"tag_value": "test",
"address_type": "private_v4",
}

if args["access_key_id"] == "" || args["access_key_secret"] == "" || args["region"] == "" {
t.Skip("TencentCloud credentials or region info missing")
}

p := &tencentcloud.Provider{}
l := log.New(os.Stderr, "", log.LstdFlags)

addrs, err := p.Addrs(args, l)
assert.Nil(t, err)
likexian marked this conversation as resolved.
Show resolved Hide resolved
assert.Len(t, addrs, 2)
}
Loading