softplayer-backend/api/v1/environments.go

136 lines
3.2 KiB
Go

package v1
import (
"context"
"errors"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc/metadata"
ctrl "sigs.k8s.io/controller-runtime"
)
func NewapiGrpcImpl(controller ctrl.Manager) *EnvironmentsServer {
return &EnvironmentsServer{
controller: controller,
}
}
type EnvironmentsServer struct {
proto.UnimplementedEnvironmentsServer
controller ctrl.Manager
}
func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.EnvironmentData) (*proto.EnvironmentFull, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, errors.New("metadata is not provided")
}
token, ok := md["token"]
if !ok {
return nil, errors.New("token is not sent via metadata")
}
uuid, ok := md["uuid"]
if !ok {
return nil, errors.New("used id is not sent via metadata")
}
data := &controllers.EnvironemntData{
Name: in.GetName(),
Provider: in.GetProvider().String(),
Kubernetes: in.GetKubernetes().String(),
}
environment := &controllers.Environemnt{
UserID: uuid[0],
Controller: e.controller,
Data: data,
Token: token[0],
}
err := environment.Create(ctx)
if err != nil {
return nil, err
}
return nil, nil
}
func (e *EnvironmentsServer) Update(ctx context.Context, in *proto.EnvironmentFull) (*proto.EnvironmentFull, error) {
return nil, nil
}
func (e *EnvironmentsServer) Delete(ctx context.Context, in *proto.EnvironmentFull) (*empty.Empty, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, errors.New("metadata is not provided")
}
token, ok := md["token"]
if !ok {
return nil, errors.New("token is not sent via metadata")
}
uuid, ok := md["uuid"]
if !ok {
return nil, errors.New("used id is not sent via metadata")
}
data := &controllers.EnvironemntData{
Name: in.Data.GetName(),
Provider: in.Data.GetProvider().String(),
Kubernetes: in.Data.GetKubernetes().String(),
}
environment := &controllers.Environemnt{
UserID: uuid[0],
Controller: e.controller,
Data: data,
Token: token[0],
}
err := environment.Create(ctx)
if err != nil {
return nil, err
}
return nil, nil
}
func (e *EnvironmentsServer) Get(ctx context.Context, in *proto.EnvironmentId) (*proto.EnvironmentFull, error) {
return nil, nil
}
func (e *EnvironmentsServer) List(in *empty.Empty, stream proto.Environments_ListServer) error {
md, ok := metadata.FromIncomingContext(stream.Context())
if !ok {
return errors.New("metadata is not provided")
}
token, ok := md["token"]
if !ok {
return errors.New("token is not sent via metadata")
}
uuid, ok := md["uuid"]
if !ok {
return errors.New("used id is not sent via metadata")
}
environment := &controllers.Environemnt{
UserID: uuid[0],
Controller: e.controller,
Token: token[0],
}
envs, err := environment.ListEnvs(stream.Context())
if err != nil {
return err
}
for _, env := range envs {
if err := stream.Send(&proto.EnvironmentFull{
Id: &proto.EnvironmentId{Id: "test"},
Data: &proto.EnvironmentData{
Name: env,
},
}); err != nil {
return err
}
}
return nil
}