softplayer-backend/api/v1/environments.go

73 lines
1.8 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) {
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 {
return nil
}