Skip to content

Commit

Permalink
feat(servertype): show included traffic (#480)
Browse files Browse the repository at this point in the history
* deps: bump hcloud-go to v1.45.1

* feat(servertype): show included traffic
  • Loading branch information
apricote authored May 15, 2023
1 parent 5d086dc commit 17c5f4f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 23 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.5.9
github.com/guptarohit/asciigraph v0.5.5
github.com/hetznercloud/hcloud-go v1.44.0
github.com/hetznercloud/hcloud-go v1.45.1
github.com/pelletier/go-toml/v2 v2.0.6
github.com/rjeczalik/interfaces v0.3.0
github.com/spf13/cobra v1.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/guptarohit/asciigraph v0.5.5 h1:ccFnUF8xYIOUPPY3tmdvRyHqmn1MYI9iv1pLKX+/ZkQ=
github.com/guptarohit/asciigraph v0.5.5/go.mod h1:dYl5wwK4gNsnFf9Zp+l06rFiDZ5YtXM6x7SRWZ3KGag=
github.com/hetznercloud/hcloud-go v1.44.0 h1:zMqXHs9crinVy7SkaVCREk7hN2THqxPCEKZ4H3mY2rI=
github.com/hetznercloud/hcloud-go v1.44.0/go.mod h1:aAUGxSfSnB8/lVXHNEDxtCT1jykaul8kqjD7f5KQXF8=
github.com/hetznercloud/hcloud-go v1.45.1 h1:nl0OOklFfQT5J6AaNIOhl5Ruh3fhmGmhvZEqHbibVuk=
github.com/hetznercloud/hcloud-go v1.45.1/go.mod h1:aAUGxSfSnB8/lVXHNEDxtCT1jykaul8kqjD7f5KQXF8=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down
20 changes: 11 additions & 9 deletions internal/cmd/servertype/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/spf13/cobra"

Expand All @@ -23,15 +24,16 @@ var describeCmd = base.DescribeCmd{
PrintText: func(_ context.Context, _ hcapi2.Client, _ *cobra.Command, resource interface{}) error {
serverType := resource.(*hcloud.ServerType)

fmt.Printf("ID:\t\t%d\n", serverType.ID)
fmt.Printf("Name:\t\t%s\n", serverType.Name)
fmt.Printf("Description:\t%s\n", serverType.Description)
fmt.Printf("Cores:\t\t%d\n", serverType.Cores)
fmt.Printf("CPU Type:\t%s\n", serverType.CPUType)
fmt.Printf("Architecture:\t%s\n", serverType.Architecture)
fmt.Printf("Memory:\t\t%.1f GB\n", serverType.Memory)
fmt.Printf("Disk:\t\t%d GB\n", serverType.Disk)
fmt.Printf("Storage Type:\t%s\n", serverType.StorageType)
fmt.Printf("ID:\t\t\t%d\n", serverType.ID)
fmt.Printf("Name:\t\t\t%s\n", serverType.Name)
fmt.Printf("Description:\t\t%s\n", serverType.Description)
fmt.Printf("Cores:\t\t\t%d\n", serverType.Cores)
fmt.Printf("CPU Type:\t\t%s\n", serverType.CPUType)
fmt.Printf("Architecture:\t\t%s\n", serverType.Architecture)
fmt.Printf("Memory:\t\t\t%.1f GB\n", serverType.Memory)
fmt.Printf("Disk:\t\t\t%d GB\n", serverType.Disk)
fmt.Printf("Storage Type:\t\t%s\n", serverType.StorageType)
fmt.Printf("Included Traffic:\t%d TB\n", serverType.IncludedTraffic/util.Tebibyte)

fmt.Printf("Pricings per Location:\n")
for _, price := range serverType.Pricings {
Expand Down
8 changes: 6 additions & 2 deletions internal/cmd/servertype/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
var ListCmd = base.ListCmd{
ResourceNamePlural: "Server Types",

DefaultColumns: []string{"id", "name", "cores", "cpu_type", "architecture", "memory", "disk", "storage_type"},
DefaultColumns: []string{"id", "name", "cores", "cpu_type", "architecture", "memory", "disk", "storage_type", "traffic"},

Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
opts := hcloud.ServerTypeListOpts{ListOpts: listOpts}
Expand All @@ -44,7 +44,11 @@ var ListCmd = base.ListCmd{
AddFieldFn("disk", output.FieldFn(func(obj interface{}) string {
serverType := obj.(*hcloud.ServerType)
return fmt.Sprintf("%d GB", serverType.Disk)
}))
})).
AddFieldFn("traffic", func(obj interface{}) string {
serverType := obj.(*hcloud.ServerType)
return fmt.Sprintf("%d TB", serverType.IncludedTraffic/util.Tebibyte)
})
},

JSONSchema: func(resources []interface{}) interface{} {
Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/util/byte_units.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package util

const (
Byte = 1
Kibibyte = Byte * 1024
Mebibyte = Kibibyte * 1024
Gibibyte = Mebibyte * 1024
Tebibyte = Gibibyte * 1024
)
19 changes: 10 additions & 9 deletions internal/cmd/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,16 @@ func DatacenterToSchema(datacenter hcloud.Datacenter) schema.Datacenter {

func ServerTypeToSchema(serverType hcloud.ServerType) schema.ServerType {
serverTypeSchema := schema.ServerType{
ID: serverType.ID,
Name: serverType.Name,
Description: serverType.Description,
Cores: serverType.Cores,
Memory: serverType.Memory,
Disk: serverType.Disk,
StorageType: string(serverType.StorageType),
CPUType: string(serverType.CPUType),
Architecture: string(serverType.Architecture),
ID: serverType.ID,
Name: serverType.Name,
Description: serverType.Description,
Cores: serverType.Cores,
Memory: serverType.Memory,
Disk: serverType.Disk,
StorageType: string(serverType.StorageType),
CPUType: string(serverType.CPUType),
Architecture: string(serverType.Architecture),
IncludedTraffic: serverType.IncludedTraffic,
}
for _, pricing := range serverType.Pricings {
serverTypeSchema.Prices = append(serverTypeSchema.Prices, schema.PricingServerTypePrice{
Expand Down

0 comments on commit 17c5f4f

Please sign in to comment.