Skip to content

Commit

Permalink
refactor(loadbalancertype): use new price formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
apricote committed Jul 16, 2024
1 parent dcfe5b6 commit d5d2bea
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
36 changes: 30 additions & 6 deletions internal/cmd/loadbalancertype/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
Expand All @@ -22,7 +23,7 @@ var DescribeCmd = base.DescribeCmd{
}
return lbt, hcloud.SchemaFromLoadBalancerType(lbt), nil
},
PrintText: func(_ state.State, cmd *cobra.Command, resource interface{}) error {
PrintText: func(s state.State, cmd *cobra.Command, resource interface{}) error {
loadBalancerType := resource.(*hcloud.LoadBalancerType)

cmd.Printf("ID:\t\t\t\t%d\n", loadBalancerType.ID)
Expand All @@ -33,12 +34,35 @@ var DescribeCmd = base.DescribeCmd{
cmd.Printf("Max Targets:\t\t\t%d\n", loadBalancerType.MaxTargets)
cmd.Printf("Max assigned Certificates:\t%d\n", loadBalancerType.MaxAssignedCertificates)

cmd.Printf("Pricings per Location:\n")
for _, price := range loadBalancerType.Pricings {
cmd.Printf(" - Location:\t%s:\n", price.Location.Name)
cmd.Printf(" Hourly:\t€ %s\n", price.Hourly.Gross)
cmd.Printf(" Monthly:\t€ %s\n", price.Monthly.Gross)
pricings, err := fullPricingInfo(s, loadBalancerType)
if err != nil {
cmd.PrintErrf("failed to get prices for server type: %v", err)
}

if pricings != nil {
cmd.Printf("Pricings per Location:\n")
for _, price := range pricings {
cmd.Printf(" - Location:\t%s\n", price.Location.Name)
cmd.Printf(" Hourly:\t%s\n", util.GrossPrice(price.Hourly))
cmd.Printf(" Monthly:\t%s\n", util.GrossPrice(price.Monthly))
}
}

return nil
},
}

func fullPricingInfo(s state.State, loadBalancerType *hcloud.LoadBalancerType) ([]hcloud.LoadBalancerTypeLocationPricing, error) {
pricing, _, err := s.Client().Pricing().Get(s)
if err != nil {
return nil, err
}

for _, price := range pricing.LoadBalancerTypes {
if price.LoadBalancerType.ID == loadBalancerType.ID {
return price.Pricings, nil
}
}

return nil, nil
}
43 changes: 43 additions & 0 deletions internal/cmd/loadbalancertype/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,46 @@ func TestDescribe(t *testing.T) {
MaxAssignedCertificates: 10,
}, nil, nil)

fx.Client.PricingClient.EXPECT().
Get(gomock.Any()).
Return(hcloud.Pricing{
LoadBalancerTypes: []hcloud.LoadBalancerTypePricing{
// Two load balancer types to test that fullPricingInfo filters for the correct one
{
LoadBalancerType: &hcloud.LoadBalancerType{ID: 1},
Pricings: []hcloud.LoadBalancerTypeLocationPricing{{
Location: &hcloud.Location{
Name: "Nuremberg",
},
Hourly: hcloud.Price{
Gross: "4.0000",
Currency: "EUR",
},
Monthly: hcloud.Price{
Gross: "7.0000",
Currency: "EUR",
},
}},
},
{
LoadBalancerType: &hcloud.LoadBalancerType{ID: 123},
Pricings: []hcloud.LoadBalancerTypeLocationPricing{{
Location: &hcloud.Location{
Name: "Falkenstein",
},
Hourly: hcloud.Price{
Gross: "1.0000",
Currency: "EUR",
},
Monthly: hcloud.Price{
Gross: "2.0000",
Currency: "EUR",
},
}},
},
},
}, nil, nil)

out, errOut, err := fx.Run(cmd, []string{"lb11"})

expOut := `ID: 123
Expand All @@ -40,6 +80,9 @@ Max Connections: 10000
Max Targets: 25
Max assigned Certificates: 10
Pricings per Location:
- Location: Falkenstein
Hourly: € 1.0000
Monthly: € 2.0000
`

assert.NoError(t, err)
Expand Down

0 comments on commit d5d2bea

Please sign in to comment.