github.com/outbrain/consul@v1.4.5/agent/snapshot_endpoint.go (about) 1 package agent 2 3 import ( 4 "bytes" 5 "net/http" 6 7 "github.com/hashicorp/consul/agent/structs" 8 ) 9 10 // Snapshot handles requests to take and restore snapshots. This uses a special 11 // mechanism to make the RPC since we potentially stream large amounts of data 12 // as part of these requests. 13 func (s *HTTPServer) Snapshot(resp http.ResponseWriter, req *http.Request) (interface{}, error) { 14 var args structs.SnapshotRequest 15 s.parseDC(req, &args.Datacenter) 16 s.parseToken(req, &args.Token) 17 if _, ok := req.URL.Query()["stale"]; ok { 18 args.AllowStale = true 19 } 20 21 switch req.Method { 22 case "GET": 23 args.Op = structs.SnapshotSave 24 25 // Headers need to go out before we stream the body. 26 replyFn := func(reply *structs.SnapshotResponse) error { 27 setMeta(resp, &reply.QueryMeta) 28 return nil 29 } 30 31 // Don't bother sending any request body through since it will 32 // be ignored. 33 var null bytes.Buffer 34 if err := s.agent.SnapshotRPC(&args, &null, resp, replyFn); err != nil { 35 return nil, err 36 } 37 return nil, nil 38 39 case "PUT": 40 args.Op = structs.SnapshotRestore 41 if err := s.agent.SnapshotRPC(&args, req.Body, resp, nil); err != nil { 42 return nil, err 43 } 44 return nil, nil 45 46 default: 47 return nil, MethodNotAllowedError{req.Method, []string{"GET", "PUT"}} 48 } 49 }