forked from cockroachdb/cockroach
/
admin.go
174 lines (151 loc) · 5.08 KB
/
admin.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Marc Berhault (marc@cockroachlabs.com)
package client
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/cockroachdb/cockroach/base"
"github.com/cockroachdb/cockroach/util"
)
const (
// Types of admin config to manipulate.
// Quit only handles Get requests.
Quit = "quit"
)
// AdminClient issues http requests to admin endpoints.
// TODO(marc): unify the way we handle addresses in clients.
type AdminClient struct {
context *base.Context
address string
configType string
}
// NewAdminClient returns a new 'configType' admin client, talking to 'address'.
func NewAdminClient(ctx *base.Context, address, configType string) AdminClient {
return AdminClient{ctx, address, configType}
}
// adminURI builds a base URI for the embedded 'configType'
func (a *AdminClient) adminURI() string {
return fmt.Sprintf("%s://%s/_admin/%s", a.context.HTTPRequestScheme(),
a.address, a.configType)
}
// adminURIWithKey builds a URI for the 'configType' and key.
func (a *AdminClient) adminURIWithKey(key string) string {
return fmt.Sprintf("%s/%s", a.adminURI(), url.QueryEscape(key))
}
// Get issues a GET and returns the plain-text body. It cannot take a key.
func (a *AdminClient) Get() (string, error) {
body, err := a.do("GET", a.adminURI(), "", util.PlaintextContentType, nil)
if err != nil {
return "", err
}
return string(body), nil
}
// GetJSON issues a GET request and returns a json-encoded response.
func (a *AdminClient) GetJSON(key string) (string, error) {
body, err := a.do("GET", a.adminURIWithKey(key), "", util.JSONContentType, nil)
if err != nil {
return "", err
}
return string(body), nil
}
// GetYAML issues a GET request and returns a yaml-encoded response.
func (a *AdminClient) GetYAML(key string) (string, error) {
body, err := a.do("GET", a.adminURIWithKey(key), "", util.YAMLContentType, nil)
if err != nil {
return "", err
}
return string(body), nil
}
// SetJSON issues a POST request for the given key using the json-encoded body.
func (a *AdminClient) SetJSON(key, body string) error {
_, err := a.do("POST", a.adminURIWithKey(key), util.JSONContentType,
"", strings.NewReader(body))
return err
}
// SetYAML issues a POST request for the given key using the yaml-encoded body.
func (a *AdminClient) SetYAML(key, body string) error {
_, err := a.do("POST", a.adminURIWithKey(key), util.YAMLContentType,
"", strings.NewReader(body))
return err
}
// List issues a GET request to list all configs. Returns a list of keys.
func (a *AdminClient) List() ([]string, error) {
body, err := a.do("GET", a.adminURI(), "", util.JSONContentType, nil)
if err != nil {
return nil, err
}
type wrapper struct {
Data []string `json:"d"`
}
var w wrapper
if err = json.Unmarshal(body, &w); err != nil {
return nil, util.Errorf("unable to parse response %q: %s", body, err)
}
// Make sure we unescape all keys.
for i, val := range w.Data {
w.Data[i], err = url.QueryUnescape(val)
if err != nil {
return nil, util.Errorf("unable to unescape %q: %s", val, err)
}
}
return w.Data, nil
}
// Delete issues a DELETE request for the given key.
func (a *AdminClient) Delete(key string) error {
_, err := a.do("DELETE", a.adminURIWithKey(key), "", "", nil)
return err
}
// do issues the http request to 'url' using 'method'.
// If 'contentType' is not empty, set the request header ContentType.
// If 'acceptContentType is not empty, set the request header Accept.
// Returns the response body.
// Errors out on transport problems and non-200 response codes.
func (a *AdminClient) do(method, url, contentType, acceptContentType string,
body io.Reader) ([]byte, error) {
client, err := a.context.GetHTTPClient()
if err != nil {
return nil, util.Errorf("failed to initialize %s client: %s",
a.context.HTTPRequestScheme(), err)
}
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
if len(contentType) != 0 {
req.Header.Add(util.ContentTypeHeader, contentType)
}
if len(acceptContentType) != 0 {
req.Header.Add(util.AcceptHeader, acceptContentType)
}
resp, err := client.Do(req)
if err != nil {
return nil, util.Errorf("%s request failed: %s, err: %s", req.Method, req.URL, err)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, util.Errorf("failed to read response for %s %s: %s", req.Method, req.URL, err)
}
if resp.StatusCode != http.StatusOK {
return nil, util.Errorf("%s %s: got %s: %s", req.Method, req.URL, resp.Status, string(b))
}
return b, nil
}