diff --git a/api/v1/environments.go b/api/v1/environments.go index 09d5eb4..16f05e0 100644 --- a/api/v1/environments.go +++ b/api/v1/environments.go @@ -2,12 +2,10 @@ 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" ) @@ -22,31 +20,41 @@ type EnvironmentsServer struct { 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") - } - +// Create an environment +func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.CreateOptions) (*proto.EnvironmentFull, error) { data := &controllers.EnvironemntData{ - Name: in.GetName(), - Provider: in.GetProvider().String(), - Kubernetes: in.GetKubernetes().String(), + Name: in.GetName().GetName(), + Provider: in.GetData().GetProvider().String(), + Kubernetes: in.GetData().GetKubernetes().String(), } environment := &controllers.Environemnt{ - UserID: uuid[0], + UserID: in.GetOwnerId().GetUuid(), Controller: e.controller, Data: data, - Token: token[0], + 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.GetName().GetName(), + } + + environment := &controllers.Environemnt{ + UserID: in.GetOwnerId().GetUuid(), + Controller: e.controller, + Data: data, + Token: in.GetToken().GetToken(), } err := environment.Create(ctx) if err != nil { @@ -55,62 +63,16 @@ func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.EnvironmentDa 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") - } - +func (e *EnvironmentsServer) Get(ctx context.Context, in *proto.GetOptions) (*proto.EnvironmentFull, error) { data := &controllers.EnvironemntData{ - Name: in.Data.GetName(), - Provider: in.Data.GetProvider().String(), - Kubernetes: in.Data.GetKubernetes().String(), + Name: in.GetName().GetName(), } + environment := &controllers.Environemnt{ - UserID: uuid[0], + UserID: in.GetOwnerId().GetUuid(), 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) { - 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") - } - - environment := &controllers.Environemnt{ - UserID: uuid[0], - Controller: e.controller, - Token: token[0], + Token: in.GetToken().GetToken(), } if err := environment.Get(ctx); err != nil { @@ -118,35 +80,19 @@ func (e *EnvironmentsServer) Get(ctx context.Context, in *proto.EnvironmentId) ( } return &proto.EnvironmentFull{ - Id: &proto.EnvironmentId{ - Id: "test", - }, - Data: &proto.EnvironmentData{ + Name: &proto.EnvironmentName{ Name: environment.Data.Name, }, }, 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") - } - +func (e *EnvironmentsServer) List(in *proto.ListOptions, stream proto.Environments_ListServer) error { environment := &controllers.Environemnt{ - UserID: uuid[0], + UserID: in.GetOwnerId().GetUuid(), Controller: e.controller, - Token: token[0], + Token: in.GetToken().GetToken(), } + envs, err := environment.ListEnvs(stream.Context()) if err != nil { return err @@ -154,8 +100,7 @@ func (e *EnvironmentsServer) List(in *empty.Empty, stream proto.Environments_Lis for _, env := range envs { if err := stream.Send(&proto.EnvironmentFull{ - Id: &proto.EnvironmentId{Id: "test"}, - Data: &proto.EnvironmentData{ + Name: &proto.EnvironmentName{ Name: env, }, }); err != nil { diff --git a/go.mod b/go.mod index f526e53..ba32167 100644 --- a/go.mod +++ b/go.mod @@ -67,7 +67,7 @@ require ( ) require ( - git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.3 + git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.5 github.com/golang/protobuf v1.5.4 golang.org/x/net v0.22.0 // indirect golang.org/x/sys v0.18.0 // indirect diff --git a/go.sum b/go.sum index 3a821ae..6b3ce8b 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.3 h1:c0q0nz9bT1TXUaMI7G+RII6mzZI1Tg0xWn2ToKekh4c= -git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.3/go.mod h1:OU+833cHwvecr+gsnPEKQYlAJbpL8bqSJVLobdw63qI= +git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.5 h1:HETV0silTTK//EUGsSM71U1j5lzVjHThmvKXZXWtBLg= +git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.5/go.mod h1:OU+833cHwvecr+gsnPEKQYlAJbpL8bqSJVLobdw63qI= github.com/alecthomas/assert/v2 v2.6.0 h1:o3WJwILtexrEUk3cUVal3oiQY2tfgr/FHWiz/v2n4FU= github.com/alecthomas/assert/v2 v2.6.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k= github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA=