Upload preview config as candidate
This commit is contained in:
parent
c9e3698008
commit
e703dacce6
@ -73,6 +73,7 @@ func main() {
|
||||
// Proxy routes for device actions
|
||||
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}/reload", h.ProxyAgent)
|
||||
r.Post("/devices/{id}/rollback", h.ProxyAgent)
|
||||
r.Get("/devices/{id}/graphs", h.ProxyAgent)
|
||||
|
||||
@ -129,6 +129,9 @@ func (h *Handler) ProxyAgent(w http.ResponseWriter, r *http.Request) {
|
||||
agentPath = "/v1/info"
|
||||
case r.URL.Path == fmt.Sprintf("/api/devices/%s/config/status", id):
|
||||
agentPath = "/v1/config/status"
|
||||
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/reload", id):
|
||||
agentPath = "/v1/media-server/reload"
|
||||
method = "POST"
|
||||
|
||||
@ -143,3 +143,44 @@ func TestHandler_ProxyAgentMapsConfigStatus(t *testing.T) {
|
||||
t.Fatalf("expected config status response, got %s", rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandler_ProxyAgentMapsConfigCandidate(t *testing.T) {
|
||||
agentServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPut {
|
||||
t.Fatalf("expected PUT, got %s", r.Method)
|
||||
}
|
||||
if r.URL.Path != "/v1/config/candidate" {
|
||||
t.Fatalf("expected /v1/config/candidate, got %s", r.URL.Path)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"ok":true,"path":"/tmp/media-server.json.candidate.json"}`))
|
||||
}))
|
||||
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.Put("/api/devices/{id}/config/candidate", h.ProxyAgent)
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, httptest.NewRequest(http.MethodPut, "/api/devices/edge-01/config/candidate", strings.NewReader(`{"metadata":{"config_id":"preview"},"templates":{"t":{}},"instances":[]}`)))
|
||||
|
||||
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 response, got %s", rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
@ -181,6 +181,7 @@ func (u *UI) Routes() (chi.Router, error) {
|
||||
r.Get("/devices/{id}/config-friendly", u.pageDeviceConfigFriendly)
|
||||
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-ui/plan", u.actionDeviceConfigUIPlan)
|
||||
r.Post("/devices/{id}/config-ui/apply", u.actionDeviceConfigUIApply)
|
||||
r.Post("/devices/{id}/face-gallery/upload", u.actionDeviceFaceGalleryUpload)
|
||||
@ -876,6 +877,35 @@ func (u *UI) actionDeviceConfigPreview(w http.ResponseWriter, r *http.Request) {
|
||||
u.render(w, r, "config_preview", data)
|
||||
}
|
||||
|
||||
func (u *UI) actionDeviceConfigCandidate(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "id")
|
||||
dev, ok := u.findDevice(id)
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
_ = r.ParseForm()
|
||||
raw := strings.TrimSpace(r.FormValue("json"))
|
||||
data := u.configPreviewPageData(dev)
|
||||
if raw == "" {
|
||||
data.Error = "候选配置 JSON 不能为空"
|
||||
u.render(w, r, "config_preview", data)
|
||||
return
|
||||
}
|
||||
if err := json.Unmarshal([]byte(raw), new(any)); err != nil {
|
||||
data.Error = "候选配置 JSON 无效: " + err.Error()
|
||||
u.render(w, r, "config_preview", data)
|
||||
return
|
||||
}
|
||||
body, code, err := u.agent.Do("PUT", dev.IP, dev.AgentPort, "/v1/config/candidate", []byte(raw))
|
||||
data.Message = fmt.Sprintf("PUT /v1/config/candidate -> %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}
|
||||
|
||||
@ -53,6 +53,7 @@
|
||||
</div>
|
||||
<div class="actions" style="margin-top:12px">
|
||||
<button type="submit">生成预览</button>
|
||||
<button type="button" disabled>上传为候选配置</button>
|
||||
<a class="btn ghost" href="/ui/devices/{{.Device.DeviceID}}">查看当前运行配置</a>
|
||||
</div>
|
||||
</form>
|
||||
@ -76,7 +77,19 @@
|
||||
|
||||
<div class="card">
|
||||
<h2>完整 JSON</h2>
|
||||
<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>
|
||||
<a class="btn ghost" href="/ui/devices/{{.Device.DeviceID}}">查看当前运行配置</a>
|
||||
</form>
|
||||
<pre>{{.ConfigPreview.JSON}}</pre>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
{{if .RawText}}
|
||||
<div class="card">
|
||||
<h2>上传结果</h2>
|
||||
<pre>{{.RawText}}</pre>
|
||||
</div>
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
@ -331,6 +331,7 @@ func TestUI_ConfigPreviewPageShowsTemplateProfileOverlayForm(t *testing.T) {
|
||||
"workshop_face_shoe_alarm",
|
||||
"local_3588_test",
|
||||
"face_debug",
|
||||
"上传为候选配置",
|
||||
} {
|
||||
if !strings.Contains(body, want) {
|
||||
t.Fatalf("expected config preview page to contain %q, got:\n%s", want, body)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user