2016-10-26 02:20:24 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2016-10-26 02:20:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Snapshot handles requests to take and restore snapshots. This uses a special
|
|
|
|
// mechanism to make the RPC since we potentially stream large amounts of data
|
|
|
|
// as part of these requests.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) Snapshot(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-10-26 02:20:24 +00:00
|
|
|
var args structs.SnapshotRequest
|
|
|
|
s.parseDC(req, &args.Datacenter)
|
|
|
|
s.parseToken(req, &args.Token)
|
|
|
|
if _, ok := req.URL.Query()["stale"]; ok {
|
|
|
|
args.AllowStale = true
|
|
|
|
}
|
|
|
|
|
|
|
|
switch req.Method {
|
|
|
|
case "GET":
|
|
|
|
args.Op = structs.SnapshotSave
|
|
|
|
|
|
|
|
// Headers need to go out before we stream the body.
|
|
|
|
replyFn := func(reply *structs.SnapshotResponse) error {
|
|
|
|
setMeta(resp, &reply.QueryMeta)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't bother sending any request body through since it will
|
|
|
|
// be ignored.
|
|
|
|
var null bytes.Buffer
|
2020-03-25 20:42:43 +00:00
|
|
|
if err := s.agent.delegate.SnapshotRPC(&args, &null, resp, replyFn); err != nil {
|
2016-10-26 02:20:24 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
|
|
|
|
case "PUT":
|
|
|
|
args.Op = structs.SnapshotRestore
|
2020-03-25 20:42:43 +00:00
|
|
|
if err := s.agent.delegate.SnapshotRPC(&args, req.Body, resp, nil); err != nil {
|
2016-10-26 02:20:24 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
|
|
|
|
default:
|
2017-09-26 06:11:19 +00:00
|
|
|
return nil, MethodNotAllowedError{req.Method, []string{"GET", "PUT"}}
|
2016-10-26 02:20:24 +00:00
|
|
|
}
|
|
|
|
}
|