Refactor providers data
This commit is contained in:
		@@ -7,8 +7,12 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Providers interface {
 | 
			
		||||
	GetServerType(proto.ServerType) (string, error)
 | 
			
		||||
	GetProviderName() string
 | 
			
		||||
	RawProviderName() string
 | 
			
		||||
	GetServerType(string) (string, error)
 | 
			
		||||
	GetServerLocation(string) (string, error)
 | 
			
		||||
	RawServerType(string) (string, error)
 | 
			
		||||
	RawServerLocation(string) (string, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetProvider(provider proto.Provider) (Providers, error) {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,47 +1,101 @@
 | 
			
		||||
package providers
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Hetzner struct{}
 | 
			
		||||
 | 
			
		||||
// GetProviderName implements Providers.
 | 
			
		||||
func (h *Hetzner) GetProviderName() string {
 | 
			
		||||
	return "hetzner"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RawProviderName implements Providers.
 | 
			
		||||
func (h *Hetzner) RawProviderName() string {
 | 
			
		||||
	proto.Provider_PROVIDER_HETZNER.String()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetServerLocation implements Providers.
 | 
			
		||||
func (h *Hetzner) GetServerLocation(location string) (string, error) {
 | 
			
		||||
	if !strings.HasPrefix(location, "HETZNER") {
 | 
			
		||||
		return "", fmt.Errorf("location isn't supported by hetzner: %s", location)
 | 
			
		||||
	}
 | 
			
		||||
	switch location {
 | 
			
		||||
	case proto.HetznerLocation_HETZNER_LOCATION_ASHBURN.String():
 | 
			
		||||
	case proto.Location_LOCATION_HETZNER_ASHBURN.String():
 | 
			
		||||
		return "ash", nil
 | 
			
		||||
	case proto.HetznerLocation_HETZNER_LOCATION_HILLSBORO.String():
 | 
			
		||||
	case proto.Location_LOCATION_HETZNER_HILLSBORO.String():
 | 
			
		||||
		return "hil", nil
 | 
			
		||||
	case proto.HetznerLocation_HETZNER_LOCATION_FALKENSTEIN.String():
 | 
			
		||||
	case proto.Location_LOCATION_HETZNER_FALKENSTEIN.String():
 | 
			
		||||
		return "fsn1", nil
 | 
			
		||||
	case proto.HetznerLocation_HETZNER_LOCATION_NUREMBERG.String():
 | 
			
		||||
	case proto.Location_LOCATION_HETZNER_NUREMBERG.String():
 | 
			
		||||
		return "nbg1", nil
 | 
			
		||||
	case proto.HetznerLocation_HETZNER_LOCATION_HELSINKI.String():
 | 
			
		||||
	case proto.Location_LOCATION_HETZNER_HELSINKI.String():
 | 
			
		||||
		return "hel1", nil
 | 
			
		||||
	default:
 | 
			
		||||
		return "", fmt.Errorf("unknown location: %s", location)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (h *Hetzner) GetServerType(kind proto.ServerType) (serverType string, err error) {
 | 
			
		||||
func (h *Hetzner) GetServerType(kind string) (serverType string, err error) {
 | 
			
		||||
	switch kind {
 | 
			
		||||
	case proto.ServerType_SERVER_TYPE_STARTER:
 | 
			
		||||
	case proto.ServerType_SERVER_TYPE_STARTER.String():
 | 
			
		||||
		serverType = "cpx21"
 | 
			
		||||
		return
 | 
			
		||||
	case proto.ServerType_SERVER_TYPE_REGULAR:
 | 
			
		||||
	case proto.ServerType_SERVER_TYPE_REGULAR.String():
 | 
			
		||||
		serverType = "cpx31"
 | 
			
		||||
		return
 | 
			
		||||
	case proto.ServerType_SERVER_TYPE_PLUS:
 | 
			
		||||
	case proto.ServerType_SERVER_TYPE_PLUS.String():
 | 
			
		||||
		serverType = "cpx41"
 | 
			
		||||
		return
 | 
			
		||||
	case proto.ServerType_SERVER_TYPE_PRO:
 | 
			
		||||
	case proto.ServerType_SERVER_TYPE_PRO.String():
 | 
			
		||||
		serverType = "cpx51"
 | 
			
		||||
		return
 | 
			
		||||
	case proto.ServerType_SERVER_TYPE_CUSTOM.String():
 | 
			
		||||
		err = errors.New("custom server types are not supported yet")
 | 
			
		||||
		return
 | 
			
		||||
	default:
 | 
			
		||||
		err = fmt.Errorf("unknown server type: %s", kind.String())
 | 
			
		||||
		err = fmt.Errorf("unknown server type: %s", kind)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user