hcloud-pricing-exporter/fetcher/server.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

57 lines
1.4 KiB
Go

package fetcher
import (
"fmt"
"github.com/hetznercloud/hcloud-go/hcloud"
)
var _ Fetcher = &server{}
// NewServer creates a new fetcher that will collect pricing information on servers.
func NewServer(pricing *PriceProvider, additionalLabels ...string) Fetcher {
return &loadBalancer{newBase(pricing, "server", []string{"location", "type"}, additionalLabels...)}
}
type server struct {
*baseFetcher
}
func (server server) Run(client *hcloud.Client) error {
servers, _, err := client.Server.List(ctx, hcloud.ServerListOpts{})
if err != nil {
return err
}
for _, s := range servers {
location := s.Datacenter.Location
labels := append([]string{
s.Name,
location.Name,
s.ServerType.Name,
},
parseAdditionalLabels(server.additionalLabels, s.Labels)...,
)
pricing, err := findServerPricing(location, s.ServerType.Pricings)
if err != nil {
return err
}
parseToGauge(server.hourly.WithLabelValues(labels...), pricing.Hourly.Gross)
parseToGauge(server.monthly.WithLabelValues(labels...), pricing.Monthly.Gross)
}
return nil
}
func findServerPricing(location *hcloud.Location, pricings []hcloud.ServerTypeLocationPricing) (*hcloud.ServerTypeLocationPricing, error) {
for _, pricing := range pricings {
if pricing.Location.Name == location.Name {
return &pricing, nil
}
}
return nil, fmt.Errorf("no server pricing found for location %s", location.Name)
}