hcloud-pricing-exporter/fetcher/volume.go
2021-03-05 21:02:07 +01:00

36 lines
885 B
Go

package fetcher
import (
"math"
"strconv"
"github.com/hetznercloud/hcloud-go/hcloud"
)
var _ Fetcher = &volume{}
// NewVolume creates a new fetcher that will collect pricing information on volumes.
func NewVolume(pricing *PriceProvider) Fetcher {
return &server{newBase(pricing, "volume", "location", "bytes")}
}
type volume struct {
*baseFetcher
}
func (volume volume) Run(client *hcloud.Client) error {
volumes, _, err := client.Volume.List(ctx, hcloud.VolumeListOpts{})
if err != nil {
return err
}
for _, v := range volumes {
monthlyPrice := math.Ceil(float64(v.Size)/sizeGB) * volume.pricing.Volume()
hourlyPrice := pricingPerHour(monthlyPrice)
volume.hourly.WithLabelValues(v.Name, v.Location.Name, strconv.Itoa(v.Size)).Set(hourlyPrice)
volume.monthly.WithLabelValues(v.Name, v.Location.Name, strconv.Itoa(v.Size)).Set(monthlyPrice)
}
return nil
}