softplayer-backend/api/v1/environments.go

262 lines
7.1 KiB
Go
Raw Normal View History

2024-03-19 15:49:29 +00:00
package v1
import (
"context"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
2024-05-06 08:44:08 +00:00
"git.badhouseplants.net/softplayer/softplayer-backend/internal/providers/infra"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/providers/kubernetes"
2024-03-19 15:49:29 +00:00
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
2024-05-06 09:39:28 +00:00
"github.com/go-logr/logr"
2024-03-19 15:49:29 +00:00
"github.com/golang/protobuf/ptypes/empty"
2024-04-03 18:05:23 +00:00
ctrl "sigs.k8s.io/controller-runtime"
2024-03-19 15:49:29 +00:00
)
2024-05-06 09:39:28 +00:00
func NewapiGrpcImpl(controller ctrl.Manager, log logr.Logger) *EnvironmentsServer {
2024-04-03 18:05:23 +00:00
return &EnvironmentsServer{
2024-05-06 09:39:28 +00:00
controller: controller,
logInstance: log,
2024-04-03 18:05:23 +00:00
}
2024-03-19 15:49:29 +00:00
}
type EnvironmentsServer struct {
proto.UnimplementedEnvironmentsServer
2024-05-06 09:39:28 +00:00
controller ctrl.Manager
logInstance logr.Logger
2024-03-19 15:49:29 +00:00
}
// Create an environment
func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.CreateOptions) (*proto.EnvironmentFull, error) {
2024-05-06 09:39:28 +00:00
log := e.logInstance
log.WithValues("user_id", in.GetOwnerId().GetUuid())
ctx = logr.NewContext(ctx, log)
2024-05-06 08:44:08 +00:00
provider, err := infra.GetProvider(in.GetSpec().GetProvider().String())
2024-04-29 17:34:55 +00:00
if err != nil {
return nil, err
}
2024-04-30 07:47:00 +00:00
k8s, err := kubernetes.GetKubernetes(in.GetSpec().GetKubernetes().String())
2024-04-29 18:03:37 +00:00
if err != nil {
return nil, err
}
location, err := provider.GetServerLocation(in.GetSpec().GetServerLocation().String())
if err != nil {
return nil, err
2024-04-29 17:34:55 +00:00
}
2024-04-29 18:03:37 +00:00
serverType, err := provider.GetServerType(in.Spec.ServerType.String())
2024-04-29 17:34:55 +00:00
if err != nil {
return nil, err
}
2024-03-19 15:49:29 +00:00
data := &controllers.EnvironemntData{
2024-04-29 12:07:02 +00:00
Name: in.GetMetadata().GetName(),
Description: in.GetMetadata().GetDescription(),
2024-04-29 18:03:37 +00:00
Provider: provider.GetProviderName(),
Kubernetes: k8s.GetKubernetesName(),
2024-04-29 17:34:55 +00:00
Location: location,
ServerType: serverType,
2024-05-06 19:57:54 +00:00
DiskSize: int(in.GetSpec().GetDiskSize()),
2024-03-19 15:49:29 +00:00
}
2024-04-29 17:34:55 +00:00
2024-03-19 15:49:29 +00:00
environment := &controllers.Environemnt{
UserID: in.GetOwnerId().GetUuid(),
2024-05-06 19:11:44 +00:00
Config: ctrl.GetConfigOrDie(),
2024-04-03 18:05:23 +00:00
Controller: e.controller,
2024-03-19 15:49:29 +00:00
Data: data,
Token: in.GetToken().GetToken(),
2024-03-19 15:49:29 +00:00
}
2024-04-29 17:34:55 +00:00
err = environment.Create(ctx)
2024-03-19 15:49:29 +00:00
if err != nil {
return nil, err
}
2024-04-30 08:03:06 +00:00
return &proto.EnvironmentFull{
Metadata: in.GetMetadata(),
2024-05-02 11:02:38 +00:00
Id: &proto.EnvironmentId{
Uuid: environment.Data.UUID,
},
Spec: in.GetSpec(),
2024-04-30 08:03:06 +00:00
}, nil
2024-03-19 15:49:29 +00:00
}
func (e *EnvironmentsServer) Update(ctx context.Context, in *proto.UpdateOptions) (*proto.EnvironmentFull, error) {
2024-05-06 19:11:44 +00:00
log := e.logInstance
log.WithValues("user_id", in.GetOwnerId().GetUuid())
ctx = logr.NewContext(ctx, log)
2024-05-06 08:44:08 +00:00
provider, err := infra.GetProvider(in.GetSpec().GetProvider().String())
2024-04-30 09:19:56 +00:00
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(),
2024-05-02 11:02:38 +00:00
UUID: in.GetId().GetUuid(),
2024-04-30 09:19:56 +00:00
Description: in.GetMetadata().GetDescription(),
Provider: provider.GetProviderName(),
Kubernetes: k8s.GetKubernetesName(),
Location: location,
ServerType: serverType,
2024-05-06 19:57:54 +00:00
DiskSize: int(in.GetSpec().GetDiskSize()),
2024-04-30 09:19:56 +00:00
}
environment := &controllers.Environemnt{
2024-05-06 19:11:44 +00:00
Config: ctrl.GetConfigOrDie(),
2024-04-30 09:19:56 +00:00
UserID: in.GetOwnerId().GetUuid(),
Controller: e.controller,
Data: data,
Token: in.GetToken().GetToken(),
}
err = environment.Update(ctx)
if err != nil {
return nil, err
}
2024-05-05 19:12:56 +00:00
2024-04-30 09:19:56 +00:00
return &proto.EnvironmentFull{
Metadata: in.GetMetadata(),
2024-05-02 11:02:38 +00:00
Id: in.GetId(),
2024-04-30 09:19:56 +00:00
Spec: in.GetSpec(),
}, nil
2024-03-19 15:49:29 +00:00
}
func (e *EnvironmentsServer) Delete(ctx context.Context, in *proto.DeleteOptions) (*empty.Empty, error) {
2024-05-06 19:11:44 +00:00
log := e.logInstance
log.WithValues("user_id", in.GetOwnerId().GetUuid())
ctx = logr.NewContext(ctx, log)
2024-04-05 15:40:20 +00:00
data := &controllers.EnvironemntData{
2024-04-29 12:07:02 +00:00
Name: in.GetMetadata().GetName(),
2024-05-02 11:02:38 +00:00
UUID: in.GetId().GetUuid(),
2024-04-05 15:40:20 +00:00
}
2024-04-05 15:40:20 +00:00
environment := &controllers.Environemnt{
2024-05-06 19:11:44 +00:00
Config: ctrl.GetConfigOrDie(),
UserID: in.GetOwnerId().GetUuid(),
2024-04-05 15:40:20 +00:00
Controller: e.controller,
Data: data,
Token: in.GetToken().GetToken(),
2024-04-05 15:40:20 +00:00
}
2024-04-30 09:34:10 +00:00
err := environment.Delete(ctx)
2024-04-05 15:40:20 +00:00
if err != nil {
return nil, err
}
2024-03-19 15:49:29 +00:00
return nil, nil
}
func (e *EnvironmentsServer) Get(ctx context.Context, in *proto.GetOptions) (*proto.EnvironmentFull, error) {
2024-05-06 19:11:44 +00:00
log := e.logInstance
log.WithValues("user_id", in.GetOwnerId().GetUuid())
ctx = logr.NewContext(ctx, log)
data := &controllers.EnvironemntData{
2024-05-02 11:02:38 +00:00
UUID: in.GetId().GetUuid(),
2024-04-19 14:23:53 +00:00
}
environment := &controllers.Environemnt{
2024-05-06 19:11:44 +00:00
Config: ctrl.GetConfigOrDie(),
UserID: in.GetOwnerId().GetUuid(),
2024-04-19 14:23:53 +00:00
Controller: e.controller,
Data: data,
Token: in.GetToken().GetToken(),
2024-04-19 14:23:53 +00:00
}
if err := environment.Get(ctx); err != nil {
return nil, err
}
2024-05-06 08:44:08 +00:00
provider, err := infra.GetProvider(environment.Data.Provider)
2024-04-30 07:47:00 +00:00
if err != nil {
return nil, err
}
k8s, err := kubernetes.GetKubernetes(environment.Data.Kubernetes)
if err != nil {
return nil, err
}
2024-04-19 14:23:53 +00:00
return &proto.EnvironmentFull{
2024-04-30 07:47:00 +00:00
Spec: &proto.EnvironmentSpec{
Provider: proto.Provider(proto.Provider_value[provider.RawProviderName()]),
Kubernetes: proto.Kubernetes(proto.Kubernetes_value[k8s.RawKubernetesName()]),
2024-04-30 08:20:46 +00:00
ServerLocation: proto.Location(proto.Location_value[provider.RawServerLocation(environment.Data.Location)]),
ServerType: proto.ServerType(proto.ServerType_value[provider.RawServerType(environment.Data.ServerType)]),
2024-05-06 19:57:54 +00:00
DiskSize: int32(environment.Data.DiskSize),
2024-04-30 07:47:00 +00:00
},
2024-05-02 11:02:38 +00:00
Id: in.GetId(),
2024-04-29 12:07:02 +00:00
Metadata: &proto.EnvironmentMetadata{
2024-04-30 07:47:00 +00:00
Name: environment.Data.Name,
Description: environment.Data.Description,
2024-04-19 14:23:53 +00:00
},
}, nil
2024-03-19 15:49:29 +00:00
}
func (e *EnvironmentsServer) List(in *proto.ListOptions, stream proto.Environments_ListServer) error {
2024-05-06 15:30:35 +00:00
logInstance := e.logInstance
log := logInstance.WithValues("user_id", in.GetOwnerId().GetUuid())
2024-05-06 09:46:23 +00:00
ctx := logr.NewContext(stream.Context(), log)
2024-04-15 13:45:05 +00:00
environment := &controllers.Environemnt{
2024-05-06 19:11:44 +00:00
Config: ctrl.GetConfigOrDie(),
UserID: in.GetOwnerId().GetUuid(),
2024-04-15 13:45:05 +00:00
Controller: e.controller,
Token: in.GetToken().GetToken(),
2024-04-15 13:45:05 +00:00
}
2024-05-06 16:00:31 +00:00
envs, err := environment.List(ctx, in.GetSearchString())
2024-04-15 13:45:05 +00:00
if err != nil {
return err
}
for _, env := range envs {
2024-05-06 08:44:08 +00:00
provider, err := infra.GetProvider(env.Data.Provider)
2024-04-30 08:00:06 +00:00
if err != nil {
return err
}
2024-04-30 08:47:40 +00:00
k8s, err := kubernetes.GetKubernetes(env.Data.Kubernetes)
2024-04-30 08:00:06 +00:00
if err != nil {
return err
}
2024-04-15 13:45:05 +00:00
if err := stream.Send(&proto.EnvironmentFull{
2024-04-29 12:07:02 +00:00
Metadata: &proto.EnvironmentMetadata{
2024-04-30 08:00:06 +00:00
Name: env.Data.Name,
Description: env.Data.Description,
},
2024-05-02 11:02:38 +00:00
Id: &proto.EnvironmentId{
Uuid: env.Data.UUID,
},
2024-04-30 08:00:06 +00:00
Spec: &proto.EnvironmentSpec{
Provider: proto.Provider(proto.Provider_value[provider.RawProviderName()]),
Kubernetes: proto.Kubernetes(proto.Kubernetes_value[k8s.RawKubernetesName()]),
2024-04-30 08:47:40 +00:00
ServerLocation: proto.Location(proto.Location_value[provider.RawServerLocation(env.Data.Location)]),
ServerType: proto.ServerType(proto.ServerType_value[provider.RawServerType(env.Data.ServerType)]),
2024-05-06 19:57:54 +00:00
DiskSize: int32(env.Data.DiskSize),
2024-04-15 13:45:05 +00:00
},
}); err != nil {
return err
}
}
2024-03-19 15:49:29 +00:00
return nil
}