Migrate from uuid to name

This commit is contained in:
2025-07-19 11:21:12 +02:00
parent c8e623ae7b
commit 13bbf07465
10 changed files with 1151 additions and 158 deletions

View File

@@ -6,7 +6,6 @@ import (
"os/exec"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
)
type Handler struct {
@@ -82,25 +81,82 @@ func (h *Handler) ListDevicesHandler() http.HandlerFunc {
}
}
// GetInstance godoc
// @Summary Get details of a specific instance
// @Description Returns the details of a specific instance by ID
// ListInstances godoc
// @Summary List all instances
// @Description Returns a list of all instances managed by the server
// @Tags instances
// @Param id path string true "Instance ID"
// @Success 200 {object} Instance "Instance details"
// @Failure 400 {string} string "Invalid UUID format"
// @Produce json
// @Success 200 {array} Instance "List of instances"
// @Failure 500 {string} string "Internal Server Error"
// @Router /instances/{id} [get]
func (h *Handler) GetInstance() http.HandlerFunc {
// @Router /instances [get]
func (h *Handler) ListInstances() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
uuid, err := uuid.Parse(id)
instances, err := h.InstanceManager.ListInstances()
if err != nil {
http.Error(w, "Invalid UUID format", http.StatusBadRequest)
http.Error(w, "Failed to list instances: "+err.Error(), http.StatusInternalServerError)
return
}
instance, err := h.InstanceManager.GetInstance(uuid)
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(instances); err != nil {
http.Error(w, "Failed to encode instances: "+err.Error(), http.StatusInternalServerError)
return
}
}
}
// CreateInstance godoc
// @Summary Create and start a new instance
// @Description Creates a new instance with the provided configuration options
// @Tags instances
// @Accept json
// @Produce json
// @Param options body InstanceOptions true "Instance configuration options"
// @Success 201 {object} Instance "Created instance details"
// @Failure 400 {string} string "Invalid request body"
// @Failure 500 {string} string "Internal Server Error"
// @Router /instances [post]
func (h *Handler) CreateInstance() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var options InstanceOptions
if err := json.NewDecoder(r.Body).Decode(&options); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
instance, err := h.InstanceManager.CreateInstance(&options)
if err != nil {
http.Error(w, "Failed to create instance: "+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(instance); err != nil {
http.Error(w, "Failed to encode instance: "+err.Error(), http.StatusInternalServerError)
return
}
}
}
// GetInstance godoc
// @Summary Get details of a specific instance
// @Description Returns the details of a specific instance by name
// @Tags instances
// @Param name path string true "Instance Name"
// @Success 200 {object} Instance "Instance details"
// @Failure 400 {string} string "Invalid name format"
// @Failure 500 {string} string "Internal Server Error"
// @Router /instances/{name} [get]
func (h *Handler) GetInstance() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
if name == "" {
http.Error(w, "Instance name cannot be empty", http.StatusBadRequest)
return
}
instance, err := h.InstanceManager.GetInstance(name)
if err != nil {
http.Error(w, "Failed to get instance: "+err.Error(), http.StatusInternalServerError)
return
@@ -116,22 +172,21 @@ func (h *Handler) GetInstance() http.HandlerFunc {
// UpdateInstance godoc
// @Summary Update an instance's configuration
// @Description Updates the configuration of a specific instance by ID
// @Description Updates the configuration of a specific instance by name
// @Tags instances
// @Accept json
// @Produce json
// @Param id path string true "Instance ID"
// @Param name path string true "Instance Name"
// @Param options body InstanceOptions true "Instance configuration options"
// @Success 200 {object} Instance "Updated instance details"
// @Failure 400 {string} string "Invalid UUID format"
// @Failure 400 {string} string "Invalid name format"
// @Failure 500 {string} string "Internal Server Error"
// @Router /instances/{id} [put]
// @Router /instances/{name} [put]
func (h *Handler) UpdateInstance() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
uuid, err := uuid.Parse(id)
if err != nil {
http.Error(w, "Invalid UUID format", http.StatusBadRequest)
name := chi.URLParam(r, "name")
if name == "" {
http.Error(w, "Instance name cannot be empty", http.StatusBadRequest)
return
}
@@ -141,13 +196,13 @@ func (h *Handler) UpdateInstance() http.HandlerFunc {
return
}
instance, err := h.InstanceManager.UpdateInstance(uuid, &options)
instance, err := h.InstanceManager.UpdateInstance(name, &options)
if err != nil {
http.Error(w, "Failed to update instance: "+err.Error(), http.StatusInternalServerError)
return
}
instance, err = h.InstanceManager.RestartInstance(uuid)
instance, err = h.InstanceManager.RestartInstance(name)
if err != nil {
http.Error(w, "Failed to restart instance: "+err.Error(), http.StatusInternalServerError)
return
@@ -163,24 +218,23 @@ func (h *Handler) UpdateInstance() http.HandlerFunc {
// StartInstance godoc
// @Summary Start a stopped instance
// @Description Starts a specific instance by ID
// @Description Starts a specific instance by name
// @Tags instances
// @Produce json
// @Param id path string true "Instance ID"
// @Param name path string true "Instance Name"
// @Success 200 {object} Instance "Started instance details"
// @Failure 400 {string} string "Invalid UUID format"
// @Failure 400 {string} string "Invalid name format"
// @Failure 500 {string} string "Internal Server Error"
// @Router /instances/{id}/start [post]
// @Router /instances/{name}/start [post]
func (h *Handler) StartInstance() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
uuid, err := uuid.Parse(id)
if err != nil {
http.Error(w, "Invalid UUID format", http.StatusBadRequest)
name := chi.URLParam(r, "name")
if name == "" {
http.Error(w, "Instance name cannot be empty", http.StatusBadRequest)
return
}
instance, err := h.InstanceManager.StartInstance(uuid)
instance, err := h.InstanceManager.StartInstance(name)
if err != nil {
http.Error(w, "Failed to start instance: "+err.Error(), http.StatusInternalServerError)
return
@@ -196,24 +250,23 @@ func (h *Handler) StartInstance() http.HandlerFunc {
// StopInstance godoc
// @Summary Stop a running instance
// @Description Stops a specific instance by ID
// @Description Stops a specific instance by name
// @Tags instances
// @Produce json
// @Param id path string true "Instance ID"
// @Param name path string true "Instance Name"
// @Success 200 {object} Instance "Stopped instance details"
// @Failure 400 {string} string "Invalid UUID format"
// @Failure 400 {string} string "Invalid name format"
// @Failure 500 {string} string "Internal Server Error"
// @Router /instances/{id}/stop [post]
// @Router /instances/{name}/stop [post]
func (h *Handler) StopInstance() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
uuid, err := uuid.Parse(id)
if err != nil {
http.Error(w, "Invalid UUID format", http.StatusBadRequest)
name := chi.URLParam(r, "name")
if name == "" {
http.Error(w, "Instance name cannot be empty", http.StatusBadRequest)
return
}
instance, err := h.InstanceManager.StopInstance(uuid)
instance, err := h.InstanceManager.StopInstance(name)
if err != nil {
http.Error(w, "Failed to stop instance: "+err.Error(), http.StatusInternalServerError)
return
@@ -229,24 +282,23 @@ func (h *Handler) StopInstance() http.HandlerFunc {
// RestartInstance godoc
// @Summary Restart a running instance
// @Description Restarts a specific instance by ID
// @Description Restarts a specific instance by name
// @Tags instances
// @Produce json
// @Param id path string true "Instance ID"
// @Param name path string true "Instance Name"
// @Success 200 {object} Instance "Restarted instance details"
// @Failure 400 {string} string "Invalid UUID format"
// @Failure 400 {string} string "Invalid name format"
// @Failure 500 {string} string "Internal Server Error"
// @Router /instances/{id}/restart [post]
// @Router /instances/{name}/restart [post]
func (h *Handler) RestartInstance() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
uuid, err := uuid.Parse(id)
if err != nil {
http.Error(w, "Invalid UUID format", http.StatusBadRequest)
name := chi.URLParam(r, "name")
if name == "" {
http.Error(w, "Instance name cannot be empty", http.StatusBadRequest)
return
}
instance, err := h.InstanceManager.RestartInstance(uuid)
instance, err := h.InstanceManager.RestartInstance(name)
if err != nil {
http.Error(w, "Failed to restart instance: "+err.Error(), http.StatusInternalServerError)
return
@@ -262,24 +314,23 @@ func (h *Handler) RestartInstance() http.HandlerFunc {
// DeleteInstance godoc
// @Summary Delete an instance
// @Description Stops and removes a specific instance by ID
// @Description Stops and removes a specific instance by name
// @Tags instances
// @Produce json
// @Param id path string true "Instance ID"
// @Param name path string true "Instance Name"
// @Success 204 "No Content"
// @Failure 400 {string} string "Invalid UUID format"
// @Failure 400 {string} string "Invalid name format"
// @Failure 500 {string} string "Internal Server Error"
// @Router /instances/{id} [delete]
// @Router /instances/{name} [delete]
func (h *Handler) DeleteInstance() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
uuid, err := uuid.Parse(id)
if err != nil {
http.Error(w, "Invalid UUID format", http.StatusBadRequest)
name := chi.URLParam(r, "name")
if name == "" {
http.Error(w, "Instance name cannot be empty", http.StatusBadRequest)
return
}
if err := h.InstanceManager.DeleteInstance(uuid); err != nil {
if err := h.InstanceManager.DeleteInstance(name); err != nil {
http.Error(w, "Failed to delete instance: "+err.Error(), http.StatusInternalServerError)
return
}