softplayer-backend/internal/providers/infra/hetzner.go

101 lines
2.7 KiB
Go
Raw Normal View History

2024-05-06 08:44:08 +00:00
package infra
2024-04-03 18:05:23 +00:00
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.
2024-04-30 08:20:46 +00:00
func (h *Hetzner) RawServerLocation(location string) string {
2024-04-29 18:03:37 +00:00
switch location {
case "ash":
2024-04-30 08:20:46 +00:00
return proto.Location_LOCATION_HETZNER_ASHBURN.String()
2024-04-29 18:03:37 +00:00
case "hil":
2024-04-30 08:20:46 +00:00
return proto.Location_LOCATION_HETZNER_HILLSBORO.String()
2024-04-29 18:03:37 +00:00
case "fsn1":
2024-04-30 08:20:46 +00:00
return proto.Location_LOCATION_HETZNER_FALKENSTEIN.String()
2024-04-29 18:03:37 +00:00
case "nbg1":
2024-04-30 08:20:46 +00:00
return proto.Location_LOCATION_HETZNER_NUREMBERG.String()
2024-04-29 18:03:37 +00:00
case "hel1":
2024-04-30 08:20:46 +00:00
return proto.Location_LOCATION_HETZNER_HELSINKI.String()
2024-04-29 18:03:37 +00:00
default:
2024-04-30 08:20:46 +00:00
return proto.Location_LOCATION_UNSPECIFIED.String()
2024-04-29 18:03:37 +00:00
}
}
// RawServerType implements Providers.
2024-04-30 08:20:46 +00:00
func (h *Hetzner) RawServerType(kind string) string {
2024-04-29 18:03:37 +00:00
switch kind {
case "cpx21":
2024-04-30 08:20:46 +00:00
return proto.ServerType_SERVER_TYPE_STARTER.String()
2024-04-29 18:03:37 +00:00
case "cpx31":
2024-04-30 08:20:46 +00:00
return proto.ServerType_SERVER_TYPE_REGULAR.String()
2024-04-29 18:03:37 +00:00
case "cpx41":
2024-04-30 08:20:46 +00:00
return proto.ServerType_SERVER_TYPE_PLUS.String()
2024-04-29 18:03:37 +00:00
case "cpx51":
2024-04-30 08:20:46 +00:00
return proto.ServerType_SERVER_TYPE_PRO.String()
2024-04-29 18:03:37 +00:00
default:
2024-04-30 08:20:46 +00:00
return proto.ServerType_SERVER_TYPE_UNSPECIFIED.String()
2024-04-29 18:03:37 +00:00
}
}
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
}