hcloud-pricing-exporter/fetcher/floatingip.go

44 lines
1 KiB
Go
Raw Normal View History

2021-03-05 18:47:23 +00:00
package fetcher
import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
2021-03-05 18:47:23 +00:00
)
var _ Fetcher = &floatingIP{}
2021-03-05 18:53:37 +00:00
// 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...)}
2021-03-05 18:47:23 +00:00
}
type floatingIP struct {
*baseFetcher
}
func (floatingIP floatingIP) Run(client *hcloud.Client) error {
2024-01-17 19:19:21 +00:00
floatingIPs, err := client.FloatingIP.All(ctx)
2021-03-05 18:47:23 +00:00
if err != nil {
return err
}
for _, f := range floatingIPs {
location := f.HomeLocation
2021-08-24 10:16:03 +00:00
monthlyPrice := floatingIP.pricing.FloatingIP(f.Type, location.Name)
2021-03-05 18:47:23 +00:00
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)
2021-03-05 18:47:23 +00:00
}
return nil
}