Skip to content

Commit

Permalink
add clb
Browse files Browse the repository at this point in the history
  • Loading branch information
deancn committed Sep 5, 2020
1 parent e16808c commit b436c2b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
13 changes: 12 additions & 1 deletion cmd/bbhj.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/airdb/adb/internal/adblib"

"github.com/spf13/cobra"
)
Expand All @@ -12,6 +13,16 @@ var bbhjCommand = &cobra.Command{
Long: "bbhj operation",
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("bbhj operation")
bbhj()
},
}

func bbhj() {
client, err := adblib.NewCLBClient()
if err != nil {
fmt.Println(err)
return
}

client.ListCLB()
}
2 changes: 1 addition & 1 deletion cmd/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var mysqlCmd = &cobra.Command{
Long: "Airdb mysql client",
DisableFlagParsing: false,
Args: cobra.MinimumNArgs(1),
Example:adblib.SqlDoc,
Example: adblib.SqlDoc,
Aliases: []string{"sql"},
Run: func(cmd *cobra.Command, args []string) {
mysql(args)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ require (
github.com/slack-go/slack v0.6.5
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.4.0
github.com/tencentcloud/tencentcloud-sdk-go v1.0.12
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/swaggo/gin-swagger v1.2.0/go.mod h1:qlH2+W7zXGZkczuL+r2nEBR2JTT+/lX05Nn6vPhc7OI=
github.com/swaggo/swag v1.5.1/go.mod h1:1Bl9F/ZBpVWh22nY0zmYyASPO1lI/zIwRDrpZU+tv8Y=
github.com/tencentcloud/tencentcloud-sdk-go v1.0.12 h1:ABgTcYxBePz5rlueSYpwFHImNq2NCnrjmx21jIXLbyM=
github.com/tencentcloud/tencentcloud-sdk-go v1.0.12/go.mod h1:asUz5BPXxgoPGaRgZaVm1iGcUAuHyYUo1nXqKa83cvI=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go v1.1.5-pre/go.mod h1:FwP/aQVg39TXzItUBMwnWp9T9gPQnXw4Poh4/oBQZ/0=
Expand Down
90 changes: 90 additions & 0 deletions internal/adblib/tencentcloud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package adblib

import (
"fmt"
"os"

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"
)

const CLBEndpoint = "clb.tencentcloudapi.com"

type Client struct {
*clb.Client
}

func NewCLBClient() (*Client, error){
secretID := os.Getenv("TENCENTCLOUD_SECRET_ID")
secretKey := os.Getenv("TENCENTCLOUD_SECRET_KEY")
region := os.Getenv("TENCENTCLOUD_REGION")

credential := common.NewCredential(secretID, secretKey)

cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = CLBEndpoint

client, err := clb.NewClient(credential, region, cpf)

return &Client{
client,
}, err
}

func (client *Client) ListCLB() {
request := clb.NewDescribeLoadBalancersRequest()

params := "{}"
err := request.FromJsonString(params)
if err != nil {
panic(err)
}
response, err := client.DescribeLoadBalancers(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return
}
if err != nil {
panic(err)
}

for _, lbSet := range response.Response.LoadBalancerSet {
// fmt.Println(*lbSet.LoadBalancerId)
client.ShowRS(*lbSet.LoadBalancerId)
}
}

func (client *Client)ShowRS(lbID string) {

request := clb.NewDescribeTargetsRequest()

params := fmt.Sprintf("{\"LoadBalancerId\":\"%s\"}", lbID)
err := request.FromJsonString(params)
if err != nil {
panic(err)
}
response, err := client.DescribeTargets(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())
for _, listener := range response.Response.Listeners {
for _, rule := range listener.Rules {
for _, target := range rule.Targets {
fmt.Printf("%s%s\t%d\t%s\n",
*rule.Domain,
*rule.Url,
*target.Weight,
*target.InstanceName,
)
}
}
}
}

0 comments on commit b436c2b

Please sign in to comment.