mirror of
https://github.com/jangraefen/hcloud-pricing-exporter
synced 2024-11-10 05:54:15 +00:00
fca0b5f77b
* 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
41 lines
996 B
Go
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
|
|
}
|