Skip to content

Commit

Permalink
feat: Add loadBalancer ip retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
jdvgh committed Apr 17, 2024
1 parent ee85f1a commit 12d3de5
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 2 deletions.
33 changes: 33 additions & 0 deletions hcloudHelpers/loadbalancers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package hcloudHelpers

import (
"context"
"log"

"github.com/hetznercloud/hcloud-go/hcloud"
)

func GetLoadBalancerIP(token string) string {
return ""
}
func GetLoadBalancers(hcloudToken string) []*hcloud.LoadBalancer {
if hcloudToken == "" {
log.Fatalln("Please supply filled HcloudToken")
}
client := hcloud.NewClient(hcloud.WithToken(hcloudToken))
servers, _, _ := client.LoadBalancer.List(context.Background(), hcloud.LoadBalancerListOpts{ListOpts: hcloud.ListOpts{}})
return servers

}

func GetFirstLoadBalancerIP(hcloudToken string) string {
if servers := GetLoadBalancers(hcloudToken); servers != nil {

return getLoadBalancerIP(servers[0])
}
return ""
}

func getLoadBalancerIP(server *hcloud.LoadBalancer) string {
return server.PublicNet.IPv4.IP.String()
}
8 changes: 6 additions & 2 deletions hetzner-tools-go/cmd/dns/updateBulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ var updateBulkCmd = &cobra.Command{
func init() {
}
func retrieveUpdateRecords(zoneID, dnsToken, hcloudToken string) {
newIP := hcloudHelpers.GetFirstServerIP(hcloudToken)
newIP := hcloudHelpers.GetFirstLoadBalancerIP(hcloudToken)
if newIP == "" {
log.Fatalln("Could not retrieve IP from Server")
log.Println("Could not retrieve IP from LoadBalancer - trying server next")
newIP = hcloudHelpers.GetFirstServerIP(hcloudToken)
if newIP == "" {
log.Fatalln("Could not retrieve IP from Server")
}
}
dnsRecords := dns.GetDnsRecordsByType(dnsToken, zoneID, dns.A)
if dnsRecords.Records == nil {
Expand Down
47 changes: 47 additions & 0 deletions hetzner-tools-go/cmd/loadBalancer/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cmdLoadBalancer

import (
"context"
"fmt"
"log"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/spf13/cobra"
)

// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list",
Short: "List loadBalancers",
Run: func(c *cobra.Command, args []string) {

ListLoadBalancers(HCloudApiToken)

},
}

func init() {
}

func ListLoadBalancers(hcloudToken string) {
client := hcloud.NewClient(hcloud.WithToken(hcloudToken))
loadBalancers, _, err := client.LoadBalancer.List(context.Background(), hcloud.LoadBalancerListOpts{ListOpts: hcloud.ListOpts{}})
if err != nil {
log.Fatalf("error retrieving severs:%v", err)
}
if loadBalancers == nil {
fmt.Printf("error retrieving severs:%v\n", loadBalancers)

} else {
if len(loadBalancers) == 0 {
fmt.Println("None")
} else {
listLoadBalancers(loadBalancers)
}
}
}
func listLoadBalancers(loadBalancers []*hcloud.LoadBalancer) {
for _, loadBalancer := range loadBalancers {
fmt.Printf("ID: %v Name: %v, IPv4: %v\n", loadBalancer.ID, loadBalancer.Name, loadBalancer.PublicNet.IPv4.IP)
}
}
43 changes: 43 additions & 0 deletions hetzner-tools-go/cmd/loadBalancer/loadBalancer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmdLoadBalancer

import (
"log"
"os"

"github.com/joho/godotenv"
"github.com/spf13/cobra"
)

const HCLOUD_API_TOKEN_KEY = "HCLOUD_API_TOKEN"
const HCLOUD_API_TOKEN_FLAG = "hcloud-api-token"

// loadBalancerCmd represents the loadBalancer command
var loadBalancerCmd = &cobra.Command{

Use: "loadBalancer",
Short: "Manage hcloud loadBalancers",
Run: func(c *cobra.Command, args []string) {
c.HelpFunc()(c, args)
os.Exit(1)
},
}
var HCloudApiToken string

func NewLoadBalancerCommand() *cobra.Command {
return loadBalancerCmd
}
func init() {
err := godotenv.Load("../../.env")
if err != nil {
err := godotenv.Load("../.env")
if err != nil {
err = godotenv.Load()
if err != nil {
log.Printf("Could not load .env file at `../../.env` or `../.env` or `.env`")
}
}
}
hcloudApiToken, _ := os.LookupEnv(HCLOUD_API_TOKEN_KEY)
loadBalancerCmd.AddCommand(listCmd)
loadBalancerCmd.PersistentFlags().StringVar(&HCloudApiToken, HCLOUD_API_TOKEN_FLAG, hcloudApiToken, "API Token to authenticate against hcloud api")
}
2 changes: 2 additions & 0 deletions hetzner-tools-go/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"

cmdDns "github.com/jdvgh/hetzner-tools-go/hetzner-tools-go/cmd/dns"
cmdLoadBalancer "github.com/jdvgh/hetzner-tools-go/hetzner-tools-go/cmd/loadBalancer"
cmdServer "github.com/jdvgh/hetzner-tools-go/hetzner-tools-go/cmd/server"
"github.com/spf13/cobra"
)
Expand All @@ -27,6 +28,7 @@ func Execute() {

func init() {
rootCmd.AddCommand(cmdServer.NewServerCommand())
rootCmd.AddCommand(cmdLoadBalancer.NewLoadBalancerCommand())
rootCmd.AddCommand(cmdDns.NewDnsCommand())

}

0 comments on commit 12d3de5

Please sign in to comment.