From d5a31ce219a36dc809a7b4944ba3ff1169bd7721 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Thu, 30 Jul 2020 08:32:51 +0200 Subject: [PATCH] Expose the new traffic fields and add load-balancer change-type command (#256) * Update hcloud-go to latest version and expose the new traffic fields * Add load-balancer change-type command --- cli/load_balancer.go | 1 + cli/load_balancer_change_type.go | 57 ++++++++++++++++++++++++++++++++ cli/load_balancer_describe.go | 21 +++++++++++- cli/load_balancer_list.go | 3 ++ go.mod | 2 +- go.sum | 4 +-- 6 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 cli/load_balancer_change_type.go diff --git a/cli/load_balancer.go b/cli/load_balancer.go index 75eac005..344c8389 100644 --- a/cli/load_balancer.go +++ b/cli/load_balancer.go @@ -31,6 +31,7 @@ func newLoadBalancerCommand(cli *CLI) *cobra.Command { newLoadBalancerDetachFromNetworkCommand(cli), newLoadBalancerEnablePublicInterface(cli), newLoadBalancerDisablePublicInterface(cli), + newLoadBalancerChangeTypeCommand(cli), ) return cmd } diff --git a/cli/load_balancer_change_type.go b/cli/load_balancer_change_type.go new file mode 100644 index 00000000..62a554e5 --- /dev/null +++ b/cli/load_balancer_change_type.go @@ -0,0 +1,57 @@ +package cli + +import ( + "fmt" + + "github.com/hetznercloud/hcloud-go/hcloud" + "github.com/spf13/cobra" +) + +func newLoadBalancerChangeTypeCommand(cli *CLI) *cobra.Command { + cmd := &cobra.Command{ + Use: "change-type [FLAGS] LOADBALANCER LOADBALANCERTYPE", + Short: "Change type of a Load Balancer", + Args: cobra.ExactArgs(2), + TraverseChildren: true, + DisableFlagsInUseLine: true, + PreRunE: cli.ensureToken, + RunE: cli.wrap(runLoadBalancerChangeType), + } + + return cmd +} + +func runLoadBalancerChangeType(cli *CLI, cmd *cobra.Command, args []string) error { + idOrName := args[0] + loadBalancer, _, err := cli.Client().LoadBalancer.Get(cli.Context, idOrName) + if err != nil { + return err + } + if loadBalancer == nil { + return fmt.Errorf("Load Balancer not found: %s", idOrName) + } + + loadBalancerTypeIDOrName := args[1] + loadBalancerType, _, err := cli.Client().LoadBalancerType.Get(cli.Context, loadBalancerTypeIDOrName) + if err != nil { + return err + } + if loadBalancerType == nil { + return fmt.Errorf("Load Balancer type not found: %s", loadBalancerTypeIDOrName) + } + + opts := hcloud.LoadBalancerChangeTypeOpts{ + LoadBalancerType: loadBalancerType, + } + action, _, err := cli.Client().LoadBalancer.ChangeType(cli.Context, loadBalancer, opts) + if err != nil { + return err + } + + if err := cli.ActionProgress(cli.Context, action); err != nil { + return err + } + + fmt.Printf("LoadBalancer %d changed to type %s\n", loadBalancer.ID, loadBalancerType.Name) + return nil +} diff --git a/cli/load_balancer_describe.go b/cli/load_balancer_describe.go index 8e4ce39b..0beea7d0 100644 --- a/cli/load_balancer_describe.go +++ b/cli/load_balancer_describe.go @@ -1,6 +1,7 @@ package cli import ( + "encoding/json" "fmt" humanize "github.com/dustin/go-humanize" "github.com/hetznercloud/hcloud-go/hcloud" @@ -35,7 +36,7 @@ func runLoadBalancerDescribe(cli *CLI, cmd *cobra.Command, args []string) error switch { case outputFlags.IsSet("json"): - return serverDescribeJSON(resp) + return loadBalancerDescribeJSON(resp) case outputFlags.IsSet("format"): return describeFormat(loadBalancer, outputFlags["format"][0]) default: @@ -134,6 +135,10 @@ func loadBalancerDescribeText(cli *CLI, loadBalancer *hcloud.LoadBalancer) error } } } + fmt.Printf("Traffic:\n") + fmt.Printf(" Outgoing:\t%v\n", humanize.Bytes(loadBalancer.OutgoingTraffic)) + fmt.Printf(" Ingoing:\t%v\n", humanize.Bytes(loadBalancer.IngoingTraffic)) + fmt.Printf(" Included:\t%v\n", humanize.Bytes(loadBalancer.IncludedTraffic)) fmt.Printf("Protection:\n") fmt.Printf(" Delete:\t%s\n", yesno(loadBalancer.Protection.Delete)) @@ -149,3 +154,17 @@ func loadBalancerDescribeText(cli *CLI, loadBalancer *hcloud.LoadBalancer) error return nil } + +func loadBalancerDescribeJSON(resp *hcloud.Response) error { + var data map[string]interface{} + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return err + } + if loadBalancer, ok := data["load_balancer"]; ok { + return describeJSON(loadBalancer) + } + if loadBalancers, ok := data["load_balancers"].([]interface{}); ok { + return describeJSON(loadBalancers[0]) + } + return describeJSON(data) +} diff --git a/cli/load_balancer_list.go b/cli/load_balancer_list.go index 4c609bc8..0bb7dfe7 100644 --- a/cli/load_balancer_list.go +++ b/cli/load_balancer_list.go @@ -65,6 +65,9 @@ func runLoadBalancerList(cli *CLI, cmd *cobra.Command, args []string) error { Labels: loadBalancer.Labels, LoadBalancerType: loadBalancerTypeToSchema(*loadBalancer.LoadBalancerType), Location: locationToSchema(*loadBalancer.Location), + IncludedTraffic: loadBalancer.IncludedTraffic, + OutgoingTraffic: &loadBalancer.OutgoingTraffic, + IngoingTraffic: &loadBalancer.IngoingTraffic, Protection: schema.LoadBalancerProtection{ Delete: loadBalancer.Protection.Delete, }, diff --git a/go.mod b/go.mod index feab1356..820d75b2 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ require ( github.com/cheggaaa/pb/v3 v3.0.4 github.com/dustin/go-humanize v1.0.0 github.com/fatih/structs v1.1.0 - github.com/hetznercloud/hcloud-go v1.18.0 + github.com/hetznercloud/hcloud-go v1.19.0 github.com/pelletier/go-toml v1.7.0 github.com/spf13/cobra v0.0.7 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index 278bb86d..f0835847 100644 --- a/go.sum +++ b/go.sum @@ -51,8 +51,8 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmg github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= -github.com/hetznercloud/hcloud-go v1.18.0 h1:gNmwDQ/Jt7bc7dqb0E1x5hJ52yyYJN8q8OHh/Oq3mMo= -github.com/hetznercloud/hcloud-go v1.18.0/go.mod h1:EhElojlVU1biA5JgBaV8rRU1vE5+iYke402kXC9pooE= +github.com/hetznercloud/hcloud-go v1.19.0 h1:8g28MQg8Eg97K7GASKUnaTZSNKo9CB73Xfxbt/NjvnU= +github.com/hetznercloud/hcloud-go v1.19.0/go.mod h1:EhElojlVU1biA5JgBaV8rRU1vE5+iYke402kXC9pooE= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=