hcloud-pricing-exporter/fetcher/floatingip.go
2024-01-17 20:19:21 +01:00

43 lines
1 KiB
Go

package fetcher
import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)
var _ Fetcher = &floatingIP{}
// NewFloatingIP creates a new fetcher that will collect pricing information on floating IPs.
func NewFloatingIP(pricing *PriceProvider, additionalLabels ...string) Fetcher {
return &floatingIP{newBase(pricing, "floatingip", []string{"location", "type"}, additionalLabels...)}
}
type floatingIP struct {
*baseFetcher
}
func (floatingIP floatingIP) Run(client *hcloud.Client) error {
floatingIPs, err := client.FloatingIP.All(ctx)
if err != nil {
return err
}
for _, f := range floatingIPs {
location := f.HomeLocation
monthlyPrice := floatingIP.pricing.FloatingIP(f.Type, location.Name)
hourlyPrice := pricingPerHour(monthlyPrice)
labels := append([]string{
f.Name,
location.Name,
string(f.Type),
},
parseAdditionalLabels(floatingIP.additionalLabels, f.Labels)...,
)
floatingIP.hourly.WithLabelValues(labels...).Set(hourlyPrice)
floatingIP.monthly.WithLabelValues(labels...).Set(monthlyPrice)
}
return nil
}