hcloud-pricing-exporter/fetcher/server.go

50 lines
1.2 KiB
Go
Raw Normal View History

2021-03-05 18:47:23 +00:00
package fetcher
import (
"fmt"
"github.com/hetznercloud/hcloud-go/hcloud"
)
var _ Fetcher = &server{}
2021-03-05 18:53:37 +00:00
// NewServer creates a new fetcher that will collect pricing information on servers.
2021-03-05 19:28:17 +00:00
func NewServer(pricing *PriceProvider) Fetcher {
return &server{newBase(pricing, "server", "location")}
2021-03-05 18:47:23 +00:00
}
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
pricing, err := findServerPricing(location, s.ServerType.Pricings)
if err != nil {
return err
}
parseToGauge(server.hourly.WithLabelValues(s.Name, location.Name, s.ServerType.Name), pricing.Hourly.Gross)
parseToGauge(server.monthly.WithLabelValues(s.Name, location.Name, s.ServerType.Name), pricing.Monthly.Gross)
}
return nil
}
func findServerPricing(location *hcloud.Location, pricings []hcloud.ServerTypeLocationPricing) (*hcloud.ServerTypeLocationPricing, error) {
for _, pricing := range pricings {
if pricing.Location.ID == location.ID {
return &pricing, nil
}
}
return nil, fmt.Errorf("no pricing found for location %s", location.Name)
}