softplayer-backend/api/v1/application.go

59 lines
1.5 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"
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
}