Refactor providers data

This commit is contained in:
Nikolai Rodionov 2024-04-29 19:34:55 +02:00
parent f3e0917471
commit a8325fd202
Signed by: allanger
GPG Key ID: 0AA46A90E25592AD
2 changed files with 31 additions and 19 deletions

View File

@ -2,8 +2,10 @@ package v1
import (
"context"
"fmt"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/providers"
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
"github.com/golang/protobuf/ptypes/empty"
ctrl "sigs.k8s.io/controller-runtime"
@ -22,12 +24,36 @@ type EnvironmentsServer struct {
// Create an environment
func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.CreateOptions) (*proto.EnvironmentFull, error) {
provider, err := providers.GetProvider(in.GetSpec().GetProvider())
if err != nil {
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())
}
serverType, err := provider.GetServerType(in.Spec.HetznerOptions.ServerType)
if err != nil {
return nil, err
}
data := &controllers.EnvironemntData{
Name: in.GetMetadata().GetName(),
Description: in.GetMetadata().GetDescription(),
Provider: in.GetSpec().GetProvider(),
Kubernetes: in.GetSpec().GetKubernetes().String(),
Location: location,
ServerType: serverType,
}
environment := &controllers.Environemnt{
UserID: in.GetOwnerId().GetUuid(),
Controller: e.controller,
@ -35,7 +61,7 @@ func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.CreateOptions
Token: in.GetToken().GetToken(),
}
err := environment.Create(ctx)
err = environment.Create(ctx)
if err != nil {
return nil, err
}

View File

@ -9,7 +9,6 @@ import (
"github.com/joho/godotenv"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/kube"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/providers"
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -31,24 +30,11 @@ type EnvironemntData struct {
Description string
Provider proto.Provider
Kubernetes string
HetznerData proto.HetznerOptions
Location string
ServerType string
}
func (e *EnvironemntData) buildVars() (string, error) {
provider, err := providers.GetProvider(e.Provider)
if err != nil {
return "", err
}
serverType, err := provider.GetServerType(e.HetznerData.ServerType)
if err != nil {
return "", err
}
serverLocation, err := provider.GetServerLocation(e.HetznerData.ServerLocation.String())
if err != nil {
return "", err
}
vars := fmt.Sprintf(`# -- Generated by the softplayer controller
SP_PROVIDER=%s
SP_KUBERNETES=%s
@ -56,8 +42,8 @@ SP_SERVER_TYPE=%s
SP_SERVER_LOCATION=%s`,
e.Provider,
e.Kubernetes,
serverType,
serverLocation,
e.ServerType,
e.Location,
)
return vars, nil