Skip to content

Commit cf1c9a3

Browse files
committed
TUN-3192: Use zone ID in tunnelstore request path; improve debug logging
1 parent d61e3fb commit cf1c9a3

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

tunnelstore/client.go

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
"strings"
1010
"time"
1111

12-
"github.com/cloudflare/cloudflared/logger"
1312
"github.com/google/uuid"
1413
"github.com/pkg/errors"
14+
15+
"github.com/cloudflare/cloudflared/logger"
1516
)
1617

1718
const (
@@ -129,7 +130,7 @@ func NewRESTClient(baseURL string, accountTag, zoneTag string, authToken string,
129130
return &RESTClient{
130131
baseEndpoints: &baseEndpoints{
131132
accountLevel: fmt.Sprintf("%s/accounts/%s/tunnels", baseURL, accountTag),
132-
zoneLevel: fmt.Sprintf("%s/zones/%s/tunnels", baseURL, accountTag),
133+
zoneLevel: fmt.Sprintf("%s/zones/%s/tunnels", baseURL, zoneTag),
133134
},
134135
authToken: authToken,
135136
client: http.Client{
@@ -152,15 +153,12 @@ func (r *RESTClient) CreateTunnel(name string, tunnelSecret []byte) (*Tunnel, er
152153
if name == "" {
153154
return nil, errors.New("tunnel name required")
154155
}
155-
body, err := json.Marshal(&newTunnel{
156+
body := &newTunnel{
156157
Name: name,
157158
TunnelSecret: tunnelSecret,
158-
})
159-
if err != nil {
160-
return nil, errors.Wrap(err, "Failed to serialize new tunnel request")
161159
}
162160

163-
resp, err := r.sendRequest("POST", r.baseEndpoints.accountLevel, bytes.NewBuffer(body))
161+
resp, err := r.sendRequest("POST", r.baseEndpoints.accountLevel, body)
164162
if err != nil {
165163
return nil, errors.Wrap(err, "REST request failed")
166164
}
@@ -173,7 +171,7 @@ func (r *RESTClient) CreateTunnel(name string, tunnelSecret []byte) (*Tunnel, er
173171
return nil, ErrTunnelNameConflict
174172
}
175173

176-
return nil, statusCodeToError("create tunnel", resp)
174+
return nil, r.statusCodeToError("create tunnel", resp)
177175
}
178176

179177
func (r *RESTClient) GetTunnel(tunnelID uuid.UUID) (*Tunnel, error) {
@@ -188,7 +186,7 @@ func (r *RESTClient) GetTunnel(tunnelID uuid.UUID) (*Tunnel, error) {
188186
return unmarshalTunnel(resp.Body)
189187
}
190188

191-
return nil, statusCodeToError("get tunnel", resp)
189+
return nil, r.statusCodeToError("get tunnel", resp)
192190
}
193191

194192
func (r *RESTClient) DeleteTunnel(tunnelID uuid.UUID) error {
@@ -199,7 +197,7 @@ func (r *RESTClient) DeleteTunnel(tunnelID uuid.UUID) error {
199197
}
200198
defer resp.Body.Close()
201199

202-
return statusCodeToError("delete tunnel", resp)
200+
return r.statusCodeToError("delete tunnel", resp)
203201
}
204202

205203
func (r *RESTClient) ListTunnels() ([]Tunnel, error) {
@@ -217,7 +215,7 @@ func (r *RESTClient) ListTunnels() ([]Tunnel, error) {
217215
return tunnels, nil
218216
}
219217

220-
return nil, statusCodeToError("list tunnels", resp)
218+
return nil, r.statusCodeToError("list tunnels", resp)
221219
}
222220

223221
func (r *RESTClient) CleanupConnections(tunnelID uuid.UUID) error {
@@ -228,32 +226,37 @@ func (r *RESTClient) CleanupConnections(tunnelID uuid.UUID) error {
228226
}
229227
defer resp.Body.Close()
230228

231-
return statusCodeToError("cleanup connections", resp)
229+
return r.statusCodeToError("cleanup connections", resp)
232230
}
233231

234232
func (r *RESTClient) RouteTunnel(tunnelID uuid.UUID, route Route) error {
235-
body, err := json.Marshal(route)
236-
if err != nil {
237-
return errors.Wrap(err, "Failed to serialize Route")
238-
}
239-
240233
endpoint := fmt.Sprintf("%s/%v/routes", r.baseEndpoints.zoneLevel, tunnelID)
241-
resp, err := r.sendRequest("PUT", endpoint, bytes.NewBuffer(body))
234+
resp, err := r.sendRequest("PUT", endpoint, route)
242235
if err != nil {
243236
return errors.Wrap(err, "REST request failed")
244237
}
245238
defer resp.Body.Close()
246239

247-
return statusCodeToError("add route", resp)
240+
return r.statusCodeToError("add route", resp)
248241
}
249242

250-
func (r *RESTClient) sendRequest(method string, url string, body io.Reader) (*http.Response, error) {
243+
func (r *RESTClient) sendRequest(method string, url string, body interface{}) (*http.Response, error) {
251244
r.logger.Debugf("%s %s", method, url)
252-
req, err := http.NewRequest(method, url, body)
245+
246+
var bodyReader io.Reader
247+
if body != nil {
248+
if bodyBytes, err := json.Marshal(body); err != nil {
249+
return nil, errors.Wrap(err, "failed to serialize json body")
250+
} else {
251+
bodyReader = bytes.NewBuffer(bodyBytes)
252+
}
253+
}
254+
255+
req, err := http.NewRequest(method, url, bodyReader)
253256
if err != nil {
254257
return nil, errors.Wrapf(err, "can't create %s request", method)
255258
}
256-
if body != nil {
259+
if bodyReader != nil {
257260
req.Header.Set("Content-Type", jsonContentType)
258261
}
259262
req.Header.Add("X-Auth-User-Service-Key", r.authToken)
@@ -268,7 +271,7 @@ func unmarshalTunnel(reader io.Reader) (*Tunnel, error) {
268271
return &tunnel, nil
269272
}
270273

271-
func statusCodeToError(op string, resp *http.Response) error {
274+
func (r *RESTClient) statusCodeToError(op string, resp *http.Response) error {
272275
switch resp.StatusCode {
273276
case http.StatusOK:
274277
return nil

0 commit comments

Comments
 (0)