softplayer-backend/api/v1/application.go

85 lines
2.2 KiB
Go

package v1
import (
"context"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/applications"
"github.com/go-logr/logr"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/protobuf/types/known/emptypb"
ctrl "sigs.k8s.io/controller-runtime"
)
func NewApplicationsGrpcImpl(controller ctrl.Manager, log logr.Logger) *ApplicationServer {
return &ApplicationServer{
controller: controller,
logInstance: log,
}
}
type ApplicationServer struct {
proto.UnimplementedApplicationsServer
controller ctrl.Manager
logInstance logr.Logger
}
// Create an environment
func (app *ApplicationServer) Create(ctx context.Context, in *proto.CreateOptions) (*proto.ApplicationFull, error) {
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)
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
}
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
}