github.imxd.top/hashicorp/consul@v1.4.5/api/snapshot.go (about)

     1  package api
     2  
     3  import (
     4  	"io"
     5  )
     6  
     7  // Snapshot can be used to query the /v1/snapshot endpoint to take snapshots of
     8  // Consul's internal state and restore snapshots for disaster recovery.
     9  type Snapshot struct {
    10  	c *Client
    11  }
    12  
    13  // Snapshot returns a handle that exposes the snapshot endpoints.
    14  func (c *Client) Snapshot() *Snapshot {
    15  	return &Snapshot{c}
    16  }
    17  
    18  // Save requests a new snapshot and provides an io.ReadCloser with the snapshot
    19  // data to save. If this doesn't return an error, then it's the responsibility
    20  // of the caller to close it. Only a subset of the QueryOptions are supported:
    21  // Datacenter, AllowStale, and Token.
    22  func (s *Snapshot) Save(q *QueryOptions) (io.ReadCloser, *QueryMeta, error) {
    23  	r := s.c.newRequest("GET", "/v1/snapshot")
    24  	r.setQueryOptions(q)
    25  
    26  	rtt, resp, err := requireOK(s.c.doRequest(r))
    27  	if err != nil {
    28  		return nil, nil, err
    29  	}
    30  
    31  	qm := &QueryMeta{}
    32  	parseQueryMeta(resp, qm)
    33  	qm.RequestTime = rtt
    34  	return resp.Body, qm, nil
    35  }
    36  
    37  // Restore streams in an existing snapshot and attempts to restore it.
    38  func (s *Snapshot) Restore(q *WriteOptions, in io.Reader) error {
    39  	r := s.c.newRequest("PUT", "/v1/snapshot")
    40  	r.body = in
    41  	r.setWriteOptions(q)
    42  	_, _, err := requireOK(s.c.doRequest(r))
    43  	if err != nil {
    44  		return err
    45  	}
    46  	return nil
    47  }