forked from sonatype-nexus-community/gonexus
/
readOnly.go
85 lines (66 loc) · 2.13 KB
/
readOnly.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package nexusrm
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
const (
restReadOnly = "service/rest/v1/read-only"
restReadOnlyFreeze = "service/rest/v1/read-only/freeze"
restReadOnlyRelease = "service/rest/v1/read-only/release"
restReadOnlyForceRelease = "service/rest/v1/read-only/force-release"
)
// ReadOnlyState returns information about the read-only state of an RM instance
type ReadOnlyState struct {
SystemInitiated bool `json:"systemInitiated"`
SummaryReason string `json:"summaryReason"`
Frozen bool `json:"frozen"`
}
func (s ReadOnlyState) String() string {
var buf bytes.Buffer
buf.WriteString("SystemInitiated: ")
buf.WriteString(fmt.Sprintf("%v", s.SystemInitiated))
buf.WriteString("\n")
buf.WriteString("Frozen: ")
buf.WriteString(fmt.Sprintf("%v", s.Frozen))
buf.WriteString("\n")
buf.WriteString("SummaryReason: ")
buf.WriteString(s.SummaryReason)
buf.WriteString("\n")
return buf.String()
}
// GetReadOnlyState returns the read-only state of the RM instance
func GetReadOnlyState(rm RM) (state ReadOnlyState, err error) {
body, resp, err := rm.Get(restReadOnly)
if err != nil {
return state, fmt.Errorf("could not get read-only state: %v", err)
}
if resp.StatusCode != http.StatusOK {
return state, fmt.Errorf("could not get read-only state: %v", resp.Status)
}
err = json.Unmarshal(body, &state)
return
}
// ReadOnlyEnable enables read-only mode for the RM instance
func ReadOnlyEnable(rm RM) (state ReadOnlyState, err error) {
body, resp, err := rm.Post(restReadOnlyFreeze, nil)
if err != nil && resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusNotFound {
return
}
err = json.Unmarshal(body, &state)
return
}
// ReadOnlyRelease disables read-only mode for the RM instance
func ReadOnlyRelease(rm RM, force bool) (state ReadOnlyState, err error) {
endpoint := restReadOnlyRelease
if force {
endpoint = restReadOnlyForceRelease
}
body, resp, err := rm.Post(endpoint, nil)
if err != nil && resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusNotFound {
return
}
err = json.Unmarshal(body, &state)
return
}