forked from etcd-io/etcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_index_handler.go
45 lines (36 loc) · 971 Bytes
/
get_index_handler.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
package v2
import (
"net/http"
"path"
etcdErr "github.com/coreos/etcd/error"
"github.com/coreos/etcd/third_party/github.com/gorilla/mux"
)
// getIndexHandler retrieves the current lock index.
// The "field" parameter specifies to read either the lock "index" or lock "value".
func (h *handler) getIndexHandler(w http.ResponseWriter, req *http.Request) error {
h.client.SyncCluster()
vars := mux.Vars(req)
keypath := path.Join(prefix, vars["key"])
field := req.FormValue("field")
if len(field) == 0 {
field = "value"
}
// Read all indices.
resp, err := h.client.Get(keypath, true, true)
if err != nil {
return err
}
nodes := lockNodes{resp.Node.Nodes}
// Write out the requested field.
if node := nodes.First(); node != nil {
switch field {
case "index":
w.Write([]byte(path.Base(node.Key)))
case "value":
w.Write([]byte(node.Value))
default:
return etcdErr.NewError(etcdErr.EcodeInvalidField, "Get", 0)
}
}
return nil
}