2024-05-04 17:48:19 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
|
|
|
|
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/applications"
|
2024-05-13 08:41:14 +00:00
|
|
|
"github.com/go-logr/logr"
|
2024-05-13 11:43:26 +00:00
|
|
|
"github.com/golang/protobuf/ptypes/empty"
|
|
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
2024-05-04 17:48:19 +00:00
|
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
|
|
)
|
|
|
|
|
2024-05-13 08:41:14 +00:00
|
|
|
func NewApplicationsGrpcImpl(controller ctrl.Manager, log logr.Logger) *ApplicationServer {
|
2024-05-04 17:48:19 +00:00
|
|
|
return &ApplicationServer{
|
2024-05-13 08:41:14 +00:00
|
|
|
controller: controller,
|
|
|
|
logInstance: log,
|
2024-05-04 17:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ApplicationServer struct {
|
|
|
|
proto.UnimplementedApplicationsServer
|
2024-05-13 08:41:14 +00:00
|
|
|
controller ctrl.Manager
|
|
|
|
logInstance logr.Logger
|
2024-05-04 17:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create an environment
|
|
|
|
func (app *ApplicationServer) Create(ctx context.Context, in *proto.CreateOptions) (*proto.ApplicationFull, error) {
|
2024-05-13 08:41:14 +00:00
|
|
|
log := app.logInstance
|
|
|
|
log.WithValues("user_id", in.GetOwnerId().GetUuid(), "environment_id", in.GetSpec().GetEnvironemntId(), "app_name", in.GetSpec().GetApplication())
|
|
|
|
ctx = logr.NewContext(ctx, log)
|
2024-05-04 17:48:19 +00:00
|
|
|
|
|
|
|
data := &controllers.ApplicationData{
|
|
|
|
Name: in.Metadata.Name,
|
|
|
|
Application: in.Spec.Application,
|
|
|
|
Version: in.Spec.Version,
|
|
|
|
Environemnt: in.Spec.EnvironemntId,
|
|
|
|
Config: in.Spec.Config,
|
|
|
|
RawConfig: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
application := &controllers.Application{
|
|
|
|
UserID: in.GetOwnerId().GetUuid(),
|
|
|
|
Controller: app.controller,
|
|
|
|
Data: data,
|
|
|
|
Token: in.GetToken().GetToken(),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := application.Create(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &proto.ApplicationFull{
|
|
|
|
Metadata: in.GetMetadata(),
|
|
|
|
Id: &proto.ApplicationId{
|
|
|
|
Uuid: application.Data.UUID,
|
|
|
|
},
|
|
|
|
Spec: in.GetSpec(),
|
|
|
|
}, nil
|
|
|
|
}
|
2024-05-13 11:43:26 +00:00
|
|
|
|
|
|
|
func (app *ApplicationServer) Delete(ctx context.Context, in *proto.DeleteOptions) (*empty.Empty, error) {
|
|
|
|
log := app.logInstance
|
|
|
|
log.WithValues("user_id", in.GetOwnerId().GetUuid(), "app_id", in.GetId().GetUuid())
|
|
|
|
ctx = logr.NewContext(ctx, log)
|
|
|
|
|
|
|
|
data := &controllers.ApplicationData{
|
|
|
|
Name: in.Metadata.Name,
|
|
|
|
UUID: in.GetId().GetUuid(),
|
|
|
|
}
|
|
|
|
|
|
|
|
application := &controllers.Application{
|
|
|
|
UserID: in.GetOwnerId().GetUuid(),
|
|
|
|
Controller: app.controller,
|
|
|
|
Data: data,
|
|
|
|
Token: in.GetToken().GetToken(),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := application.Delete(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &emptypb.Empty{}, nil
|
|
|
|
}
|