Skip to content

Commit 94e0f44

Browse files
authored
feat: new APIs for traffic pricings (#495)
The API has been updated to provide a better insight and more flexibility for displaying the pricing of traffic for servers and load balancers. In addition to the new fields, the old fields are deprecated and will be set to `null` in the API on 2024-08-05. As far as we could tell they are not widely used in Open Source code, please check if you are using them in any private code. Some linters automatically check for deprecated fields, if you use `golangci-lint` this is provided by the `staticcheck` linter. You can learn more about this change in [our changelog](https://docs.hetzner.cloud/changelog#2024-07-25-cloud-api-returns-traffic-information-in-different-format). ### Upgrading #### Server Type Included Traffic If you were using the field `hcloud.ServerType.IncludedTraffic`, you can now get the information through `hcloud.ServerType.Pricings`: ```go func main() { // previous includedTraffic := serverType.IncludedTraffic // now locationOfInterest := "fsn1" var includedTraffic uint64 for _, price := range serverType.Pricings { if price.Location.Name == locationOfInterest { includedTraffic = price.IncludedTraffic break } } } ``` #### Traffic Prices If you were using the field `hcloud.Pricing.Traffic`, you can now get the information through `hcloud.Pricing.ServerTypes` or `hcloud.Pricing.LoadBalancerTypes`: ```go func main() { // previous trafficPrice := pricing.Traffic // now serverTypeOfInterest := "cx22" locationOfInterest := "fsn1" var trafficPrice hcloud.Price for _, serverTypePricings := range pricing.ServerTypes { if serverTypePricings.ServerType.Name == serverTypeOfInterest { for _, price := range serverTypePricings { if price.Location.Name == locationOfInterest { trafficPrice = price.PerTBTraffic break } } } } } ```
1 parent 2c1a2d6 commit 94e0f44

File tree

6 files changed

+71
-22
lines changed

6 files changed

+71
-22
lines changed

hcloud/pricing.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import (
88

99
// Pricing specifies pricing information for various resources.
1010
type Pricing struct {
11-
Image ImagePricing
12-
FloatingIP FloatingIPPricing
13-
FloatingIPs []FloatingIPTypePricing
14-
PrimaryIPs []PrimaryIPPricing
11+
Image ImagePricing
12+
FloatingIP FloatingIPPricing
13+
FloatingIPs []FloatingIPTypePricing
14+
PrimaryIPs []PrimaryIPPricing
15+
// Deprecated: [Pricing.Traffic] is deprecated and will report 0 after 2024-08-05.
16+
// Use traffic pricing from [Pricing.ServerTypes] or [Pricing.LoadBalancerTypes] instead.
1517
Traffic TrafficPricing
1618
ServerBackup ServerBackupPricing
1719
ServerTypes []ServerTypePricing
@@ -102,6 +104,10 @@ type ServerTypeLocationPricing struct {
102104
Location *Location
103105
Hourly Price
104106
Monthly Price
107+
108+
// IncludedTraffic is the free traffic per month in bytes
109+
IncludedTraffic uint64
110+
PerTBTraffic Price
105111
}
106112

107113
// LoadBalancerTypePricing provides pricing information for a Load Balancer type.
@@ -116,6 +122,10 @@ type LoadBalancerTypeLocationPricing struct {
116122
Location *Location
117123
Hourly Price
118124
Monthly Price
125+
126+
// IncludedTraffic is the free traffic per month in bytes
127+
IncludedTraffic uint64
128+
PerTBTraffic Price
119129
}
120130

121131
// PricingClient is a client for the pricing API.

hcloud/schema/pricing.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package schema
22

33
// Pricing defines the schema for pricing information.
44
type Pricing struct {
5-
Currency string `json:"currency"`
6-
VATRate string `json:"vat_rate"`
7-
Image PricingImage `json:"image"`
8-
FloatingIP PricingFloatingIP `json:"floating_ip"`
9-
FloatingIPs []PricingFloatingIPType `json:"floating_ips"`
10-
PrimaryIPs []PricingPrimaryIP `json:"primary_ips"`
5+
Currency string `json:"currency"`
6+
VATRate string `json:"vat_rate"`
7+
Image PricingImage `json:"image"`
8+
FloatingIP PricingFloatingIP `json:"floating_ip"`
9+
FloatingIPs []PricingFloatingIPType `json:"floating_ips"`
10+
PrimaryIPs []PricingPrimaryIP `json:"primary_ips"`
11+
// Deprecated: [Pricing.Traffic] is deprecated and will report 0 after 2024-08-05.
12+
// Use traffic pricing from [Pricing.ServerTypes] or [Pricing.LoadBalancerTypes] instead.
1113
Traffic PricingTraffic `json:"traffic"`
1214
ServerBackup PricingServerBackup `json:"server_backup"`
1315
ServerTypes []PricingServerType `json:"server_types"`
@@ -72,6 +74,9 @@ type PricingServerTypePrice struct {
7274
Location string `json:"location"`
7375
PriceHourly Price `json:"price_hourly"`
7476
PriceMonthly Price `json:"price_monthly"`
77+
78+
IncludedTraffic uint64 `json:"included_traffic"`
79+
PricePerTBTraffic Price `json:"price_per_tb_traffic"`
7580
}
7681

7782
// PricingLoadBalancerType defines the schema of pricing information for a Load Balancer type.
@@ -87,6 +92,9 @@ type PricingLoadBalancerTypePrice struct {
8792
Location string `json:"location"`
8893
PriceHourly Price `json:"price_hourly"`
8994
PriceMonthly Price `json:"price_monthly"`
95+
96+
IncludedTraffic uint64 `json:"included_traffic"`
97+
PricePerTBTraffic Price `json:"price_per_tb_traffic"`
9098
}
9199

92100
// PricingGetResponse defines the schema of the response when retrieving pricing information.

hcloud/schema/server_type.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ package schema
22

33
// ServerType defines the schema of a server type.
44
type ServerType struct {
5-
ID int64 `json:"id"`
6-
Name string `json:"name"`
7-
Description string `json:"description"`
8-
Cores int `json:"cores"`
9-
Memory float32 `json:"memory"`
10-
Disk int `json:"disk"`
11-
StorageType string `json:"storage_type"`
12-
CPUType string `json:"cpu_type"`
13-
Architecture string `json:"architecture"`
5+
ID int64 `json:"id"`
6+
Name string `json:"name"`
7+
Description string `json:"description"`
8+
Cores int `json:"cores"`
9+
Memory float32 `json:"memory"`
10+
Disk int `json:"disk"`
11+
StorageType string `json:"storage_type"`
12+
CPUType string `json:"cpu_type"`
13+
Architecture string `json:"architecture"`
14+
15+
// Deprecated: [ServerType.IncludedTraffic] is deprecated and will always report 0 after 2024-08-05.
16+
// Use [ServerType.Prices] instead to get the included traffic for each location.
1417
IncludedTraffic int64 `json:"included_traffic"`
1518
Prices []PricingServerTypePrice `json:"prices"`
1619
Deprecated bool `json:"deprecated"`

hcloud/schema_gen.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,12 @@ type converter interface {
207207

208208
// goverter:map PriceHourly Hourly
209209
// goverter:map PriceMonthly Monthly
210+
// goverter:map PricePerTBTraffic PerTBTraffic
210211
LoadBalancerTypeLocationPricingFromSchema(schema.PricingLoadBalancerTypePrice) LoadBalancerTypeLocationPricing
211212

212213
// goverter:map Hourly PriceHourly
213214
// goverter:map Monthly PriceMonthly
215+
// goverter:map PerTBTraffic PricePerTBTraffic
214216
SchemaFromLoadBalancerTypeLocationPricing(LoadBalancerTypeLocationPricing) schema.PricingLoadBalancerTypePrice
215217

216218
LoadBalancerServiceFromSchema(schema.LoadBalancerService) LoadBalancerService
@@ -263,6 +265,7 @@ type converter interface {
263265

264266
// goverter:map PriceHourly Hourly
265267
// goverter:map PriceMonthly Monthly
268+
// goverter:map PricePerTBTraffic PerTBTraffic
266269
serverTypePricingFromSchema(schema.PricingServerTypePrice) ServerTypeLocationPricing
267270

268271
// goverter:map Image.PerGBMonth.Currency Currency
@@ -306,6 +309,7 @@ type converter interface {
306309

307310
// goverter:map Monthly PriceMonthly
308311
// goverter:map Hourly PriceHourly
312+
// goverter:map PerTBTraffic PricePerTBTraffic
309313
schemaFromServerTypeLocationPricing(ServerTypeLocationPricing) schema.PricingServerTypePrice
310314

311315
FirewallFromSchema(schema.Firewall) *Firewall
@@ -707,8 +711,8 @@ func primaryIPPricingFromSchema(s schema.Pricing) []PrimaryIPPricing {
707711
func trafficPricingFromSchema(s schema.Pricing) TrafficPricing {
708712
return TrafficPricing{
709713
PerTB: Price{
710-
Net: s.Traffic.PricePerTB.Net,
711-
Gross: s.Traffic.PricePerTB.Gross,
714+
Net: s.Traffic.PricePerTB.Net, // nolint:staticcheck // Field is deprecated, but we still need to map it as long as it is available
715+
Gross: s.Traffic.PricePerTB.Gross, // nolint:staticcheck // Field is deprecated, but we still need to map it as long as it is available
712716
Currency: s.Currency,
713717
VATRate: s.VATRate,
714718
},
@@ -734,6 +738,13 @@ func serverTypePricingFromSchema(s schema.Pricing) []ServerTypePricing {
734738
Net: price.PriceMonthly.Net,
735739
Gross: price.PriceMonthly.Gross,
736740
},
741+
IncludedTraffic: price.IncludedTraffic,
742+
PerTBTraffic: Price{
743+
Currency: s.Currency,
744+
VATRate: s.VATRate,
745+
Net: price.PricePerTBTraffic.Net,
746+
Gross: price.PricePerTBTraffic.Gross,
747+
},
737748
}
738749
}
739750
p[i] = ServerTypePricing{
@@ -766,6 +777,13 @@ func loadBalancerTypePricingFromSchema(s schema.Pricing) []LoadBalancerTypePrici
766777
Net: price.PriceMonthly.Net,
767778
Gross: price.PriceMonthly.Gross,
768779
},
780+
IncludedTraffic: price.IncludedTraffic,
781+
PerTBTraffic: Price{
782+
Currency: s.Currency,
783+
VATRate: s.VATRate,
784+
Net: price.PricePerTBTraffic.Net,
785+
Gross: price.PricePerTBTraffic.Gross,
786+
},
769787
}
770788
}
771789
p[i] = LoadBalancerTypePricing{

hcloud/server_type.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ type ServerType struct {
2020
StorageType StorageType
2121
CPUType CPUType
2222
Architecture Architecture
23-
// IncludedTraffic is the free traffic per month in bytes
23+
24+
// Deprecated: [ServerType.IncludedTraffic] is deprecated and will always report 0 after 2024-08-05.
25+
// Use [ServerType.Pricings] instead to get the included traffic for each location.
2426
IncludedTraffic int64
2527
Pricings []ServerTypeLocationPricing
2628
DeprecatableResource

hcloud/zz_schema.go

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)