Skip to content

Commit

Permalink
Expose the new traffic fields and add load-balancer change-type comma…
Browse files Browse the repository at this point in the history
…nd (#256)

* Update hcloud-go to latest version and expose the new traffic fields

* Add load-balancer change-type command
  • Loading branch information
LKaemmerling committed Jul 30, 2020
1 parent 143be6c commit d5a31ce
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 4 deletions.
1 change: 1 addition & 0 deletions cli/load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func newLoadBalancerCommand(cli *CLI) *cobra.Command {
newLoadBalancerDetachFromNetworkCommand(cli),
newLoadBalancerEnablePublicInterface(cli),
newLoadBalancerDisablePublicInterface(cli),
newLoadBalancerChangeTypeCommand(cli),
)
return cmd
}
Expand Down
57 changes: 57 additions & 0 deletions cli/load_balancer_change_type.go
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 20 additions & 1 deletion cli/load_balancer_describe.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"encoding/json"
"fmt"
humanize "github.com/dustin/go-humanize"
"github.com/hetznercloud/hcloud-go/hcloud"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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))
Expand All @@ -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)
}
3 changes: 3 additions & 0 deletions cli/load_balancer_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit d5a31ce

Please sign in to comment.