Refactor providers data
This commit is contained in:
		@@ -2,9 +2,9 @@ package v1
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
 | 
			
		||||
	"git.badhouseplants.net/softplayer/softplayer-backend/internal/kubernetes"
 | 
			
		||||
	"git.badhouseplants.net/softplayer/softplayer-backend/internal/providers"
 | 
			
		||||
	proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
 | 
			
		||||
	"github.com/golang/protobuf/ptypes/empty"
 | 
			
		||||
@@ -29,18 +29,17 @@ func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.CreateOptions
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var location string
 | 
			
		||||
	switch in.GetSpec().GetProvider() {
 | 
			
		||||
	case proto.Provider_PROVIDER_HETZNER:
 | 
			
		||||
		location, err = provider.GetServerLocation(in.GetSpec().GetHetznerOptions().GetServerLocation().String())
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
	default:
 | 
			
		||||
		return nil, fmt.Errorf("unknown provider: %s", in.GetSpec().GetProvider())
 | 
			
		||||
	k8s, err := kubernetes.GetKubernetes(in.GetSpec().GetKubernetes())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	serverType, err := provider.GetServerType(in.Spec.HetznerOptions.ServerType)
 | 
			
		||||
	location, err := provider.GetServerLocation(in.GetSpec().GetServerLocation().String())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	serverType, err := provider.GetServerType(in.Spec.ServerType.String())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -48,8 +47,8 @@ func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.CreateOptions
 | 
			
		||||
	data := &controllers.EnvironemntData{
 | 
			
		||||
		Name:        in.GetMetadata().GetName(),
 | 
			
		||||
		Description: in.GetMetadata().GetDescription(),
 | 
			
		||||
		Provider:    in.GetSpec().GetProvider(),
 | 
			
		||||
		Kubernetes:  in.GetSpec().GetKubernetes().String(),
 | 
			
		||||
		Provider:    provider.GetProviderName(),
 | 
			
		||||
		Kubernetes:  k8s.GetKubernetesName(),
 | 
			
		||||
		Location:    location,
 | 
			
		||||
		ServerType:  serverType,
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							@@ -67,7 +67,7 @@ require (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.7
 | 
			
		||||
	git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.8
 | 
			
		||||
	github.com/golang/protobuf v1.5.4
 | 
			
		||||
	golang.org/x/net v0.22.0 // indirect
 | 
			
		||||
	golang.org/x/sys v0.18.0 // indirect
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.sum
									
									
									
									
									
								
							@@ -1,5 +1,5 @@
 | 
			
		||||
git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.7 h1:l7v4iWm1cjvEg6BqbWAm21t6WmY0yb5TacKdjIT28rA=
 | 
			
		||||
git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.7/go.mod h1:OU+833cHwvecr+gsnPEKQYlAJbpL8bqSJVLobdw63qI=
 | 
			
		||||
git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.8 h1:ldO0P0GFilL9rVBdPU8NZFRg5x4FeYC7D3iOSSIHpIA=
 | 
			
		||||
git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.8/go.mod h1:OU+833cHwvecr+gsnPEKQYlAJbpL8bqSJVLobdw63qI=
 | 
			
		||||
github.com/alecthomas/assert/v2 v2.6.0 h1:o3WJwILtexrEUk3cUVal3oiQY2tfgr/FHWiz/v2n4FU=
 | 
			
		||||
github.com/alecthomas/assert/v2 v2.6.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
 | 
			
		||||
github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA=
 | 
			
		||||
 
 | 
			
		||||
@@ -9,7 +9,6 @@ import (
 | 
			
		||||
	"github.com/joho/godotenv"
 | 
			
		||||
 | 
			
		||||
	"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/kube"
 | 
			
		||||
	proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
 | 
			
		||||
	corev1 "k8s.io/api/core/v1"
 | 
			
		||||
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/types"
 | 
			
		||||
@@ -28,7 +27,7 @@ type Environemnt struct {
 | 
			
		||||
type EnvironemntData struct {
 | 
			
		||||
	Name        string
 | 
			
		||||
	Description string
 | 
			
		||||
	Provider    proto.Provider
 | 
			
		||||
	Provider    string
 | 
			
		||||
	Kubernetes  string
 | 
			
		||||
	Location    string
 | 
			
		||||
	ServerType  string
 | 
			
		||||
@@ -58,7 +57,7 @@ func (env *Environemnt) isNsVerified(ctx context.Context) error {
 | 
			
		||||
 | 
			
		||||
	val, ok := ns.GetLabels()["email-verified"]
 | 
			
		||||
	if !ok || val == "false" {
 | 
			
		||||
		return errors.New("User email is not verified, can't create an new env")
 | 
			
		||||
		return errors.New("user email is not verified, can't create an new env")
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										21
									
								
								internal/kubernetes/common.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								internal/kubernetes/common.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
package kubernetes
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Kubernetes interface {
 | 
			
		||||
	GetKubernetesName() string
 | 
			
		||||
	RawKubernetesName() string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetKubernetes(k8s proto.Kubernetes) (Kubernetes, error) {
 | 
			
		||||
	switch k8s {
 | 
			
		||||
	case proto.Kubernetes_KUBERNETES_K3S:
 | 
			
		||||
		return &K3s{}, nil
 | 
			
		||||
	default:
 | 
			
		||||
		return nil, fmt.Errorf("unknown provider: %s", k8s.String())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										15
									
								
								internal/kubernetes/k3s.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								internal/kubernetes/k3s.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
package kubernetes
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type K3s struct{}
 | 
			
		||||
 | 
			
		||||
func (k *K3s) GetKubernetesName() string {
 | 
			
		||||
	return "k3s"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (k *K3s) RawKubernetesName() string {
 | 
			
		||||
	return proto.Kubernetes_KUBERNETES_K3S.String()
 | 
			
		||||
}
 | 
			
		||||
@@ -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