179 lines
6.2 KiB
Go
179 lines
6.2 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"browser.local/platform/dto"
|
|
)
|
|
|
|
// serverInstanceCreateWorkflow godoc
|
|
// @Summary Create server instance workflow
|
|
// @Description Creates a server instance through the platform-mediated lifecycle workflow and queues an install job for the selected run endpoint.
|
|
// @Tags server-instances
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param body body dto.ServerLifecycleCreateRequest true "Server lifecycle create request"
|
|
// @Success 200 {object} dto.ServerLifecycleResponse
|
|
// @Failure 400 {object} dto.ErrorResponse
|
|
// @Failure 404 {object} dto.ErrorResponse
|
|
// @Failure 409 {object} dto.ErrorResponse
|
|
// @Failure 405 {object} dto.ErrorResponse
|
|
// @Router /api/v1/server-instances/workflows/create [post]
|
|
func (h *coreHandlers) serverInstanceCreateWorkflow(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
writeMethodNotAllowed(w, http.MethodPost)
|
|
return
|
|
}
|
|
request, err := decodeJSON[dto.ServerLifecycleCreateRequest](r)
|
|
if err != nil {
|
|
writeDecodeError(w, err)
|
|
return
|
|
}
|
|
result, err := h.core.CreateServerInstanceWorkflowForSession(bearerToken(r), request.ToDomain())
|
|
if err != nil {
|
|
writeServiceError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, dto.ServerLifecycleFromDomain(result))
|
|
}
|
|
|
|
// serverDeployment reads safe deployment metadata or updates protected deployment input.
|
|
func (h *coreHandlers) serverDeployment(w http.ResponseWriter, r *http.Request) {
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
view, err := h.core.GetServerDeploymentForSession(bearerToken(r), r.PathValue("id"))
|
|
if err != nil {
|
|
writeServiceError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, dto.ServerDeploymentFromDomain(view))
|
|
case http.MethodPut:
|
|
request, err := decodeJSON[dto.ServerDeploymentRequest](r)
|
|
if err != nil {
|
|
writeDecodeError(w, err)
|
|
return
|
|
}
|
|
view, err := h.core.UpdateServerDeploymentForSession(bearerToken(r), r.PathValue("id"), request.ToDomain())
|
|
if err != nil {
|
|
writeServiceError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, dto.ServerDeploymentFromDomain(view))
|
|
default:
|
|
writeMethodNotAllowed(w, http.MethodGet+", "+http.MethodPut)
|
|
}
|
|
}
|
|
|
|
// serverInstanceDeploy binds an existing draft definition to its configured Run and queues install.
|
|
func (h *coreHandlers) serverInstanceDeploy(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
writeMethodNotAllowed(w, http.MethodPost)
|
|
return
|
|
}
|
|
request, err := decodeJSON[dto.ServerLifecycleCommandRequest](r)
|
|
if err != nil {
|
|
writeDecodeError(w, err)
|
|
return
|
|
}
|
|
result, err := h.core.DeployServerInstanceForSession(bearerToken(r), request.ToDomain(r.PathValue("id")))
|
|
if err != nil {
|
|
writeServiceError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusAccepted, dto.ServerLifecycleFromDomain(result))
|
|
}
|
|
|
|
// serverInstanceStart godoc
|
|
// @Summary Start server instance
|
|
// @Description Validates lifecycle state and config version, then queues a start job through the platform job channel.
|
|
// @Tags server-instances
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path string true "Server instance ID"
|
|
// @Param body body dto.ServerLifecycleCommandRequest true "Server lifecycle command request"
|
|
// @Success 200 {object} dto.ServerLifecycleResponse
|
|
// @Failure 400 {object} dto.ErrorResponse
|
|
// @Failure 404 {object} dto.ErrorResponse
|
|
// @Failure 405 {object} dto.ErrorResponse
|
|
// @Router /api/v1/server-instances/{id}/start [post]
|
|
func (h *coreHandlers) serverInstanceStart(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
writeMethodNotAllowed(w, http.MethodPost)
|
|
return
|
|
}
|
|
request, err := decodeJSON[dto.ServerLifecycleCommandRequest](r)
|
|
if err != nil {
|
|
writeDecodeError(w, err)
|
|
return
|
|
}
|
|
result, err := h.core.StartServerInstanceForSession(bearerToken(r), request.ToDomain(r.PathValue("id")))
|
|
if err != nil {
|
|
writeServiceError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, dto.ServerLifecycleFromDomain(result))
|
|
}
|
|
|
|
// serverInstanceStop godoc
|
|
// @Summary Stop server instance
|
|
// @Description Validates lifecycle state and config version, then queues a stop job through the platform job channel.
|
|
// @Tags server-instances
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path string true "Server instance ID"
|
|
// @Param body body dto.ServerLifecycleCommandRequest true "Server lifecycle command request"
|
|
// @Success 200 {object} dto.ServerLifecycleResponse
|
|
// @Failure 400 {object} dto.ErrorResponse
|
|
// @Failure 404 {object} dto.ErrorResponse
|
|
// @Failure 405 {object} dto.ErrorResponse
|
|
// @Router /api/v1/server-instances/{id}/stop [post]
|
|
func (h *coreHandlers) serverInstanceStop(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
writeMethodNotAllowed(w, http.MethodPost)
|
|
return
|
|
}
|
|
request, err := decodeJSON[dto.ServerLifecycleCommandRequest](r)
|
|
if err != nil {
|
|
writeDecodeError(w, err)
|
|
return
|
|
}
|
|
result, err := h.core.StopServerInstanceForSession(bearerToken(r), request.ToDomain(r.PathValue("id")))
|
|
if err != nil {
|
|
writeServiceError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, dto.ServerLifecycleFromDomain(result))
|
|
}
|
|
|
|
// serverInstanceProcessStatus godoc
|
|
// @Summary Query supervised server process status
|
|
// @Description Queues a typed process.status job for the selected server/profile.
|
|
// @Tags server-instances
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path string true "Server instance ID"
|
|
// @Param body body dto.ServerLifecycleCommandRequest true "Process status request"
|
|
// @Success 202 {object} dto.ServerLifecycleResponse
|
|
// @Failure 400 {object} dto.ErrorResponse
|
|
// @Failure 401 {object} dto.ErrorResponse
|
|
// @Failure 403 {object} dto.ErrorResponse
|
|
// @Failure 404 {object} dto.ErrorResponse
|
|
// @Router /api/v1/server-instances/{id}/process/status [post]
|
|
func (h *coreHandlers) serverInstanceProcessStatus(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
writeMethodNotAllowed(w, http.MethodPost)
|
|
return
|
|
}
|
|
request, err := decodeJSON[dto.ServerLifecycleCommandRequest](r)
|
|
if err != nil {
|
|
writeDecodeError(w, err)
|
|
return
|
|
}
|
|
result, err := h.core.QueryServerInstanceProcessForSession(bearerToken(r), request.ToDomain(r.PathValue("id")))
|
|
if err != nil {
|
|
writeServiceError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusAccepted, dto.ServerLifecycleFromDomain(result))
|
|
}
|