feature/tt/initial-implementation (#1)
Reviewed-on: #1 Co-authored-by: Tobias Trabelsi <lerentis@uploadfilter24.eu> Co-committed-by: Tobias Trabelsi <lerentis@uploadfilter24.eu>
This commit is contained in:
40
internal/hetzner.go
Normal file
40
internal/hetzner.go
Normal file
@@ -0,0 +1,40 @@
|
||||
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 servers == nil {
|
||||
return nil, fmt.Errorf("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 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.PrivateNet[0].IP.String()))
|
||||
ips[i] = instance.PublicNet.IPv4.IP.String()
|
||||
}
|
||||
return ips, nil
|
||||
}
|
Reference in New Issue
Block a user