95 lines
3.3 KiB
Go
95 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
|
|
clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
|
)
|
|
|
|
// ./TencentCloudClient -t b -o add -key AKIDSoxxjI5tukPr9ECsQmwthKhroaCZlldI1 -sec b1FG2b3JMrESVLxjJdhYDgx12qPiplXB1 -reg ap-shanghai -params "{\"LoadBalancerId\":\"lb-jip7505r\",\"Targets\":[{\"InstanceId\":\"ins-n2i7x483\",\"Weight\":20}]}"
|
|
// ./TencentCloudClient -t b -o remove -key AKIDSoxxjI5tukPr9ECsQmwthKhroaCZlldI1 -sec b1FG2b3JMrESVLxjJdhYDgx12qPiplXB1 -reg ap-shanghai -params "{\"LoadBalancerId\":\"lb-jip7505r\",\"InstanceIds\":[\"ins-n2i7x483\"]}"
|
|
func main03() {
|
|
|
|
var target string
|
|
flag.StringVar(&target, "t", "b", "target: b=backendServer")
|
|
var operation string
|
|
flag.StringVar(&operation, "o", "add", "operation: add、remove")
|
|
var accessKeyId string
|
|
flag.StringVar(&accessKeyId, "key", "secretId", "accessKeyId")
|
|
var accessSecret string
|
|
flag.StringVar(&accessSecret, "sec", "secretKey", "accessSecret")
|
|
var regionId string
|
|
flag.StringVar(®ionId, "reg", "regionId", "regionId: cn-hangzhou")
|
|
var params string
|
|
flag.StringVar(¶ms, "params", "params", `
|
|
AddBackendServers: [{"ServerId":"ServerId","Weight":"100","Type":"eni","ServerIp":"192.168.11.1"}]
|
|
RemoveBackendServers: [{"ServerId":"ServerId","Weight":"100"}]
|
|
`)
|
|
flag.Parse()
|
|
flag.Usage()
|
|
fmt.Printf("target=%s,operation=%s,accessKeyId=%s,accessSecret=%s,regionId=%s,params=%s\n", target, operation, accessKeyId, accessSecret, regionId, params)
|
|
if target == "b" {
|
|
switch operation {
|
|
case "add":
|
|
ClassicBind(regionId, accessKeyId, accessSecret, params)
|
|
break
|
|
case "remove":
|
|
ClassicRemove(regionId, accessKeyId, accessSecret, params)
|
|
break
|
|
default:
|
|
fmt.Println("operation: " + operation + " not support")
|
|
}
|
|
} else {
|
|
fmt.Println("target: " + target + " not support")
|
|
}
|
|
}
|
|
|
|
func ClassicBind(regionId, accessKeyId, accessSecret, params string) {
|
|
credential := common.NewCredential(accessKeyId, accessSecret)
|
|
cpf := profile.NewClientProfile()
|
|
cpf.HttpProfile.Endpoint = "clb.tencentcloudapi.com"
|
|
client, _ := clb.NewClient(credential, regionId, cpf)
|
|
|
|
request := clb.NewRegisterTargetsWithClassicalLBRequest()
|
|
|
|
err := request.FromJsonString(params)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
response, err := client.RegisterTargetsWithClassicalLB(request)
|
|
if _, ok := err.(*errors.TencentCloudSDKError); ok {
|
|
fmt.Printf("An API error has returned: %s", err)
|
|
return
|
|
}
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Printf("%s", response.ToJsonString())
|
|
}
|
|
|
|
func ClassicRemove(regionId, accessKeyId, accessSecret, params string) {
|
|
credential := common.NewCredential(accessKeyId, accessSecret)
|
|
cpf := profile.NewClientProfile()
|
|
cpf.HttpProfile.Endpoint = "clb.tencentcloudapi.com"
|
|
client, _ := clb.NewClient(credential, regionId, cpf)
|
|
|
|
request := clb.NewDeregisterTargetsFromClassicalLBRequest()
|
|
err := request.FromJsonString(params)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
response, err := client.DeregisterTargetsFromClassicalLB(request)
|
|
if _, ok := err.(*errors.TencentCloudSDKError); ok {
|
|
fmt.Printf("An API error has returned: %s", err)
|
|
return
|
|
}
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Printf("%s", response.ToJsonString())
|
|
}
|