softplayer-backend/api/v1/environments.go

136 lines
3.2 KiB
Go
Raw Normal View History

2024-03-19 15:49:29 +00:00
package v1
import (
"context"
2024-04-03 18:05:23 +00:00
"errors"
2024-03-19 15:49:29 +00:00
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
"github.com/golang/protobuf/ptypes/empty"
2024-04-03 18:05:23 +00:00
"google.golang.org/grpc/metadata"
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
}
func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.EnvironmentData) (*proto.EnvironmentFull, error) {
2024-04-03 18:05:23 +00:00
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")
}
2024-03-19 15:49:29 +00:00
data := &controllers.EnvironemntData{
2024-04-03 18:05:23 +00:00
Name: in.GetName(),
Provider: in.GetProvider().String(),
Kubernetes: in.GetKubernetes().String(),
2024-03-19 15:49:29 +00:00
}
environment := &controllers.Environemnt{
2024-04-03 18:05:23 +00:00
UserID: uuid[0],
Controller: e.controller,
2024-03-19 15:49:29 +00:00
Data: data,
2024-04-03 18:05:23 +00:00
Token: token[0],
2024-03-19 15:49:29 +00:00
}
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) {
2024-04-05 15:40:20 +00:00
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
}
2024-03-19 15:49:29 +00:00
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 {
2024-04-15 13:45:05 +00:00
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
}
}
2024-03-19 15:49:29 +00:00
return nil
}