hcloud-pricing-exporter/fetcher/floatingip.go
Rikard Gynnerstedt fca0b5f77b
Add additional labels flag (#48)
* added additional tags flag

* move additional labels to base fetcher

* find label function instead of jump label

* bugfix to labels not correctly set when -additional-labels flag not present

* check for more than 0, instead of 1
2023-01-25 20:24:33 +01:00

43 lines
1.1 KiB
Go

package fetcher
import (
"github.com/hetznercloud/hcloud-go/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.List(ctx, hcloud.FloatingIPListOpts{})
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
}