forked from sonatype-nexus-community/gonexus
/
maintenance.go
68 lines (54 loc) · 1.49 KB
/
maintenance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package nexusrm
import (
"encoding/json"
"fmt"
"net/http"
)
const restMaintenanceDBCheck = "service/rest/v1/maintenance/%s/check"
// Define database types
const (
AccessLogDB = "accesslog"
ComponentDB = "component"
ConfigDB = "config"
SecurityDB = "security"
)
// DatabaseState contains state information about a given state
type DatabaseState struct {
PageCorruption bool `json:"pageCorruption"`
IndexErrors int `json:"indexErrors"`
}
// CheckDatabase returns the state of the named database
func CheckDatabase(rm RM, dbName string) (DatabaseState, error) {
doError := func(err error) error {
return fmt.Errorf("error checking status of database '%s': %v", dbName, err)
}
var state DatabaseState
url := fmt.Sprintf(restMaintenanceDBCheck, dbName)
body, resp, err := rm.Put(url, nil)
if err != nil || resp.StatusCode != http.StatusOK {
return state, doError(err)
}
if err := json.Unmarshal(body, &state); err != nil {
return state, doError(err)
}
return state, nil
}
// CheckAllDatabases returns state on all of the databases
func CheckAllDatabases(rm RM) (states map[string]DatabaseState, err error) {
states = make(map[string]DatabaseState)
check := func(dbName string) {
if err != nil {
return
}
if state, er := CheckDatabase(rm, dbName); er != nil {
err = fmt.Errorf("error with '%s' database when all states: %v", dbName, er)
} else {
states[dbName] = state
}
}
check(AccessLogDB)
check(ComponentDB)
check(ConfigDB)
check(SecurityDB)
return
}