diff --git a/api/v1/environments.go b/api/v1/environments.go index 497389d..bcd4cea 100644 --- a/api/v1/environments.go +++ b/api/v1/environments.go @@ -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 } diff --git a/internal/controllers/environments.go b/internal/controllers/environments.go index 021f474..89a6066 100644 --- a/internal/controllers/environments.go +++ b/internal/controllers/environments.go @@ -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