forked from michaelklishin/rabbit-hole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime_parameters.go
158 lines (126 loc) · 3.95 KB
/
runtime_parameters.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package rabbithole
import (
"encoding/json"
"net/http"
"net/url"
)
// RuntimeParameter represents a vhost-scoped runtime parameter.
// Value is interface{} to support creating parameters directly from types such as
// FederationUpstream and ShovelInfo.
type RuntimeParameter struct {
Name string `json:"name"`
Vhost string `json:"vhost"`
Component string `json:"component"`
Value interface{} `json:"value"`
}
// RuntimeParameterValue represents arbitrary parameter data.
type RuntimeParameterValue map[string]interface{}
//
// GET /api/parameters
//
// ListRuntimeParameters returns a list of all runtime parameters.
func (c *Client) ListRuntimeParameters() (params []RuntimeParameter, err error) {
req, err := newGETRequest(c, "parameters")
if err != nil {
return []RuntimeParameter{}, err
}
if err = executeAndParseRequest(c, req, ¶ms); err != nil {
return []RuntimeParameter{}, err
}
return params, nil
}
//
// GET /api/parameters/{component}
//
// ListRuntimeParametersFor returns a list of all runtime parameters for a component in all vhosts.
func (c *Client) ListRuntimeParametersFor(component string) (params []RuntimeParameter, err error) {
req, err := newGETRequest(c, "parameters/"+url.PathEscape(component))
if err != nil {
return []RuntimeParameter{}, err
}
if err = executeAndParseRequest(c, req, ¶ms); err != nil {
return []RuntimeParameter{}, err
}
return params, nil
}
//
// GET /api/parameters/{component}/{vhost}
//
// ListRuntimeParametersIn returns a list of all runtime parameters for a component in a vhost.
func (c *Client) ListRuntimeParametersIn(component, vhost string) (p []RuntimeParameter, err error) {
req, err := newGETRequest(c, "parameters/"+url.PathEscape(component)+"/"+url.PathEscape(vhost))
if err != nil {
return []RuntimeParameter{}, err
}
if err = executeAndParseRequest(c, req, &p); err != nil {
return []RuntimeParameter{}, err
}
return p, nil
}
//
// GET /api/parameters/{component}/{vhost}/{name}
//
// GetRuntimeParameter returns information about a runtime parameter.
func (c *Client) GetRuntimeParameter(component, vhost, name string) (p *RuntimeParameter, err error) {
req, err := newGETRequest(c, "parameters/"+url.PathEscape(component)+"/"+url.PathEscape(vhost)+"/"+url.PathEscape(name))
if err != nil {
return nil, err
}
if err = executeAndParseRequest(c, req, &p); err != nil {
return nil, err
}
return p, nil
}
//
// PUT /api/parameters/{component}/{vhost}/{name}
//
// PutRuntimeParameter creates or updates a runtime parameter.
func (c *Client) PutRuntimeParameter(component, vhost, name string, value interface{}) (res *http.Response, err error) {
p := RuntimeParameter{
Name: name,
Vhost: vhost,
Component: component,
Value: value,
}
body, err := json.Marshal(p)
if err != nil {
return nil, err
}
req, err := newRequestWithBody(c, "PUT", "parameters/"+url.PathEscape(component)+"/"+url.PathEscape(vhost)+"/"+url.PathEscape(name), body)
if err != nil {
return nil, err
}
if res, err = executeRequest(c, req); err != nil {
return nil, err
}
return res, nil
}
//
// DELETE /api/parameters/{component}/{vhost}/{name}
//
// DeleteRuntimeParameter removes a runtime parameter.
func (c *Client) DeleteRuntimeParameter(component, vhost, name string) (res *http.Response, err error) {
req, err := newRequestWithBody(c, "DELETE", "parameters/"+url.PathEscape(component)+"/"+url.PathEscape(vhost)+"/"+url.PathEscape(name), nil)
if err != nil {
return nil, err
}
if res, err = executeRequest(c, req); err != nil {
return nil, err
}
return res, nil
}
// DeleteAllRuntimeParameters clears all runtime parameters. Only mean to be used
// in integration tests.
func (c *Client) DeleteAllRuntimeParameters() (err error) {
list, err := c.ListRuntimeParameters()
if err != nil {
return err
}
for _, rp := range list {
_, err = c.DeleteRuntimeParameter(rp.Component, rp.Vhost, rp.Name)
if err != nil {
return err
}
}
return nil
}