42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
package internal
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"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, errors.New(fmt.Sprintf("Error listing Hetzner Nodes: %s", err.Error()))
|
|
}
|
|
|
|
if servers == nil {
|
|
return nil, errors.New(fmt.Sprintf("No Nodes found with label selector: %s", cfg.LabelSelector))
|
|
}
|
|
return servers, nil
|
|
|
|
}
|
|
|
|
func GetAllIps(servers []*hcloud.Server) ([]string, error) {
|
|
ips := make([]string, len(servers))
|
|
for i, instance := range servers {
|
|
if len(instance.PrivateNet) == 0 || instance.PrivateNet[0].IP == nil {
|
|
return []string{""}, errors.New(fmt.Sprintf("Instance %s has no attached IP", instance.Name))
|
|
}
|
|
log.WithFields(log.Fields{
|
|
"Caller": "GetAllIps",
|
|
}).Info(fmt.Sprintf("Found IP: %s", instance.PrivateNet[0].IP.String()))
|
|
ips[i] = instance.PrivateNet[0].IP.String()
|
|
}
|
|
return ips, nil
|
|
}
|