Initial code commit
This commit is contained in:
172
internal/providers/hetzner/client.go
Normal file
172
internal/providers/hetzner/client.go
Normal file
@@ -0,0 +1,172 @@
|
||||
package hetzner
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
hetznerAPIBaseURL = "https://api.hetzner.cloud/v1"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
apiKey string
|
||||
resty *resty.Client
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
PublicNet PublicNet `json:"public_net"`
|
||||
}
|
||||
|
||||
type PublicNet struct {
|
||||
IPv4 IPv4Info `json:"ipv4"`
|
||||
}
|
||||
|
||||
type IPv4Info struct {
|
||||
IP string `json:"ip"`
|
||||
}
|
||||
|
||||
type ServerResponse struct {
|
||||
Server Server `json:"server"`
|
||||
}
|
||||
|
||||
type ServersResponse struct {
|
||||
Servers []Server `json:"servers"`
|
||||
}
|
||||
|
||||
type CreateServerRequest struct {
|
||||
Name string `json:"name"`
|
||||
ServerType string `json:"server_type"`
|
||||
Image string `json:"image"`
|
||||
Location string `json:"location,omitempty"`
|
||||
SSHKeys []int `json:"ssh_keys,omitempty"`
|
||||
UserData string `json:"user_data,omitempty"`
|
||||
Networks []int `json:"networks,omitempty"`
|
||||
Labels map[string]string `json:"labels,omitempty"`
|
||||
}
|
||||
|
||||
func NewClient(apiKey string) *Client {
|
||||
client := resty.New()
|
||||
client.SetBaseURL(hetznerAPIBaseURL)
|
||||
client.SetHeader("Authorization", fmt.Sprintf("Bearer %s", apiKey))
|
||||
client.SetTimeout(30 * time.Second)
|
||||
|
||||
return &Client{
|
||||
apiKey: apiKey,
|
||||
resty: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) CreateServer(req CreateServerRequest) (*Server, error) {
|
||||
var resp ServerResponse
|
||||
|
||||
httpResp, err := c.resty.R().
|
||||
SetBody(req).
|
||||
SetResult(&resp).
|
||||
Post("/servers")
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create server: %w", err)
|
||||
}
|
||||
|
||||
if httpResp.IsError() {
|
||||
return nil, fmt.Errorf("API error: %s - %s", httpResp.Status(), string(httpResp.Body()))
|
||||
}
|
||||
|
||||
return &resp.Server, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetServer(serverID int) (*Server, error) {
|
||||
var resp ServerResponse
|
||||
|
||||
httpResp, err := c.resty.R().
|
||||
SetResult(&resp).
|
||||
Get(fmt.Sprintf("/servers/%d", serverID))
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get server: %w", err)
|
||||
}
|
||||
|
||||
if httpResp.IsError() {
|
||||
return nil, fmt.Errorf("API error: %s - %s", httpResp.Status(), string(httpResp.Body()))
|
||||
}
|
||||
|
||||
return &resp.Server, nil
|
||||
}
|
||||
|
||||
func (c *Client) DeleteServer(serverID int) error {
|
||||
httpResp, err := c.resty.R().
|
||||
Delete(fmt.Sprintf("/servers/%d", serverID))
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete server: %w", err)
|
||||
}
|
||||
|
||||
if httpResp.IsError() {
|
||||
return fmt.Errorf("API error: %s - %s", httpResp.Status(), string(httpResp.Body()))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) WaitForServerReady(serverID int, timeout time.Duration) error {
|
||||
deadline := time.Now().Add(timeout)
|
||||
ticker := time.NewTicker(5 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
server, err := c.GetServer(serverID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to check server status: %w", err)
|
||||
}
|
||||
|
||||
if server.Status == "running" {
|
||||
return nil
|
||||
}
|
||||
|
||||
if time.Now().After(deadline) {
|
||||
return fmt.Errorf("timeout waiting for server to be ready (current status: %s)", server.Status)
|
||||
}
|
||||
case <-time.After(timeout):
|
||||
return fmt.Errorf("timeout waiting for server to be ready")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) GetServerIP(serverID int) (string, error) {
|
||||
server, err := c.GetServer(serverID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if server.PublicNet.IPv4.IP == "" {
|
||||
return "", fmt.Errorf("server has no public IP address")
|
||||
}
|
||||
|
||||
return server.PublicNet.IPv4.IP, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListServers() ([]Server, error) {
|
||||
var resp ServersResponse
|
||||
|
||||
httpResp, err := c.resty.R().
|
||||
SetResult(&resp).
|
||||
Get("/servers")
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list servers: %w", err)
|
||||
}
|
||||
|
||||
if httpResp.IsError() {
|
||||
return nil, fmt.Errorf("API error: %s - %s", httpResp.Status(), string(httpResp.Body()))
|
||||
}
|
||||
|
||||
return resp.Servers, nil
|
||||
}
|
||||
76
internal/providers/hetzner/server.go
Normal file
76
internal/providers/hetzner/server.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package hetzner
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ServerInfo struct {
|
||||
ID int
|
||||
Name string
|
||||
Status string
|
||||
PublicIP string
|
||||
Region string
|
||||
Type string
|
||||
}
|
||||
|
||||
func (c *Client) CreateServerWithConfig(name, serverType, location, image, userData string, sshKeyIDs []int) (*ServerInfo, error) {
|
||||
if location == "" {
|
||||
return nil, fmt.Errorf("location is required")
|
||||
}
|
||||
|
||||
validLocations := map[string]bool{
|
||||
"fsn1": true, // Falkenstein, Germany
|
||||
"nbg1": true, // Nuremberg, Germany
|
||||
"hel1": true, // Helsinki, Finland
|
||||
"ash": true, // Ashburn, Virginia
|
||||
"hil": true, // Hillsboro, Oregon
|
||||
"sin": true, // Singapore
|
||||
}
|
||||
|
||||
if !validLocations[location] {
|
||||
return nil, fmt.Errorf("invalid location: %s (valid locations: fsn1, nbg1, hel1, ash, hil, sin)", location)
|
||||
}
|
||||
|
||||
req := CreateServerRequest{
|
||||
Name: name,
|
||||
ServerType: serverType,
|
||||
Image: image,
|
||||
Location: location,
|
||||
UserData: userData,
|
||||
SSHKeys: sshKeyIDs,
|
||||
Labels: map[string]string{
|
||||
"managed-by": "sslh-lab",
|
||||
"created-at": time.Now().Format("2006-01-02T15-04-05"),
|
||||
},
|
||||
}
|
||||
|
||||
server, err := c.CreateServer(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create server: %w", err)
|
||||
}
|
||||
|
||||
serverInfo := &ServerInfo{
|
||||
ID: server.ID,
|
||||
Name: server.Name,
|
||||
Status: server.Status,
|
||||
Region: location,
|
||||
Type: serverType,
|
||||
}
|
||||
|
||||
if server.PublicNet.IPv4.IP != "" {
|
||||
serverInfo.PublicIP = server.PublicNet.IPv4.IP
|
||||
} else {
|
||||
if err := c.WaitForServerReady(server.ID, 10*time.Minute); err != nil {
|
||||
return nil, fmt.Errorf("server created but failed to become ready: %w", err)
|
||||
}
|
||||
|
||||
ip, err := c.GetServerIP(server.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get server IP: %w", err)
|
||||
}
|
||||
serverInfo.PublicIP = ip
|
||||
}
|
||||
|
||||
return serverInfo, nil
|
||||
}
|
||||
107
internal/providers/hetzner/ssh_keys.go
Normal file
107
internal/providers/hetzner/ssh_keys.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package hetzner
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type SSHKey struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Fingerprint string `json:"fingerprint"`
|
||||
PublicKey string `json:"public_key"`
|
||||
Labels map[string]string `json:"labels,omitempty"`
|
||||
}
|
||||
|
||||
type SSHKeyResponse struct {
|
||||
SSHKey SSHKey `json:"ssh_key"`
|
||||
}
|
||||
|
||||
type SSHKeysResponse struct {
|
||||
SSHKeys []SSHKey `json:"ssh_keys"`
|
||||
}
|
||||
|
||||
type CreateSSHKeyRequest struct {
|
||||
Name string `json:"name"`
|
||||
PublicKey string `json:"public_key"`
|
||||
Labels map[string]string `json:"labels,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Client) CreateSSHKey(name, publicKey string) (*SSHKey, error) {
|
||||
req := CreateSSHKeyRequest{
|
||||
Name: name,
|
||||
PublicKey: publicKey,
|
||||
Labels: map[string]string{
|
||||
"managed-by": "sslh-lab",
|
||||
},
|
||||
}
|
||||
|
||||
var resp SSHKeyResponse
|
||||
|
||||
httpResp, err := c.resty.R().
|
||||
SetBody(req).
|
||||
SetResult(&resp).
|
||||
Post("/ssh_keys")
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create SSH key: %w", err)
|
||||
}
|
||||
|
||||
if httpResp.IsError() {
|
||||
return nil, fmt.Errorf("API error: %s - %s", httpResp.Status(), string(httpResp.Body()))
|
||||
}
|
||||
|
||||
return &resp.SSHKey, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetSSHKeyByName(name string) (*SSHKey, error) {
|
||||
var resp SSHKeysResponse
|
||||
|
||||
httpResp, err := c.resty.R().
|
||||
SetResult(&resp).
|
||||
Get("/ssh_keys")
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list SSH keys: %w", err)
|
||||
}
|
||||
|
||||
if httpResp.IsError() {
|
||||
return nil, fmt.Errorf("API error: %s - %s", httpResp.Status(), string(httpResp.Body()))
|
||||
}
|
||||
|
||||
for _, key := range resp.SSHKeys {
|
||||
if key.Name == name {
|
||||
return &key, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("SSH key with name '%s' not found", name)
|
||||
}
|
||||
|
||||
func (c *Client) GetOrCreateSSHKey(name, publicKey string) (int, error) {
|
||||
existingKey, err := c.GetSSHKeyByName(name)
|
||||
if err == nil {
|
||||
return existingKey.ID, nil
|
||||
}
|
||||
|
||||
newKey, err := c.CreateSSHKey(name, publicKey)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to create SSH key: %w", err)
|
||||
}
|
||||
|
||||
return newKey.ID, nil
|
||||
}
|
||||
|
||||
func (c *Client) DeleteSSHKey(keyID int) error {
|
||||
httpResp, err := c.resty.R().
|
||||
Delete(fmt.Sprintf("/ssh_keys/%d", keyID))
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete SSH key: %w", err)
|
||||
}
|
||||
|
||||
if httpResp.IsError() {
|
||||
return fmt.Errorf("API error: %s - %s", httpResp.Status(), string(httpResp.Body()))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user