73776f37eb
* Alerting: Introduce a Mimir client as part of the Remote Alertmanager Mimir client that understands the new APIs developed for mimir. Very much a WIP still. * more wip * appease the linter * more linting * add more code * get state from kvstore, encode, send * send state to the remote Alertmanager, extract fullstate logic into its own function * pass kvstore to remote.NewAlertmanager() * refactor * add fake kvstore to tests * tests * use FileStore to get state * always log 'completed state upload' * refactor compareRemoteConfig * base64-encode the state in the file store * export silences and nflog filenames, refactor * log 'completed state/config upload...' regardless of outcome * add values to the state store in tests * address code review comments * log error from filestore --------- Co-authored-by: gotjosh <josue.abreu@gmail.com>
60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
const (
|
|
grafanaAlertmanagerConfigPath = "/api/v1/grafana/config"
|
|
)
|
|
|
|
type UserGrafanaConfig struct {
|
|
ID int64 `json:"id"`
|
|
GrafanaAlertmanagerConfig string `json:"configuration"`
|
|
Hash string `json:"configuration_hash"`
|
|
CreatedAt int64 `json:"created"`
|
|
Default bool `json:"default"`
|
|
}
|
|
|
|
func (mc *Mimir) GetGrafanaAlertmanagerConfig(ctx context.Context) (*UserGrafanaConfig, error) {
|
|
gc := &UserGrafanaConfig{}
|
|
response := successResponse{
|
|
Data: gc,
|
|
}
|
|
// nolint:bodyclose
|
|
// closed within `do`
|
|
_, err := mc.do(ctx, grafanaAlertmanagerConfigPath, http.MethodGet, nil, &response)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if response.Status != "success" {
|
|
return nil, fmt.Errorf("returned non-success `status` from the MimirAPI: %s", response.Status)
|
|
}
|
|
|
|
return gc, nil
|
|
}
|
|
|
|
func (mc *Mimir) CreateGrafanaAlertmanagerConfig(ctx context.Context, cfg, hash string, id, createdAt int64, isDefault bool) error {
|
|
payload, err := json.Marshal(&UserGrafanaConfig{
|
|
ID: id,
|
|
GrafanaAlertmanagerConfig: cfg,
|
|
Hash: hash,
|
|
CreatedAt: createdAt,
|
|
Default: isDefault,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return mc.doOK(ctx, grafanaAlertmanagerConfigPath, http.MethodPost, bytes.NewBuffer(payload))
|
|
}
|
|
|
|
func (mc *Mimir) DeleteGrafanaAlertmanagerConfig(ctx context.Context) error {
|
|
return mc.doOK(ctx, grafanaAlertmanagerConfigPath, http.MethodDelete, nil)
|
|
}
|