softplayer-backend/api/v1/environments.go

175 lines
4.8 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-30 07:47:00 +00:00
provider, err := providers.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-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
}
2024-04-30 08:03:06 +00:00
return &proto.EnvironmentFull{
Metadata: in.GetMetadata(),
Spec: in.GetSpec(),
}, nil
2024-03-19 15:49:29 +00:00
}
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
}
2024-04-30 07:47:00 +00:00
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
}
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-04-30 07:47:00 +00:00
},
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-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 {
2024-04-30 08:00:06 +00:00
provider, err := providers.GetProvider(environment.Data.Provider)
if err != nil {
return err
}
k8s, err := kubernetes.GetKubernetes(environment.Data.Kubernetes)
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,
},
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-04-15 13:45:05 +00:00
},
}); err != nil {
return err
}
}
2024-03-19 15:49:29 +00:00
return nil
}