-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters