feat: complete MVP epics E7-E10 (provisioning, console proxy, audit, frontend)

This commit is contained in:
Philipp
2026-06-12 10:45:13 +02:00
parent 137c13fa98
commit ac30a19960
33 changed files with 3625 additions and 263 deletions
+189
View File
@@ -0,0 +1,189 @@
package template
import (
"context"
"encoding/json"
"net/http"
"strings"
)
type Repository interface {
List(ctx context.Context) ([]Template, error)
Get(ctx context.Context, id string) (Template, bool, error)
Upsert(ctx context.Context, t Template) (Template, error)
Delete(ctx context.Context, id string) (bool, error)
}
type Handler struct {
repository Repository
}
func NewHandler(repository Repository) Handler {
return Handler{repository: repository}
}
func (h Handler) ListTemplates(w http.ResponseWriter, r *http.Request) {
templates, err := h.repository.List(r.Context())
if err != nil {
writeError(w, http.StatusInternalServerError, "templates_list_failed")
return
}
writeJSON(w, http.StatusOK, map[string][]Template{"data": templates})
}
func (h Handler) CreateTemplate(w http.ResponseWriter, r *http.Request) {
var request createTemplateRequest
if !decodeJSON(w, r, &request) {
return
}
request.Name = strings.TrimSpace(request.Name)
if request.Name == "" {
writeError(w, http.StatusBadRequest, "name_required")
return
}
if request.ClusterID == "" {
writeError(w, http.StatusBadRequest, "cluster_id_required")
return
}
if request.ProxmoxTemplateVMID <= 0 {
writeError(w, http.StatusBadRequest, "proxmox_template_vmid_required")
return
}
saved, err := h.repository.Upsert(r.Context(), Template{
ClusterID: request.ClusterID,
Name: request.Name,
Description: request.Description,
ProxmoxTemplateVMID: request.ProxmoxTemplateVMID,
ProxmoxNode: request.ProxmoxNode,
})
if err != nil {
writeError(w, http.StatusBadRequest, "template_upsert_failed")
return
}
writeJSON(w, http.StatusCreated, saved)
}
func (h Handler) UpdateTemplate(w http.ResponseWriter, r *http.Request) {
templateID := r.PathValue("templateID")
if templateID == "" {
writeError(w, http.StatusBadRequest, "template_id_required")
return
}
existing, found, err := h.repository.Get(r.Context(), templateID)
if err != nil {
writeError(w, http.StatusInternalServerError, "template_get_failed")
return
}
if !found {
writeError(w, http.StatusNotFound, "template_not_found")
return
}
var request updateTemplateRequest
if !decodeJSON(w, r, &request) {
return
}
if request.ClusterID == nil {
request.ClusterID = &existing.ClusterID
}
if request.Name == nil {
request.Name = &existing.Name
} else {
trimmed := strings.TrimSpace(*request.Name)
request.Name = &trimmed
if *request.Name == "" {
writeError(w, http.StatusBadRequest, "name_required")
return
}
}
if request.Description == nil {
request.Description = &existing.Description
}
if request.ProxmoxTemplateVMID == nil {
request.ProxmoxTemplateVMID = &existing.ProxmoxTemplateVMID
} else if *request.ProxmoxTemplateVMID <= 0 {
writeError(w, http.StatusBadRequest, "proxmox_template_vmid_required")
return
}
if request.ProxmoxNode == nil {
request.ProxmoxNode = &existing.ProxmoxNode
}
saved, err := h.repository.Upsert(r.Context(), Template{
ID: existing.ID,
ClusterID: *request.ClusterID,
Name: *request.Name,
Description: *request.Description,
ProxmoxTemplateVMID: *request.ProxmoxTemplateVMID,
ProxmoxNode: *request.ProxmoxNode,
})
if err != nil {
writeError(w, http.StatusBadRequest, "template_upsert_failed")
return
}
writeJSON(w, http.StatusOK, saved)
}
func (h Handler) DeleteTemplate(w http.ResponseWriter, r *http.Request) {
templateID := r.PathValue("templateID")
if templateID == "" {
writeError(w, http.StatusBadRequest, "template_id_required")
return
}
deleted, err := h.repository.Delete(r.Context(), templateID)
if err != nil {
writeError(w, http.StatusInternalServerError, "template_delete_failed")
return
}
if !deleted {
writeError(w, http.StatusNotFound, "template_not_found")
return
}
w.WriteHeader(http.StatusNoContent)
}
type createTemplateRequest struct {
ClusterID string `json:"cluster_id"`
Name string `json:"name"`
Description string `json:"description"`
ProxmoxTemplateVMID int `json:"proxmox_template_vmid"`
ProxmoxNode string `json:"proxmox_node"`
}
type updateTemplateRequest struct {
ClusterID *string `json:"cluster_id"`
Name *string `json:"name"`
Description *string `json:"description"`
ProxmoxTemplateVMID *int `json:"proxmox_template_vmid"`
ProxmoxNode *string `json:"proxmox_node"`
}
func decodeJSON(w http.ResponseWriter, r *http.Request, target any) bool {
defer r.Body.Close()
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(target); err != nil {
writeError(w, http.StatusBadRequest, "invalid_json")
return false
}
return true
}
func writeJSON(w http.ResponseWriter, status int, body any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(body)
}
func writeError(w http.ResponseWriter, status int, message string) {
writeJSON(w, status, map[string]string{"error": message})
}
+208
View File
@@ -0,0 +1,208 @@
package template
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestListTemplatesReturnsTemplates(t *testing.T) {
repository := &stubRepository{
templates: []Template{{
ID: "tmpl-1",
ClusterID: "cluster-1",
Name: "ubuntu-24.04",
Description: "Ubuntu 24.04 LTS",
ProxmoxTemplateVMID: 9000,
ProxmoxNode: "pve1",
CreatedAt: time.Date(2026, 6, 11, 12, 0, 0, 0, time.UTC),
}},
}
handler := NewHandler(repository)
req := httptest.NewRequest(http.MethodGet, "/internal/templates", nil)
rec := httptest.NewRecorder()
handler.ListTemplates(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
}
var response struct {
Data []Template `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &response); err != nil {
t.Fatalf("unmarshal response: %v", err)
}
if len(response.Data) != 1 {
t.Fatalf("len(data) = %d, want 1", len(response.Data))
}
if response.Data[0].ID != "tmpl-1" {
t.Fatalf("template ID = %q, want tmpl-1", response.Data[0].ID)
}
}
func TestListTemplatesReturnsEmptyList(t *testing.T) {
handler := NewHandler(&stubRepository{templates: []Template{}})
req := httptest.NewRequest(http.MethodGet, "/internal/templates", nil)
rec := httptest.NewRecorder()
handler.ListTemplates(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
}
}
func TestCreateTemplateReturnsCreated(t *testing.T) {
repository := &stubRepository{
upsertResult: Template{
ID: "tmpl-1",
ClusterID: "cluster-1",
Name: "ubuntu-24.04",
Description: "Ubuntu 24.04 LTS",
ProxmoxTemplateVMID: 9000,
ProxmoxNode: "pve1",
},
}
handler := NewHandler(repository)
body := `{"cluster_id":"cluster-1","name":"ubuntu-24.04","description":"Ubuntu 24.04 LTS","proxmox_template_vmid":9000,"proxmox_node":"pve1"}`
req := httptest.NewRequest(http.MethodPost, "/internal/templates", io.NopCloser(strings.NewReader(body)))
rec := httptest.NewRecorder()
handler.CreateTemplate(rec, req)
if rec.Code != http.StatusCreated {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusCreated)
}
var response Template
if err := json.Unmarshal(rec.Body.Bytes(), &response); err != nil {
t.Fatalf("unmarshal response: %v", err)
}
if response.ID != "tmpl-1" {
t.Fatalf("template ID = %q, want tmpl-1", response.ID)
}
}
func TestCreateTemplateRejectsEmptyName(t *testing.T) {
handler := NewHandler(&stubRepository{})
body := `{"cluster_id":"cluster-1","name":"","proxmox_template_vmid":9000}`
req := httptest.NewRequest(http.MethodPost, "/internal/templates", io.NopCloser(strings.NewReader(body)))
rec := httptest.NewRecorder()
handler.CreateTemplate(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadRequest)
}
}
func TestUpdateTemplateReturnsUpdated(t *testing.T) {
repository := &stubRepository{
getFound: true,
getResult: Template{
ID: "tmpl-1",
ClusterID: "cluster-1",
Name: "ubuntu-24.04",
Description: "Ubuntu 24.04 LTS",
ProxmoxTemplateVMID: 9000,
ProxmoxNode: "pve1",
},
upsertResult: Template{
ID: "tmpl-1",
ClusterID: "cluster-1",
Name: "ubuntu-24.04-updated",
Description: "Updated",
ProxmoxTemplateVMID: 9000,
ProxmoxNode: "pve1",
},
}
handler := NewHandler(repository)
body := `{"name":"ubuntu-24.04-updated","description":"Updated"}`
req := httptest.NewRequest(http.MethodPut, "/internal/templates/tmpl-1", io.NopCloser(strings.NewReader(body)))
req.SetPathValue("templateID", "tmpl-1")
rec := httptest.NewRecorder()
handler.UpdateTemplate(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
}
var response Template
if err := json.Unmarshal(rec.Body.Bytes(), &response); err != nil {
t.Fatalf("unmarshal response: %v", err)
}
if response.Name != "ubuntu-24.04-updated" {
t.Fatalf("name = %q, want ubuntu-24.04-updated", response.Name)
}
}
func TestUpdateTemplateReturnsNotFound(t *testing.T) {
handler := NewHandler(&stubRepository{getFound: false})
body := `{"name":"updated"}`
req := httptest.NewRequest(http.MethodPut, "/internal/templates/tmpl-xxx", io.NopCloser(strings.NewReader(body)))
req.SetPathValue("templateID", "tmpl-xxx")
rec := httptest.NewRecorder()
handler.UpdateTemplate(rec, req)
if rec.Code != http.StatusNotFound {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusNotFound)
}
}
func TestDeleteTemplateReturnsNoContent(t *testing.T) {
handler := NewHandler(&stubRepository{deleteResult: true})
req := httptest.NewRequest(http.MethodDelete, "/internal/templates/tmpl-1", nil)
req.SetPathValue("templateID", "tmpl-1")
rec := httptest.NewRecorder()
handler.DeleteTemplate(rec, req)
if rec.Code != http.StatusNoContent {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusNoContent)
}
}
func TestDeleteTemplateReturnsNotFound(t *testing.T) {
handler := NewHandler(&stubRepository{deleteResult: false})
req := httptest.NewRequest(http.MethodDelete, "/internal/templates/tmpl-xxx", nil)
req.SetPathValue("templateID", "tmpl-xxx")
rec := httptest.NewRecorder()
handler.DeleteTemplate(rec, req)
if rec.Code != http.StatusNotFound {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusNotFound)
}
}
type stubRepository struct {
templates []Template
getFound bool
getResult Template
upsertResult Template
deleteResult bool
}
func (s *stubRepository) List(_ context.Context) ([]Template, error) {
return s.templates, nil
}
func (s *stubRepository) Get(_ context.Context, _ string) (Template, bool, error) {
return s.getResult, s.getFound, nil
}
func (s *stubRepository) Upsert(_ context.Context, _ Template) (Template, error) {
return s.upsertResult, nil
}
func (s *stubRepository) Delete(_ context.Context, _ string) (bool, error) {
return s.deleteResult, nil
}
+113
View File
@@ -0,0 +1,113 @@
package template
import (
"context"
"database/sql"
"errors"
"time"
)
type SQLRepository struct {
db *sql.DB
}
func NewSQLRepository(db *sql.DB) SQLRepository {
return SQLRepository{db: db}
}
func (r SQLRepository) List(ctx context.Context) ([]Template, error) {
rows, err := r.db.QueryContext(ctx, `
select
id::text,
cluster_id::text,
name,
description,
proxmox_template_vmid,
proxmox_node,
created_at
from public.templates
order by name asc
`)
if err != nil {
return nil, err
}
defer rows.Close()
var templates []Template
for rows.Next() {
var t Template
if err := rows.Scan(&t.ID, &t.ClusterID, &t.Name, &t.Description, &t.ProxmoxTemplateVMID, &t.ProxmoxNode, &t.CreatedAt); err != nil {
return nil, err
}
templates = append(templates, t)
}
if err := rows.Err(); err != nil {
return nil, err
}
return templates, nil
}
func (r SQLRepository) Get(ctx context.Context, id string) (Template, bool, error) {
var t Template
err := r.db.QueryRowContext(ctx, `
select
id::text,
cluster_id::text,
name,
description,
proxmox_template_vmid,
proxmox_node,
created_at
from public.templates
where id = $1
`, id).Scan(&t.ID, &t.ClusterID, &t.Name, &t.Description, &t.ProxmoxTemplateVMID, &t.ProxmoxNode, &t.CreatedAt)
if errors.Is(err, sql.ErrNoRows) {
return Template{}, false, nil
}
if err != nil {
return Template{}, false, err
}
return t, true, nil
}
func (r SQLRepository) Upsert(ctx context.Context, t Template) (Template, error) {
err := r.db.QueryRowContext(ctx, `
insert into public.templates (cluster_id, name, description, proxmox_template_vmid, proxmox_node)
values ($1, $2, $3, $4, $5)
on conflict (cluster_id, proxmox_node, proxmox_template_vmid)
do update set name = $2, description = $3, updated_at = now()
returning id::text, name, description, proxmox_template_vmid, proxmox_node, created_at, coalesce(updated_at, created_at)
`, t.ClusterID, t.Name, t.Description, t.ProxmoxTemplateVMID, t.ProxmoxNode).Scan(
&t.ID, &t.Name, &t.Description, &t.ProxmoxTemplateVMID, &t.ProxmoxNode, &t.CreatedAt, &t.UpdatedAt,
)
if err != nil {
return Template{}, err
}
return t, nil
}
func (r SQLRepository) Delete(ctx context.Context, id string) (bool, error) {
res, err := r.db.ExecContext(ctx, `delete from public.templates where id = $1`, id)
if err != nil {
return false, err
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return false, err
}
return rowsAffected > 0, nil
}
type Template struct {
ID string `json:"id"`
ClusterID string `json:"cluster_id"`
Name string `json:"name"`
Description string `json:"description"`
ProxmoxTemplateVMID int `json:"proxmox_template_vmid"`
ProxmoxNode string `json:"proxmox_node"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
+33
View File
@@ -0,0 +1,33 @@
package template
import (
"context"
"proxui/backend/internal/vm"
)
type ProvisionResolver struct {
repository Repository
}
func NewProvisionResolver(repository Repository) ProvisionResolver {
return ProvisionResolver{repository: repository}
}
func (r ProvisionResolver) GetTemplate(ctx context.Context, templateID string) (vm.TemplateInfo, bool, error) {
t, found, err := r.repository.Get(ctx, templateID)
if err != nil {
return vm.TemplateInfo{}, false, err
}
if !found {
return vm.TemplateInfo{}, false, nil
}
return vm.TemplateInfo{
ID: t.ID,
ClusterID: t.ClusterID,
Name: t.Name,
ProxmoxTemplateVMID: t.ProxmoxTemplateVMID,
ProxmoxNode: t.ProxmoxNode,
}, true, nil
}