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

41 lines
996 B
Go

package fetcher
import (
"github.com/hetznercloud/hcloud-go/hcloud"
)
var _ Fetcher = &snapshot{}
// NewSnapshot creates a new fetcher that will collect pricing information on server snapshots.
func NewSnapshot(pricing *PriceProvider, additionalLabels ...string) Fetcher {
return &snapshot{newBase(pricing, "snapshot", nil, additionalLabels...)}
}
type snapshot struct {
*baseFetcher
}
func (snapshot snapshot) Run(client *hcloud.Client) error {
images, _, err := client.Image.List(ctx, hcloud.ImageListOpts{})
if err != nil {
return err
}
for _, i := range images {
if i.Type == "snapshot" {
monthlyPrice := float64(i.ImageSize) * snapshot.pricing.Image()
hourlyPrice := pricingPerHour(monthlyPrice)
labels := append([]string{
i.Name,
},
parseAdditionalLabels(snapshot.additionalLabels, i.Labels)...,
)
snapshot.hourly.WithLabelValues(labels...).Set(hourlyPrice)
snapshot.monthly.WithLabelValues(labels...).Set(monthlyPrice)
}
}
return nil
}