Skip to content

Commit

Permalink
Add support to fetch config from controller release 3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
corny committed Oct 23, 2024
1 parent d5cac1a commit a3bcb3c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version: 2
builds:
- binary: triax-eoc-exporter
main: ./cmd
Expand Down
7 changes: 7 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
58 changes: 25 additions & 33 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand All @@ -143,7 +135,7 @@ var tmpl = template.Must(template.New("index").Option("missingkey=error").Parse(
<dt>{{.Alias}}</dt>
<dd>
<a href="/controllers/{{.Alias}}/metrics">Metrics</a>,
<a href="/controllers/{{.Alias}}/api/node/status/">Status</a>
<a href="/controllers/{{.Alias}}/config">Config</a>
</dd>
{{end}}
</dl>
Expand Down

0 comments on commit a3bcb3c

Please sign in to comment.