keys.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package client
  14. import (
  15. "encoding/json"
  16. "errors"
  17. "fmt"
  18. "net/http"
  19. "net/url"
  20. "path"
  21. "strconv"
  22. "strings"
  23. "time"
  24. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  25. )
  26. var (
  27. DefaultV2KeysPrefix = "/v2/keys"
  28. )
  29. var (
  30. ErrUnavailable = errors.New("client: no available etcd endpoints")
  31. ErrNoLeader = errors.New("client: no leader")
  32. ErrKeyNoExist = errors.New("client: key does not exist")
  33. ErrKeyExists = errors.New("client: key already exists")
  34. )
  35. func NewKeysAPI(c HTTPClient) KeysAPI {
  36. return &httpKeysAPI{
  37. client: c,
  38. prefix: DefaultV2KeysPrefix,
  39. }
  40. }
  41. func NewDiscoveryKeysAPI(c HTTPClient) KeysAPI {
  42. return &httpKeysAPI{
  43. client: c,
  44. prefix: "",
  45. }
  46. }
  47. type KeysAPI interface {
  48. Create(ctx context.Context, key, value string, ttl time.Duration) (*Response, error)
  49. Get(ctx context.Context, key string) (*Response, error)
  50. Watch(key string, idx uint64) Watcher
  51. RecursiveWatch(key string, idx uint64) Watcher
  52. }
  53. type Watcher interface {
  54. Next(context.Context) (*Response, error)
  55. }
  56. type Response struct {
  57. Action string `json:"action"`
  58. Node *Node `json:"node"`
  59. PrevNode *Node `json:"prevNode"`
  60. Index uint64
  61. }
  62. type Nodes []*Node
  63. type Node struct {
  64. Key string `json:"key"`
  65. Value string `json:"value"`
  66. Nodes Nodes `json:"nodes"`
  67. ModifiedIndex uint64 `json:"modifiedIndex"`
  68. CreatedIndex uint64 `json:"createdIndex"`
  69. }
  70. func (n *Node) String() string {
  71. return fmt.Sprintf("{Key: %s, CreatedIndex: %d, ModifiedIndex: %d}", n.Key, n.CreatedIndex, n.ModifiedIndex)
  72. }
  73. type httpKeysAPI struct {
  74. client HTTPClient
  75. prefix string
  76. }
  77. func (k *httpKeysAPI) Create(ctx context.Context, key, val string, ttl time.Duration) (*Response, error) {
  78. create := &createAction{
  79. Prefix: k.prefix,
  80. Key: key,
  81. Value: val,
  82. }
  83. if ttl >= 0 {
  84. uttl := uint64(ttl.Seconds())
  85. create.TTL = &uttl
  86. }
  87. resp, body, err := k.client.Do(ctx, create)
  88. if err != nil {
  89. return nil, err
  90. }
  91. return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
  92. }
  93. func (k *httpKeysAPI) Get(ctx context.Context, key string) (*Response, error) {
  94. get := &getAction{
  95. Prefix: k.prefix,
  96. Key: key,
  97. Recursive: false,
  98. }
  99. resp, body, err := k.client.Do(ctx, get)
  100. if err != nil {
  101. return nil, err
  102. }
  103. return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
  104. }
  105. func (k *httpKeysAPI) Watch(key string, idx uint64) Watcher {
  106. return &httpWatcher{
  107. client: k.client,
  108. nextWait: waitAction{
  109. Prefix: k.prefix,
  110. Key: key,
  111. WaitIndex: idx,
  112. Recursive: false,
  113. },
  114. }
  115. }
  116. func (k *httpKeysAPI) RecursiveWatch(key string, idx uint64) Watcher {
  117. return &httpWatcher{
  118. client: k.client,
  119. nextWait: waitAction{
  120. Prefix: k.prefix,
  121. Key: key,
  122. WaitIndex: idx,
  123. Recursive: true,
  124. },
  125. }
  126. }
  127. type httpWatcher struct {
  128. client HTTPClient
  129. nextWait waitAction
  130. }
  131. func (hw *httpWatcher) Next(ctx context.Context) (*Response, error) {
  132. httpresp, body, err := hw.client.Do(ctx, &hw.nextWait)
  133. if err != nil {
  134. return nil, err
  135. }
  136. resp, err := unmarshalHTTPResponse(httpresp.StatusCode, httpresp.Header, body)
  137. if err != nil {
  138. return nil, err
  139. }
  140. hw.nextWait.WaitIndex = resp.Node.ModifiedIndex + 1
  141. return resp, nil
  142. }
  143. // v2KeysURL forms a URL representing the location of a key.
  144. // The endpoint argument represents the base URL of an etcd
  145. // server. The prefix is the path needed to route from the
  146. // provided endpoint's path to the root of the keys API
  147. // (typically "/v2/keys").
  148. func v2KeysURL(ep url.URL, prefix, key string) *url.URL {
  149. ep.Path = path.Join(ep.Path, prefix, key)
  150. return &ep
  151. }
  152. type getAction struct {
  153. Prefix string
  154. Key string
  155. Recursive bool
  156. }
  157. func (g *getAction) HTTPRequest(ep url.URL) *http.Request {
  158. u := v2KeysURL(ep, g.Prefix, g.Key)
  159. params := u.Query()
  160. params.Set("recursive", strconv.FormatBool(g.Recursive))
  161. u.RawQuery = params.Encode()
  162. req, _ := http.NewRequest("GET", u.String(), nil)
  163. return req
  164. }
  165. type waitAction struct {
  166. Prefix string
  167. Key string
  168. WaitIndex uint64
  169. Recursive bool
  170. }
  171. func (w *waitAction) HTTPRequest(ep url.URL) *http.Request {
  172. u := v2KeysURL(ep, w.Prefix, w.Key)
  173. params := u.Query()
  174. params.Set("wait", "true")
  175. params.Set("waitIndex", strconv.FormatUint(w.WaitIndex, 10))
  176. params.Set("recursive", strconv.FormatBool(w.Recursive))
  177. u.RawQuery = params.Encode()
  178. req, _ := http.NewRequest("GET", u.String(), nil)
  179. return req
  180. }
  181. type createAction struct {
  182. Prefix string
  183. Key string
  184. Value string
  185. TTL *uint64
  186. }
  187. func (c *createAction) HTTPRequest(ep url.URL) *http.Request {
  188. u := v2KeysURL(ep, c.Prefix, c.Key)
  189. params := u.Query()
  190. params.Set("prevExist", "false")
  191. u.RawQuery = params.Encode()
  192. form := url.Values{}
  193. form.Add("value", c.Value)
  194. if c.TTL != nil {
  195. form.Add("ttl", strconv.FormatUint(*c.TTL, 10))
  196. }
  197. body := strings.NewReader(form.Encode())
  198. req, _ := http.NewRequest("PUT", u.String(), body)
  199. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  200. return req
  201. }
  202. func unmarshalHTTPResponse(code int, header http.Header, body []byte) (res *Response, err error) {
  203. switch code {
  204. case http.StatusOK, http.StatusCreated:
  205. res, err = unmarshalSuccessfulResponse(header, body)
  206. default:
  207. err = unmarshalErrorResponse(code)
  208. }
  209. return
  210. }
  211. func unmarshalSuccessfulResponse(header http.Header, body []byte) (*Response, error) {
  212. var res Response
  213. err := json.Unmarshal(body, &res)
  214. if err != nil {
  215. return nil, err
  216. }
  217. if header.Get("X-Etcd-Index") != "" {
  218. res.Index, err = strconv.ParseUint(header.Get("X-Etcd-Index"), 10, 64)
  219. }
  220. if err != nil {
  221. return nil, err
  222. }
  223. return &res, nil
  224. }
  225. func unmarshalErrorResponse(code int) error {
  226. switch code {
  227. case http.StatusNotFound:
  228. return ErrKeyNoExist
  229. case http.StatusPreconditionFailed:
  230. return ErrKeyExists
  231. case http.StatusInternalServerError:
  232. // this isn't necessarily true
  233. return ErrNoLeader
  234. case http.StatusGatewayTimeout:
  235. return ErrTimeout
  236. default:
  237. }
  238. return fmt.Errorf("unrecognized HTTP status code %d", code)
  239. }