softplayer-backend/internal/providers/hetzner.go

102 lines
2.8 KiB
Go
Raw Normal View History

2024-04-03 18:05:23 +00:00
package providers
2024-04-29 16:28:13 +00:00
import (
2024-04-29 18:03:37 +00:00
"errors"
2024-04-29 16:28:13 +00:00
"fmt"
2024-04-29 18:03:37 +00:00
"strings"
2024-04-29 16:28:13 +00:00
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
2024-04-03 18:05:23 +00:00
)
2024-04-29 16:28:13 +00:00
type Hetzner struct{}
2024-04-29 18:03:37 +00:00
// GetProviderName implements Providers.
func (h *Hetzner) GetProviderName() string {
return "hetzner"
}
// RawProviderName implements Providers.
func (h *Hetzner) RawProviderName() string {
2024-04-29 18:07:26 +00:00
return proto.Provider_PROVIDER_HETZNER.String()
2024-04-29 18:03:37 +00:00
}
// RawServerLocation implements Providers.
func (h *Hetzner) RawServerLocation(location string) (string, error) {
switch location {
case "ash":
return proto.Location_LOCATION_HETZNER_ASHBURN.String(), nil
case "hil":
return proto.Location_LOCATION_HETZNER_HILLSBORO.String(), nil
case "fsn1":
return proto.Location_LOCATION_HETZNER_FALKENSTEIN.String(), nil
case "nbg1":
return proto.Location_LOCATION_HETZNER_NUREMBERG.String(), nil
case "hel1":
return proto.Location_LOCATION_HETZNER_HELSINKI.String(), nil
default:
return "", fmt.Errorf("unknown location: %s", location)
}
}
// RawServerType implements Providers.
func (h *Hetzner) RawServerType(kind string) (string, error) {
switch kind {
case "cpx21":
return proto.ServerType_SERVER_TYPE_STARTER.String(), nil
case "cpx31":
return proto.ServerType_SERVER_TYPE_REGULAR.String(), nil
case "cpx41":
return proto.ServerType_SERVER_TYPE_PLUS.String(), nil
case "cpx51":
return proto.ServerType_SERVER_TYPE_PRO.String(), nil
default:
err := fmt.Errorf("unknown server type: %s", kind)
return "", err
}
}
2024-04-29 16:28:13 +00:00
// GetServerLocation implements Providers.
func (h *Hetzner) GetServerLocation(location string) (string, error) {
2024-04-29 18:13:44 +00:00
if !strings.Contains(location, "HETZNER") {
2024-04-29 18:03:37 +00:00
return "", fmt.Errorf("location isn't supported by hetzner: %s", location)
}
2024-04-29 16:28:13 +00:00
switch location {
2024-04-29 18:03:37 +00:00
case proto.Location_LOCATION_HETZNER_ASHBURN.String():
2024-04-29 16:28:13 +00:00
return "ash", nil
2024-04-29 18:03:37 +00:00
case proto.Location_LOCATION_HETZNER_HILLSBORO.String():
2024-04-29 16:28:13 +00:00
return "hil", nil
2024-04-29 18:03:37 +00:00
case proto.Location_LOCATION_HETZNER_FALKENSTEIN.String():
2024-04-29 16:28:13 +00:00
return "fsn1", nil
2024-04-29 18:03:37 +00:00
case proto.Location_LOCATION_HETZNER_NUREMBERG.String():
2024-04-29 16:28:13 +00:00
return "nbg1", nil
2024-04-29 18:03:37 +00:00
case proto.Location_LOCATION_HETZNER_HELSINKI.String():
2024-04-29 16:28:13 +00:00
return "hel1", nil
default:
return "", fmt.Errorf("unknown location: %s", location)
}
}
2024-04-29 18:03:37 +00:00
func (h *Hetzner) GetServerType(kind string) (serverType string, err error) {
2024-04-29 16:28:13 +00:00
switch kind {
2024-04-29 18:03:37 +00:00
case proto.ServerType_SERVER_TYPE_STARTER.String():
2024-04-29 16:28:13 +00:00
serverType = "cpx21"
return
2024-04-29 18:03:37 +00:00
case proto.ServerType_SERVER_TYPE_REGULAR.String():
2024-04-29 16:28:13 +00:00
serverType = "cpx31"
return
2024-04-29 18:03:37 +00:00
case proto.ServerType_SERVER_TYPE_PLUS.String():
2024-04-29 16:28:13 +00:00
serverType = "cpx41"
return
2024-04-29 18:03:37 +00:00
case proto.ServerType_SERVER_TYPE_PRO.String():
2024-04-29 16:28:13 +00:00
serverType = "cpx51"
return
2024-04-29 18:03:37 +00:00
case proto.ServerType_SERVER_TYPE_CUSTOM.String():
err = errors.New("custom server types are not supported yet")
return
2024-04-29 16:28:13 +00:00
default:
2024-04-29 18:03:37 +00:00
err = fmt.Errorf("unknown server type: %s", kind)
2024-04-29 16:28:13 +00:00
return
}
2024-04-03 18:05:23 +00:00
}