Apply candidate config from preview UI

This commit is contained in:
tian 2026-04-19 12:23:27 +08:00
parent e703dacce6
commit 5d3948250d
6 changed files with 66 additions and 0 deletions

View File

@ -74,6 +74,7 @@ func main() {
r.Get("/devices/{id}/info", h.ProxyAgent)
r.Get("/devices/{id}/config/status", h.ProxyAgent)
r.Put("/devices/{id}/config/candidate", h.ProxyAgent)
r.Post("/devices/{id}/config/candidate/apply", h.ProxyAgent)
r.Post("/devices/{id}/reload", h.ProxyAgent)
r.Post("/devices/{id}/rollback", h.ProxyAgent)
r.Get("/devices/{id}/graphs", h.ProxyAgent)

View File

@ -132,6 +132,9 @@ func (h *Handler) ProxyAgent(w http.ResponseWriter, r *http.Request) {
case r.URL.Path == fmt.Sprintf("/api/devices/%s/config/candidate", id):
agentPath = "/v1/config/candidate"
method = "PUT"
case r.URL.Path == fmt.Sprintf("/api/devices/%s/config/candidate/apply", id):
agentPath = "/v1/config/candidate/apply"
method = "POST"
case r.URL.Path == fmt.Sprintf("/api/devices/%s/reload", id):
agentPath = "/v1/media-server/reload"
method = "POST"

View File

@ -184,3 +184,44 @@ func TestHandler_ProxyAgentMapsConfigCandidate(t *testing.T) {
t.Fatalf("expected candidate response, got %s", rr.Body.String())
}
}
func TestHandler_ProxyAgentMapsConfigCandidateApply(t *testing.T) {
agentServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Fatalf("expected POST, got %s", r.Method)
}
if r.URL.Path != "/v1/config/candidate/apply" {
t.Fatalf("expected /v1/config/candidate/apply, got %s", r.URL.Path)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"ok":true,"status":{"metadata":{"config_id":"candidate"}}}`))
}))
defer agentServer.Close()
host, portText, err := net.SplitHostPort(strings.TrimPrefix(agentServer.URL, "http://"))
if err != nil {
t.Fatalf("parse test server address: %v", err)
}
port, err := strconv.Atoi(portText)
if err != nil {
t.Fatalf("parse test server port: %v", err)
}
cfg := &config.Config{}
agent := service.NewAgentClient(cfg)
reg := service.NewRegistryService(cfg, agent)
reg.UpdateDevice(&models.Device{DeviceID: "edge-01", IP: host, AgentPort: port})
h := NewHandler(nil, reg, agent, nil, nil)
r := chi.NewRouter()
r.Post("/api/devices/{id}/config/candidate/apply", h.ProxyAgent)
rr := httptest.NewRecorder()
r.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/api/devices/edge-01/config/candidate/apply", nil))
if rr.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d: %s", rr.Code, rr.Body.String())
}
if !strings.Contains(rr.Body.String(), "candidate") {
t.Fatalf("expected candidate apply response, got %s", rr.Body.String())
}
}

View File

@ -182,6 +182,7 @@ func (u *UI) Routes() (chi.Router, error) {
r.Get("/devices/{id}/config-preview", u.pageDeviceConfigPreview)
r.Post("/devices/{id}/config-preview", u.actionDeviceConfigPreview)
r.Post("/devices/{id}/config-candidate", u.actionDeviceConfigCandidate)
r.Post("/devices/{id}/config-candidate/apply", u.actionDeviceConfigCandidateApply)
r.Post("/devices/{id}/config-ui/plan", u.actionDeviceConfigUIPlan)
r.Post("/devices/{id}/config-ui/apply", u.actionDeviceConfigUIApply)
r.Post("/devices/{id}/face-gallery/upload", u.actionDeviceFaceGalleryUpload)
@ -906,6 +907,23 @@ func (u *UI) actionDeviceConfigCandidate(w http.ResponseWriter, r *http.Request)
u.render(w, r, "config_preview", data)
}
func (u *UI) actionDeviceConfigCandidateApply(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
dev, ok := u.findDevice(id)
if !ok {
http.NotFound(w, r)
return
}
data := u.configPreviewPageData(dev)
body, code, err := u.agent.Do("POST", dev.IP, dev.AgentPort, "/v1/config/candidate/apply", []byte(`{}`))
data.Message = fmt.Sprintf("POST /v1/config/candidate/apply -> %d", code)
data.RawText = prettyJSON(body)
if err != nil {
data.Error = err.Error()
}
u.render(w, r, "config_preview", data)
}
func (u *UI) configPreviewPageData(dev *models.Device) PageData {
sources, err := u.preview.ListSources()
data := PageData{Title: "配置预览", Device: dev, ConfigSources: sources}

View File

@ -54,6 +54,7 @@
<div class="actions" style="margin-top:12px">
<button type="submit">生成预览</button>
<button type="button" disabled>上传为候选配置</button>
<button type="button" disabled>应用候选配置</button>
<a class="btn ghost" href="/ui/devices/{{.Device.DeviceID}}">查看当前运行配置</a>
</div>
</form>
@ -80,6 +81,7 @@
<form method="post" action="/ui/devices/{{.Device.DeviceID}}/config-candidate" class="actions" style="margin-bottom:10px">
<input type="hidden" name="json" value="{{.ConfigPreview.JSON}}" />
<button type="submit">上传为候选配置</button>
<button type="submit" formaction="/ui/devices/{{.Device.DeviceID}}/config-candidate/apply">应用候选配置</button>
<a class="btn ghost" href="/ui/devices/{{.Device.DeviceID}}">查看当前运行配置</a>
</form>
<pre>{{.ConfigPreview.JSON}}</pre>

View File

@ -332,6 +332,7 @@ func TestUI_ConfigPreviewPageShowsTemplateProfileOverlayForm(t *testing.T) {
"local_3588_test",
"face_debug",
"上传为候选配置",
"应用候选配置",
} {
if !strings.Contains(body, want) {
t.Fatalf("expected config preview page to contain %q, got:\n%s", want, body)