softplayer-backend/api/v1/environments.go

138 lines
3.4 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())
if err != nil {
return nil, err
}
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
}
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 nil, nil
}
func (e *EnvironmentsServer) Update(ctx context.Context, in *proto.UpdateOptions) (*proto.EnvironmentFull, error) {
return nil, nil
}
func (e *EnvironmentsServer) Delete(ctx context.Context, in *proto.DeleteOptions) (*empty.Empty, error) {
data := &controllers.EnvironemntData{
Name: in.GetMetadata().GetName(),
}
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 nil, nil
}
func (e *EnvironmentsServer) Get(ctx context.Context, in *proto.GetOptions) (*proto.EnvironmentFull, error) {
data := &controllers.EnvironemntData{
Name: in.GetMetadata().GetName(),
}
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
}
return &proto.EnvironmentFull{
Metadata: &proto.EnvironmentMetadata{
Name: environment.Data.Name,
},
}, 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 {
if err := stream.Send(&proto.EnvironmentFull{
Metadata: &proto.EnvironmentMetadata{
Name: env,
},
}); err != nil {
return err
}
}
return nil
}