softplayer-backend/api/v1/environments.go

195 lines
5.1 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().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,
}
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 &proto.EnvironmentFull{
Metadata: in.GetMetadata(),
Spec: in.GetSpec(),
}, 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
}
provider, err := providers.GetProvider(environment.Data.Provider)
if err != nil {
return nil, err
}
serverType, err := provider.RawServerType(environment.Data.ServerType)
if err != nil {
return nil, err
}
location, err := provider.RawServerLocation(environment.Data.Location)
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[location]),
ServerType: proto.ServerType(proto.ServerType_value[serverType]),
},
Metadata: &proto.EnvironmentMetadata{
Name: environment.Data.Name,
Description: environment.Data.Description,
},
}, 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 {
provider, err := providers.GetProvider(environment.Data.Provider)
if err != nil {
return err
}
serverType, err := provider.RawServerType(environment.Data.ServerType)
if err != nil {
return err
}
location, err := provider.RawServerLocation(environment.Data.Location)
if err != nil {
return err
}
k8s, err := kubernetes.GetKubernetes(environment.Data.Kubernetes)
if err != nil {
return err
}
if err := stream.Send(&proto.EnvironmentFull{
Metadata: &proto.EnvironmentMetadata{
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()]),
ServerLocation: proto.Location(proto.Location_value[location]),
ServerType: proto.ServerType(proto.ServerType_value[serverType]),
},
}); err != nil {
return err
}
}
return nil
}