From ac5f6bcf8e699401076b5ee635a07d29d7917373 Mon Sep 17 00:00:00 2001 From: esoulard Date: Mon, 14 Oct 2024 14:10:37 +0200 Subject: [PATCH 1/2] fix: add missing prometheus config to AutomationConfig --- opsmngr/automation_config.go | 15 ++++ opsmngr/automation_config_test.go | 123 ++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) diff --git a/opsmngr/automation_config.go b/opsmngr/automation_config.go index c1e05a7..9249192 100644 --- a/opsmngr/automation_config.go +++ b/opsmngr/automation_config.go @@ -88,6 +88,7 @@ type AutomationConfig struct { Mongots []*map[string]interface{} `json:"mongots"` Options *map[string]interface{} `json:"options"` Processes []*Process `json:"processes"` + Prometheus *Prometheus `json:"prometheus,omitempty"` ReplicaSets []*ReplicaSet `json:"replicaSets"` Roles []*map[string]interface{} `json:"roles"` Sharding []*ShardingConfig `json:"sharding"` @@ -442,3 +443,17 @@ type Process struct { ProcessType string `json:"processType,omitempty"` Version string `json:"version,omitempty"` } + +type Prometheus struct { + Enabled bool `json:"enabled"` + Username string `json:"username"` + Password string `json:"password,omitempty"` + PasswordHash string `json:"passwordHash,omitempty"` + PasswordSalt string `json:"passwordSalt,omitempty"` + Scheme string `json:"scheme"` + TLSPemPath string `json:"tlsPemPath"` + TLSPemPassword string `json:"tlsPemPassword"` + Mode string `json:"mode"` + ListenAddress string `json:"listenAddress"` + MetricsPath string `json:"metricsPath"` +} diff --git a/opsmngr/automation_config_test.go b/opsmngr/automation_config_test.go index 317244e..6a72337 100644 --- a/opsmngr/automation_config_test.go +++ b/opsmngr/automation_config_test.go @@ -848,3 +848,126 @@ func TestAutomation_Sharding(t *testing.T) { t.Error(diff) } } + +func TestAutomation_GetPrometheusConfig(t *testing.T) { + client, mux, teardown := setup() + defer teardown() + + mux.HandleFunc(fmt.Sprintf("/api/public/v1.0/groups/%s/automationConfig", projectID), func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodGet) + _, _ = fmt.Fprint(w, `{ + "prometheus" : { + "enabled": true, + "listenAddress": "0.0.0.0:9216", + "metricsPath": "/metrics", + "mode": "opsManager", + "passwordHash": "", + "passwordSalt": "", + "scheme": "http", + "tlsPemPassword": "", + "tlsPemPath": "", + "username": "user" + }}`) + }) + + config, _, err := client.Automation.GetConfig(ctx, projectID) + if err != nil { + t.Fatalf("Automation.GetConfig returned error: %v", err) + } + + expected := &AutomationConfig{ + Prometheus: &Prometheus{ + Enabled: true, + ListenAddress: "0.0.0.0:9216", + MetricsPath: "/metrics", + Mode: "opsManager", + Scheme: "http", + Username: "user", + }, + } + if diff := deep.Equal(config, expected); diff != nil { + t.Error(diff) + } +} + +func TestAutomation_UpdatePrometheusConfig(t *testing.T) { + client, mux, teardown := setup() + defer teardown() + + updateRequest := &AutomationConfig{ + Prometheus: &Prometheus{ + Enabled: true, + ListenAddress: "0.0.0.0:9216", + MetricsPath: "/metrics", + Mode: "opsManager", + Scheme: "http", + Username: "user", + }, + Auth: Auth{ + AuthoritativeSet: false, + AutoAuthMechanism: "MONGODB-CR", + Disabled: true, + UsersWanted: []*MongoDBUser{ + { + Database: "admin", + }, + }, + }, + } + mux.HandleFunc(fmt.Sprintf("/api/public/v1.0/groups/%s/automationConfig", projectID), func(w http.ResponseWriter, r *http.Request) { + expected := map[string]interface{}{ + "prometheus": map[string]interface{}{ + "enabled": true, + "listenAddress": "0.0.0.0:9216", + "metricsPath": "/metrics", + "mode": "opsManager", + "scheme": "http", + "tlsPemPassword": "", + "tlsPemPath": "", + "username": "user", + }, + "auth": map[string]interface{}{ + "authoritativeSet": false, + "autoAuthMechanism": "MONGODB-CR", + "autoAuthRestrictions": interface{}(nil), + "disabled": true, + "usersDeleted": interface{}(nil), + "usersWanted": []interface{}{ + map[string]interface{}{ + "authenticationRestrictions": interface{}(nil), + "db": "admin", + "roles": interface{}(nil), + "user": "", + }, + }, + }, + "backupVersions": interface{}(nil), + "balancer": interface{}(nil), + "cpsModules": interface{}(nil), + "indexConfigs": interface{}(nil), + "mongosqlds": interface{}(nil), + "mongots": interface{}(nil), + "onlineArchiveModules": interface{}(nil), + "options": interface{}(nil), + "processes": interface{}(nil), + "replicaSets": interface{}(nil), + "roles": interface{}(nil), + "sharding": interface{}(nil), + } + var v map[string]interface{} + err := json.NewDecoder(r.Body).Decode(&v) + if err != nil { + t.Fatalf("decode json: %v", err) + } + t.Logf("v=%#v\n", v) + if diff := deep.Equal(v, expected); diff != nil { + t.Error(diff) + } + _, _ = fmt.Fprint(w, `{}`) + }) + + _, err := client.Automation.UpdateConfig(ctx, projectID, updateRequest) + if err != nil { + t.Fatalf("Automation.UpdateConfig returned error: %v", err) + } +} From f36f0e0ec419455022fe696ea966321a02fa28bb Mon Sep 17 00:00:00 2001 From: esoulard Date: Wed, 16 Oct 2024 18:11:55 +0200 Subject: [PATCH 2/2] fix: rework prometheus struct --- opsmngr/automation_config.go | 23 +++++++++++----------- opsmngr/automation_config_test.go | 32 +++++++++++++++++-------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/opsmngr/automation_config.go b/opsmngr/automation_config.go index 9249192..e9a2311 100644 --- a/opsmngr/automation_config.go +++ b/opsmngr/automation_config.go @@ -445,15 +445,16 @@ type Process struct { } type Prometheus struct { - Enabled bool `json:"enabled"` - Username string `json:"username"` - Password string `json:"password,omitempty"` - PasswordHash string `json:"passwordHash,omitempty"` - PasswordSalt string `json:"passwordSalt,omitempty"` - Scheme string `json:"scheme"` - TLSPemPath string `json:"tlsPemPath"` - TLSPemPassword string `json:"tlsPemPassword"` - Mode string `json:"mode"` - ListenAddress string `json:"listenAddress"` - MetricsPath string `json:"metricsPath"` + Enabled bool `json:"enabled"` + Username string `json:"username"` + PasswordHash string `json:"passwordHash,omitempty"` + PasswordSalt string `json:"passwordSalt,omitempty"` + Scheme string `json:"scheme"` + TLSPemPath string `json:"tlsPemPath"` + TLSPemPassword string `json:"tlsPemPassword"` + Mode string `json:"mode"` + ListenAddress string `json:"listenAddress"` + MetricsPath string `json:"metricsPath"` + TokenFillRateSeconds int `json:"tokenFillRateSeconds"` + BurstTokenCount int `json:"burstTokenCount"` } diff --git a/opsmngr/automation_config_test.go b/opsmngr/automation_config_test.go index 6a72337..345386b 100644 --- a/opsmngr/automation_config_test.go +++ b/opsmngr/automation_config_test.go @@ -896,12 +896,14 @@ func TestAutomation_UpdatePrometheusConfig(t *testing.T) { updateRequest := &AutomationConfig{ Prometheus: &Prometheus{ - Enabled: true, - ListenAddress: "0.0.0.0:9216", - MetricsPath: "/metrics", - Mode: "opsManager", - Scheme: "http", - Username: "user", + Enabled: true, + ListenAddress: "0.0.0.0:9216", + MetricsPath: "/metrics", + Mode: "opsManager", + Scheme: "http", + Username: "user", + TokenFillRateSeconds: 1, + BurstTokenCount: 1, }, Auth: Auth{ AuthoritativeSet: false, @@ -917,14 +919,16 @@ func TestAutomation_UpdatePrometheusConfig(t *testing.T) { mux.HandleFunc(fmt.Sprintf("/api/public/v1.0/groups/%s/automationConfig", projectID), func(w http.ResponseWriter, r *http.Request) { expected := map[string]interface{}{ "prometheus": map[string]interface{}{ - "enabled": true, - "listenAddress": "0.0.0.0:9216", - "metricsPath": "/metrics", - "mode": "opsManager", - "scheme": "http", - "tlsPemPassword": "", - "tlsPemPath": "", - "username": "user", + "enabled": true, + "listenAddress": "0.0.0.0:9216", + "metricsPath": "/metrics", + "mode": "opsManager", + "scheme": "http", + "tlsPemPassword": "", + "tlsPemPath": "", + "username": "user", + "tokenFillRateSeconds": 1.0, + "burstTokenCount": 1.0, }, "auth": map[string]interface{}{ "authoritativeSet": false,