softplayer-backend/api/v1/environments.go

138 lines
3.4 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-04-29 18:03:37 +00:00
"git.badhouseplants.net/softplayer/softplayer-backend/internal/kubernetes"
2024-04-29 17:34:55 +00:00
"git.badhouseplants.net/softplayer/softplayer-backend/internal/providers"
2024-03-19 15:49:29 +00:00
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
"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-04-03 18:05:23 +00:00
func NewapiGrpcImpl(controller ctrl.Manager) *EnvironmentsServer {
return &EnvironmentsServer{
controller: controller,
}
2024-03-19 15:49:29 +00:00
}
type EnvironmentsServer struct {
proto.UnimplementedEnvironmentsServer
2024-04-03 18:05:23 +00:00
controller ctrl.Manager
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-04-29 17:34:55 +00:00
provider, err := providers.GetProvider(in.GetSpec().GetProvider())
if err != nil {
return nil, err
}
2024-04-29 18:03:37 +00:00
k8s, err := kubernetes.GetKubernetes(in.GetSpec().GetKubernetes())
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-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-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
}
return nil, nil
}
func (e *EnvironmentsServer) Update(ctx context.Context, in *proto.UpdateOptions) (*proto.EnvironmentFull, error) {
2024-03-19 15:49:29 +00:00
return nil, nil
}
func (e *EnvironmentsServer) Delete(ctx context.Context, in *proto.DeleteOptions) (*empty.Empty, error) {
2024-04-05 15:40:20 +00:00
data := &controllers.EnvironemntData{
2024-04-29 12:07:02 +00:00
Name: in.GetMetadata().GetName(),
2024-04-05 15:40:20 +00:00
}
2024-04-05 15:40:20 +00:00
environment := &controllers.Environemnt{
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
}
err := environment.Create(ctx)
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) {
data := &controllers.EnvironemntData{
2024-04-29 12:07:02 +00:00
Name: in.GetMetadata().GetName(),
2024-04-19 14:23:53 +00:00
}
environment := &controllers.Environemnt{
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
}
return &proto.EnvironmentFull{
2024-04-29 12:07:02 +00:00
Metadata: &proto.EnvironmentMetadata{
2024-04-19 14:23:53 +00:00
Name: environment.Data.Name,
},
}, nil
2024-03-19 15:49:29 +00:00
}
func (e *EnvironmentsServer) List(in *proto.ListOptions, stream proto.Environments_ListServer) error {
2024-04-15 13:45:05 +00:00
environment := &controllers.Environemnt{
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-04-15 13:45:05 +00:00
envs, err := environment.ListEnvs(stream.Context())
if err != nil {
return err
}
for _, env := range envs {
if err := stream.Send(&proto.EnvironmentFull{
2024-04-29 12:07:02 +00:00
Metadata: &proto.EnvironmentMetadata{
2024-04-15 13:45:05 +00:00
Name: env,
},
}); err != nil {
return err
}
}
2024-03-19 15:49:29 +00:00
return nil
}