softplayer-backend/api/v1/environments.go
2024-05-04 19:48:19 +02:00

230 lines
6.1 KiB
Go

package v1
import (
"context"
"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"
ctrl "sigs.k8s.io/controller-runtime"
)
func NewapiGrpcImpl(controller ctrl.Manager) *EnvironmentsServer {
return &EnvironmentsServer{
controller: controller,
}
}
type EnvironmentsServer struct {
proto.UnimplementedEnvironmentsServer
controller ctrl.Manager
}
// Create an environment
func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.CreateOptions) (*proto.EnvironmentFull, error) {
provider, err := providers.GetProvider(in.GetSpec().GetProvider().String())
if err != nil {
return nil, err
}
k8s, err := kubernetes.GetKubernetes(in.GetSpec().GetKubernetes().String())
if err != nil {
return nil, err
}
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
}
data := &controllers.EnvironemntData{
Name: in.GetMetadata().GetName(),
Description: in.GetMetadata().GetDescription(),
Provider: provider.GetProviderName(),
Kubernetes: k8s.GetKubernetesName(),
Location: location,
ServerType: serverType,
}
environment := &controllers.Environemnt{
UserID: in.GetOwnerId().GetUuid(),
Controller: e.controller,
Data: data,
Token: in.GetToken().GetToken(),
}
err = environment.Create(ctx)
if err != nil {
return nil, err
}
return &proto.EnvironmentFull{
Metadata: in.GetMetadata(),
Id: &proto.EnvironmentId{
Uuid: environment.Data.UUID,
},
Spec: in.GetSpec(),
}, nil
}
func (e *EnvironmentsServer) Update(ctx context.Context, in *proto.UpdateOptions) (*proto.EnvironmentFull, error) {
provider, err := providers.GetProvider(in.GetSpec().GetProvider().String())
if err != nil {
return nil, err
}
k8s, err := kubernetes.GetKubernetes(in.GetSpec().GetKubernetes().String())
if err != nil {
return nil, err
}
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
}
data := &controllers.EnvironemntData{
Name: in.GetMetadata().GetName(),
UUID: in.GetId().GetUuid(),
Description: in.GetMetadata().GetDescription(),
Provider: provider.GetProviderName(),
Kubernetes: k8s.GetKubernetesName(),
Location: location,
ServerType: serverType,
}
environment := &controllers.Environemnt{
UserID: in.GetOwnerId().GetUuid(),
Controller: e.controller,
Data: data,
Token: in.GetToken().GetToken(),
}
err = environment.Update(ctx)
if err != nil {
return nil, err
}
return &proto.EnvironmentFull{
Metadata: in.GetMetadata(),
Id: in.GetId(),
Spec: in.GetSpec(),
}, nil
}
func (e *EnvironmentsServer) Delete(ctx context.Context, in *proto.DeleteOptions) (*empty.Empty, error) {
data := &controllers.EnvironemntData{
Name: in.GetMetadata().GetName(),
UUID: in.GetId().GetUuid(),
}
environment := &controllers.Environemnt{
UserID: in.GetOwnerId().GetUuid(),
Controller: e.controller,
Data: data,
Token: in.GetToken().GetToken(),
}
err := environment.Delete(ctx)
if err != nil {
return nil, err
}
return nil, nil
}
func (e *EnvironmentsServer) Get(ctx context.Context, in *proto.GetOptions) (*proto.EnvironmentFull, error) {
data := &controllers.EnvironemntData{
UUID: in.GetId().GetUuid(),
}
environment := &controllers.Environemnt{
UserID: in.GetOwnerId().GetUuid(),
Controller: e.controller,
Data: data,
Token: in.GetToken().GetToken(),
}
if err := environment.Get(ctx); err != nil {
return nil, err
}
provider, err := providers.GetProvider(environment.Data.Provider)
if err != nil {
return nil, err
}
k8s, err := kubernetes.GetKubernetes(environment.Data.Kubernetes)
if err != nil {
return nil, err
}
return &proto.EnvironmentFull{
Spec: &proto.EnvironmentSpec{
Provider: proto.Provider(proto.Provider_value[provider.RawProviderName()]),
Kubernetes: proto.Kubernetes(proto.Kubernetes_value[k8s.RawKubernetesName()]),
ServerLocation: proto.Location(proto.Location_value[provider.RawServerLocation(environment.Data.Location)]),
ServerType: proto.ServerType(proto.ServerType_value[provider.RawServerType(environment.Data.ServerType)]),
},
Id: in.GetId(),
Metadata: &proto.EnvironmentMetadata{
Name: environment.Data.Name,
Description: environment.Data.Description,
},
}, nil
}
func (e *EnvironmentsServer) List(in *proto.ListOptions, stream proto.Environments_ListServer) error {
environment := &controllers.Environemnt{
UserID: in.GetOwnerId().GetUuid(),
Controller: e.controller,
Token: in.GetToken().GetToken(),
}
envs, err := environment.ListEnvs(stream.Context())
if err != nil {
return err
}
for _, env := range envs {
provider, err := providers.GetProvider(env.Data.Provider)
if err != nil {
return err
}
k8s, err := kubernetes.GetKubernetes(env.Data.Kubernetes)
if err != nil {
return err
}
if err := stream.Send(&proto.EnvironmentFull{
Metadata: &proto.EnvironmentMetadata{
Name: env.Data.Name,
Description: env.Data.Description,
},
Id: &proto.EnvironmentId{
Uuid: env.Data.UUID,
},
Spec: &proto.EnvironmentSpec{
Provider: proto.Provider(proto.Provider_value[provider.RawProviderName()]),
Kubernetes: proto.Kubernetes(proto.Kubernetes_value[k8s.RawKubernetesName()]),
ServerLocation: proto.Location(proto.Location_value[provider.RawServerLocation(env.Data.Location)]),
ServerType: proto.ServerType(proto.ServerType_value[provider.RawServerType(env.Data.ServerType)]),
},
}); err != nil {
return err
}
}
return nil
}