diff --git a/cmd/lb.go b/cmd/lb.go new file mode 100644 index 0000000..07cf491 --- /dev/null +++ b/cmd/lb.go @@ -0,0 +1,40 @@ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" + "github.com/tangx/qingclix/global" + "github.com/tangx/qingclix/modules" +) + +// certCmd represents the cert command +var lbCmd = &cobra.Command{ + Use: "lb", + Short: "负载均衡", + Run: func(cmd *cobra.Command, args []string) { + // fmt.Println("cert called") + _ = cmd.Help() + }, +} + +func init() { + rootCmd.AddCommand(lbCmd) +} + +var lbCmdUpdate = &cobra.Command{ + Use: "update", + Short: "更新负载均衡配置", + Run: func(cmd *cobra.Command, args []string) { + if global.Loadbalancers == "" { + _ = cmd.Help() + os.Exit(1) + } + modules.UpdateLoadBalancers(global.Loadbalancers) + }, +} + +func init() { + lbCmd.AddCommand(lbCmdUpdate) + lbCmdUpdate.Flags().StringVarP(&global.Loadbalancers, "lb", "", "", "Loadbalances , split with comma (ex lb-123,lb-223)") +} diff --git a/global/config.go b/global/config.go index e45e688..a6b059c 100644 --- a/global/config.go +++ b/global/config.go @@ -40,3 +40,7 @@ var ( var ( WithUpdateLB bool ) + +var ( + Loadbalancers string +) diff --git a/modules/lb.go b/modules/lb.go new file mode 100644 index 0000000..7be8e78 --- /dev/null +++ b/modules/lb.go @@ -0,0 +1,27 @@ +package modules + +import ( + "strings" + + "github.com/sirupsen/logrus" + "github.com/tangx/qingclix/global" + "github.com/tangx/qingyun-sdk-go/qingyun" +) + +func UpdateLoadBalancers(lbIDs string) bool { + lbs := strings.Split(lbIDs, ",") + params := qingyun.UpdateLoadBalancersRequest{ + Loadbalancers: lbs, + } + resp, err := global.QingClix.UpdateLoadBalancers(params) + if err != nil { + logrus.Fatalf("retcode: %d, msg: %s", resp.RetCode, err.Error()) + } + + if resp.RetCode == 0 { + logrus.Infof("update lb request is accepted, job_id is %s", resp.JobID) + return true + } + + return false +}