47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package internal
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hetznercloud/hcloud-go/hcloud"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func GetAllNodes(cfg *Config) ([]*hcloud.Server, error) {
|
|
client := hcloud.NewClient(hcloud.WithToken(cfg.HcloudToken))
|
|
servers, _, err := client.Server.List(context.TODO(), hcloud.ServerListOpts{
|
|
ListOpts: hcloud.ListOpts{
|
|
LabelSelector: cfg.LabelSelector,
|
|
}})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error listing Hetzner Nodes: %s", err.Error())
|
|
}
|
|
|
|
if len(servers) == 0 {
|
|
return nil, fmt.Errorf("no Nodes found with label selector: %s", cfg.LabelSelector)
|
|
}
|
|
|
|
for _, instance := range servers {
|
|
log.WithFields(log.Fields{
|
|
"Caller": "GetAllNodes",
|
|
}).Info(fmt.Sprintf("Found server: %s", instance.Name))
|
|
}
|
|
|
|
return servers, nil
|
|
}
|
|
|
|
func GetAllIps(servers []*hcloud.Server) ([]string, error) {
|
|
ips := make([]string, len(servers))
|
|
for i, instance := range servers {
|
|
if instance.PublicNet.IPv4.IP == nil {
|
|
return []string{""}, fmt.Errorf("instance %s has no public Addresses", instance.Name)
|
|
}
|
|
log.WithFields(log.Fields{
|
|
"Caller": "GetAllIps",
|
|
}).Info(fmt.Sprintf("Found IP: %s", instance.PublicNet.IPv4.IP.String()))
|
|
ips[i] = instance.PublicNet.IPv4.IP.String()
|
|
}
|
|
return ips, nil
|
|
}
|