package v1 import ( "context" "git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers" "git.badhouseplants.net/softplayer/softplayer-backend/internal/providers/infra" "git.badhouseplants.net/softplayer/softplayer-backend/internal/providers/kubernetes" proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments" "github.com/go-logr/logr" "github.com/golang/protobuf/ptypes/empty" ctrl "sigs.k8s.io/controller-runtime" ) func NewapiGrpcImpl(controller ctrl.Manager, log logr.Logger) *EnvironmentsServer { return &EnvironmentsServer{ controller: controller, logInstance: log, } } type EnvironmentsServer struct { proto.UnimplementedEnvironmentsServer controller ctrl.Manager logInstance logr.Logger } // Create an environment func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.CreateOptions) (*proto.EnvironmentFull, error) { log := e.logInstance log.WithValues("user_id", in.GetOwnerId().GetUuid()) ctx = logr.NewContext(ctx, log) provider, err := infra.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, DiskSize: int(in.GetSpec().GetDiskSize()), } environment := &controllers.Environemnt{ UserID: in.GetOwnerId().GetUuid(), Config: ctrl.GetConfigOrDie(), 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) { log := e.logInstance log.WithValues("user_id", in.GetOwnerId().GetUuid()) ctx = logr.NewContext(ctx, log) provider, err := infra.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, DiskSize: int(in.GetSpec().GetDiskSize()), } environment := &controllers.Environemnt{ Config: ctrl.GetConfigOrDie(), 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) { log := e.logInstance log.WithValues("user_id", in.GetOwnerId().GetUuid()) ctx = logr.NewContext(ctx, log) data := &controllers.EnvironemntData{ Name: in.GetMetadata().GetName(), UUID: in.GetId().GetUuid(), } environment := &controllers.Environemnt{ Config: ctrl.GetConfigOrDie(), 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) { log := e.logInstance log.WithValues("user_id", in.GetOwnerId().GetUuid()) ctx = logr.NewContext(ctx, log) data := &controllers.EnvironemntData{ UUID: in.GetId().GetUuid(), } environment := &controllers.Environemnt{ Config: ctrl.GetConfigOrDie(), 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 := infra.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)]), DiskSize: int32(environment.Data.DiskSize), }, 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 { logInstance := e.logInstance log := logInstance.WithValues("user_id", in.GetOwnerId().GetUuid()) ctx := logr.NewContext(stream.Context(), log) environment := &controllers.Environemnt{ Config: ctrl.GetConfigOrDie(), UserID: in.GetOwnerId().GetUuid(), Controller: e.controller, Token: in.GetToken().GetToken(), } envs, err := environment.List(ctx, in.GetSearchString()) if err != nil { return err } for _, env := range envs { provider, err := infra.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)]), DiskSize: int32(env.Data.DiskSize), }, }); err != nil { return err } } return nil }