Files
browser/platform/dto/server_lifecycle.go
2026-08-20 17:58:36 +08:00

154 lines
9.1 KiB
Go

package dto
import (
"time"
"browser.local/platform/domain"
)
type ServerDeploymentRequest struct {
Mode domain.ServerDeploymentMode `json:"mode"`
CreateInputs map[string]string `json:"createInputs,omitempty"`
ServerRoot string `json:"serverRoot,omitempty"`
WorkingDirectory string `json:"workingDirectory,omitempty"`
InstallCommand string `json:"installCommand,omitempty"`
StartCommand string `json:"startCommand,omitempty"`
StopCommand string `json:"stopCommand,omitempty"`
StatusCommand string `json:"statusCommand,omitempty"`
Shell *domain.ServerCommandShell `json:"shell,omitempty"`
ClearFields []string `json:"clearFields,omitempty"`
}
type ServerDeploymentResponse struct {
ServerInstanceID string `json:"serverInstanceId"`
Mode domain.ServerDeploymentMode `json:"mode,omitempty"`
CreateInputs map[string]string `json:"createInputs,omitempty"`
ServerRootConfigured bool `json:"serverRootConfigured"`
WorkingDirectoryConfigured bool `json:"workingDirectoryConfigured"`
InstallCommandConfigured bool `json:"installCommandConfigured"`
StartCommandConfigured bool `json:"startCommandConfigured"`
StopCommandConfigured bool `json:"stopCommandConfigured"`
StatusCommandConfigured bool `json:"statusCommandConfigured"`
Shell domain.ServerCommandShell `json:"shell,omitempty"`
Revision int `json:"revision"`
UpdatedAt *time.Time `json:"updatedAt,omitempty"`
Projection ServerDeploymentProjectionBody `json:"projection,omitempty"`
LatestDispatch *ServerDeploymentDispatchEvidenceBody `json:"latestDispatch,omitempty"`
}
type ServerDeploymentRevealResponse struct {
ServerInstanceID string `json:"serverInstanceId"`
ServerRoot string `json:"serverRoot"`
WorkingDirectory string `json:"workingDirectory"`
InstallCommand string `json:"installCommand"`
StartCommand string `json:"startCommand"`
StopCommand string `json:"stopCommand"`
StatusCommand string `json:"statusCommand"`
}
type ServerDeploymentDispatchEvidenceBody struct {
JobID string `json:"jobId"`
JobState domain.JobState `json:"jobState"`
DeploymentRevision int `json:"deploymentRevision"`
DeploymentDefinitionIncluded bool `json:"deploymentDefinitionIncluded"`
RunConfirmed bool `json:"runConfirmed"`
}
type ServerDeploymentProjectionBody struct {
State string `json:"state,omitempty"`
Operation string `json:"operation,omitempty"`
TemplateKey string `json:"templateKey,omitempty"`
TemplateVersion string `json:"templateVersion,omitempty"`
PreflightState string `json:"preflightState,omitempty"`
DiscoveryState string `json:"discoveryState,omitempty"`
MappingState string `json:"mappingState,omitempty"`
VerificationState string `json:"verificationState,omitempty"`
DiscoveredFacts map[string]string `json:"discoveredFacts,omitempty"`
MappingResults map[string]string `json:"mappingResults,omitempty"`
VerificationResults map[string]string `json:"verificationResults,omitempty"`
FailureCode string `json:"failureCode,omitempty"`
UpdatedAt *time.Time `json:"updatedAt,omitempty"`
}
type ServerLifecycleCreateRequest struct {
ID string `json:"id"`
PluginID string `json:"pluginId"`
Name string `json:"name"`
OwnerUserID string `json:"ownerUserId,omitempty"`
IdempotencyKey string `json:"idempotencyKey"`
Deployment ServerDeploymentRequest `json:"deployment,omitempty"`
}
type ServerLifecycleCommandRequest struct {
ExpectedConfigVersion int `json:"expectedConfigVersion"`
IdempotencyKey string `json:"idempotencyKey"`
}
type ServerLifecycleResponse struct {
Accepted bool `json:"accepted"`
Action domain.ServerLifecycleAction `json:"action"`
Instance ServerInstanceResponse `json:"instance"`
Job JobResponse `json:"job"`
}
func (request ServerLifecycleCreateRequest) ToDomain() domain.ServerLifecycleCreate {
return domain.ServerLifecycleCreate{
ID: request.ID,
PluginID: request.PluginID,
Name: request.Name,
OwnerUserID: request.OwnerUserID,
IdempotencyKey: request.IdempotencyKey,
Deployment: request.Deployment.deploymentDefinition(),
}
}
func (request ServerDeploymentRequest) ToDomain() domain.ServerDeploymentUpdate {
update := domain.ServerDeploymentUpdate{Mode: request.Mode, CreateInputs: domain.CopyStringMap(request.CreateInputs), ServerRoot: request.ServerRoot, WorkingDirectory: request.WorkingDirectory, InstallCommand: request.InstallCommand, StartCommand: request.StartCommand, StopCommand: request.StopCommand, StatusCommand: request.StatusCommand, ClearFields: domain.CopyStringSlice(request.ClearFields)}
if request.Shell != nil {
update.Shell, update.ShellSet = *request.Shell, true
}
return update
}
func (request ServerDeploymentRequest) deploymentDefinition() domain.ServerDeploymentDefinition {
update := request.ToDomain()
return domain.ServerDeploymentDefinition{Mode: update.Mode, CreateInputs: update.CreateInputs, ServerRoot: update.ServerRoot, WorkingDirectory: update.WorkingDirectory, InstallCommand: update.InstallCommand, StartCommand: update.StartCommand, StopCommand: update.StopCommand, StatusCommand: update.StatusCommand, Shell: update.Shell}
}
func ServerDeploymentFromDomain(view domain.ServerDeploymentView) ServerDeploymentResponse {
return ServerDeploymentResponse{ServerInstanceID: view.ServerInstanceID, Mode: view.Mode, CreateInputs: domain.CopyStringMap(view.CreateInputs), ServerRootConfigured: view.ServerRootConfigured, WorkingDirectoryConfigured: view.WorkingDirectoryConfigured, InstallCommandConfigured: view.InstallCommandConfigured, StartCommandConfigured: view.StartCommandConfigured, StopCommandConfigured: view.StopCommandConfigured, StatusCommandConfigured: view.StatusCommandConfigured, Shell: view.Shell, Revision: view.Revision, UpdatedAt: optionalTime(view.UpdatedAt), Projection: deploymentProjectionFromDomain(view.Projection), LatestDispatch: deploymentDispatchEvidenceFromDomain(view.LatestDispatch)}
}
func ServerDeploymentRevealFromDomain(reveal domain.ServerDeploymentReveal) ServerDeploymentRevealResponse {
return ServerDeploymentRevealResponse{ServerInstanceID: reveal.ServerInstanceID, ServerRoot: reveal.ServerRoot, WorkingDirectory: reveal.WorkingDirectory, InstallCommand: reveal.InstallCommand, StartCommand: reveal.StartCommand, StopCommand: reveal.StopCommand, StatusCommand: reveal.StatusCommand}
}
func deploymentDispatchEvidenceFromDomain(evidence *domain.ServerDeploymentDispatchEvidence) *ServerDeploymentDispatchEvidenceBody {
if evidence == nil {
return nil
}
return &ServerDeploymentDispatchEvidenceBody{JobID: evidence.JobID, JobState: evidence.JobState, DeploymentRevision: evidence.DeploymentRevision, DeploymentDefinitionIncluded: evidence.DeploymentDefinitionIncluded, RunConfirmed: evidence.RunConfirmed}
}
func deploymentProjectionFromDomain(projection domain.ServerDeploymentProjection) ServerDeploymentProjectionBody {
return ServerDeploymentProjectionBody{State: projection.State, Operation: projection.Operation, TemplateKey: projection.TemplateKey, TemplateVersion: projection.TemplateVersion, PreflightState: projection.PreflightState, DiscoveryState: projection.DiscoveryState, MappingState: projection.MappingState, VerificationState: projection.VerificationState, DiscoveredFacts: domain.CopyStringMap(projection.DiscoveredFacts), MappingResults: domain.CopyStringMap(projection.MappingResults), VerificationResults: domain.CopyStringMap(projection.VerificationResults), FailureCode: projection.FailureCode, UpdatedAt: optionalTime(projection.UpdatedAt)}
}
func (request ServerLifecycleCommandRequest) ToDomain(serverInstanceID string) domain.ServerLifecycleCommand {
return domain.ServerLifecycleCommand{
ServerInstanceID: serverInstanceID,
ExpectedConfigVersion: request.ExpectedConfigVersion,
IdempotencyKey: request.IdempotencyKey,
}
}
func ServerLifecycleFromDomain(result domain.ServerLifecycleResult) ServerLifecycleResponse {
result = domain.CopyServerLifecycleResult(result)
return ServerLifecycleResponse{
Accepted: result.Accepted,
Action: result.Action,
Instance: ServerInstanceFromDomain(result.Instance),
Job: JobFromDomain(result.Job),
}
}