Initial code commit
This commit is contained in:
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
|
||||
}
|
||||
Reference in New Issue
Block a user