diff --git a/.goreleaser.yml b/.goreleaser.yml index 163935a..ccacdb6 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,3 +1,4 @@ +version: 2 builds: - binary: triax-eoc-exporter main: ./cmd diff --git a/client/client.go b/client/client.go index f18905c..406158c 100644 --- a/client/client.go +++ b/client/client.go @@ -63,6 +63,13 @@ func (c *Client) Get(ctx context.Context, path string, res interface{}) error { return c.ApiRequest(ctx, http.MethodGet, path, nil, res) } +// GetConfig fetches the configuration from the controller +func (c *Client) GetConfig(ctx context.Context) (json.RawMessage, error) { + msg := json.RawMessage{} + err := c.Get(ctx, "/cgi.lua/config", &msg) + return msg, err +} + func (c *Client) SetCookie(nameAndValue string) { i := strings.Index(nameAndValue, "=") if i <= 0 { diff --git a/exporter/exporter.go b/exporter/exporter.go index e46d724..ba011c1 100644 --- a/exporter/exporter.go +++ b/exporter/exporter.go @@ -27,8 +27,8 @@ func (cfg *Config) Start(listenAddress, version string) { router.GET("/controllers", cfg.listControllersHandler) router.GET("/controllers/:target/metrics", cfg.targetMiddleware(cfg.metricsHandler)) - router.GET("/controllers/:target/api/*path", cfg.targetMiddleware(cfg.apiHandler)) - //router.PUT("/controllers/:target/nodes/:mac", cfg.targetMiddleware(cfg.updateNodeHandler)) + router.GET("/controllers/:target/config", cfg.targetMiddleware(cfg.getConfigHandler)) + router.POST("/controllers/:target/config", cfg.targetMiddleware(cfg.updateConfigHandler)) slog.Info("Starting exporter", "listenAddress", listenAddress) slog.Info("Server stopped", "reason", http.ListenAndServe(listenAddress, router)) @@ -76,49 +76,41 @@ func (cfg *Config) metricsHandler(client *client.Client, w http.ResponseWriter, h.ServeHTTP(w, r) } -/* -// handler for updating nodes -func (cfg *Config) updateNodeHandler(client *client.Client, w http.ResponseWriter, r *http.Request, params httprouter.Params) { - defer r.Body.Close() +// handler for updating configs - // parse MAC address parameter - mac, err := net.ParseMAC(params.ByName("mac")) - if err != nil { - http.Error(w, fmt.Sprintf("invalid MAC address: %s", err), http.StatusBadRequest) - return - } +func (cfg *Config) updateConfigHandler(client *client.Client, w http.ResponseWriter, r *http.Request, params httprouter.Params) { + defer r.Body.Close() + /* - // decode request body - req := triax.UpdateRequest{} - err = json.NewDecoder(r.Body).Decode(&req) - if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return - } + // decode request body + req := triax.UpdateRequest{} + err = json.NewDecoder(r.Body).Decode(&req) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } - // execute update - err = client.UpdateNode(r.Context(), mac, req) - if err != nil { - http.Error(w, err.Error(), http.StatusBadGateway) - return - } + // execute update + err = client.UpdateNode(r.Context(), mac, req) + if err != nil { + http.Error(w, err.Error(), http.StatusBadGateway) + return + } + */ w.WriteHeader(http.StatusNoContent) } -*/ -// proxy handler for API GET requests. -func (cfg *Config) apiHandler(client *client.Client, w http.ResponseWriter, r *http.Request, params httprouter.Params) { - defer r.Body.Close() +// handler for getting configs +func (cfg *Config) getConfigHandler(client *client.Client, w http.ResponseWriter, r *http.Request, params httprouter.Params) { + config, err := client.GetConfig(r.Context()) - msg := json.RawMessage{} - err := client.Get(r.Context(), params.ByName("path"), &msg) if err != nil { http.Error(w, err.Error(), http.StatusBadGateway) return } - io.Copy(w, bytes.NewReader(msg)) + io.Copy(w, bytes.NewReader(config)) } type indexVariables struct { @@ -143,7 +135,7 @@ var tmpl = template.Must(template.New("index").Option("missingkey=error").Parse(