keys.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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/code.google.com/p/go.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(tr *http.Transport, eps []string, to time.Duration) (KeysAPI, error) {
  36. return newHTTPKeysAPIWithPrefix(tr, eps, to, DefaultV2KeysPrefix)
  37. }
  38. func NewDiscoveryKeysAPI(tr *http.Transport, eps []string, to time.Duration) (KeysAPI, error) {
  39. return newHTTPKeysAPIWithPrefix(tr, eps, to, "")
  40. }
  41. func newHTTPKeysAPIWithPrefix(tr *http.Transport, eps []string, to time.Duration, prefix string) (*httpKeysAPI, error) {
  42. c, err := newHTTPClusterClient(tr, eps)
  43. if err != nil {
  44. return nil, err
  45. }
  46. kAPI := httpKeysAPI{
  47. client: c,
  48. prefix: prefix,
  49. timeout: to,
  50. }
  51. return &kAPI, nil
  52. }
  53. type KeysAPI interface {
  54. Create(key, value string, ttl time.Duration) (*Response, error)
  55. Get(key string) (*Response, error)
  56. Watch(key string, idx uint64) Watcher
  57. RecursiveWatch(key string, idx uint64) Watcher
  58. }
  59. type Watcher interface {
  60. Next() (*Response, error)
  61. }
  62. type Response struct {
  63. Action string `json:"action"`
  64. Node *Node `json:"node"`
  65. PrevNode *Node `json:"prevNode"`
  66. }
  67. type Nodes []*Node
  68. type Node struct {
  69. Key string `json:"key"`
  70. Value string `json:"value"`
  71. Nodes Nodes `json:"nodes"`
  72. ModifiedIndex uint64 `json:"modifiedIndex"`
  73. CreatedIndex uint64 `json:"createdIndex"`
  74. }
  75. func (n *Node) String() string {
  76. return fmt.Sprintf("{Key: %s, CreatedIndex: %d, ModifiedIndex: %d}", n.Key, n.CreatedIndex, n.ModifiedIndex)
  77. }
  78. type httpKeysAPI struct {
  79. client httpActionDo
  80. prefix string
  81. timeout time.Duration
  82. }
  83. func (k *httpKeysAPI) Create(key, val string, ttl time.Duration) (*Response, error) {
  84. create := &createAction{
  85. Prefix: k.prefix,
  86. Key: key,
  87. Value: val,
  88. }
  89. if ttl >= 0 {
  90. uttl := uint64(ttl.Seconds())
  91. create.TTL = &uttl
  92. }
  93. ctx, cancel := context.WithTimeout(context.Background(), k.timeout)
  94. code, body, err := k.client.do(ctx, create)
  95. cancel()
  96. if err != nil {
  97. return nil, err
  98. }
  99. return unmarshalHTTPResponse(code, body)
  100. }
  101. func (k *httpKeysAPI) Get(key string) (*Response, error) {
  102. get := &getAction{
  103. Prefix: k.prefix,
  104. Key: key,
  105. Recursive: false,
  106. }
  107. ctx, cancel := context.WithTimeout(context.Background(), k.timeout)
  108. code, body, err := k.client.do(ctx, get)
  109. cancel()
  110. if err != nil {
  111. return nil, err
  112. }
  113. return unmarshalHTTPResponse(code, body)
  114. }
  115. func (k *httpKeysAPI) Watch(key string, idx uint64) Watcher {
  116. return &httpWatcher{
  117. client: k.client,
  118. nextWait: waitAction{
  119. Prefix: k.prefix,
  120. Key: key,
  121. WaitIndex: idx,
  122. Recursive: false,
  123. },
  124. }
  125. }
  126. func (k *httpKeysAPI) RecursiveWatch(key string, idx uint64) Watcher {
  127. return &httpWatcher{
  128. client: k.client,
  129. nextWait: waitAction{
  130. Prefix: k.prefix,
  131. Key: key,
  132. WaitIndex: idx,
  133. Recursive: true,
  134. },
  135. }
  136. }
  137. type httpWatcher struct {
  138. client httpActionDo
  139. nextWait waitAction
  140. }
  141. func (hw *httpWatcher) Next() (*Response, error) {
  142. //TODO(bcwaldon): This needs to be cancellable by the calling user
  143. code, body, err := hw.client.do(context.Background(), &hw.nextWait)
  144. if err != nil {
  145. return nil, err
  146. }
  147. resp, err := unmarshalHTTPResponse(code, body)
  148. if err != nil {
  149. return nil, err
  150. }
  151. hw.nextWait.WaitIndex = resp.Node.ModifiedIndex + 1
  152. return resp, nil
  153. }
  154. // v2KeysURL forms a URL representing the location of a key.
  155. // The endpoint argument represents the base URL of an etcd
  156. // server. The prefix is the path needed to route from the
  157. // provided endpoint's path to the root of the keys API
  158. // (typically "/v2/keys").
  159. func v2KeysURL(ep url.URL, prefix, key string) *url.URL {
  160. ep.Path = path.Join(ep.Path, prefix, key)
  161. return &ep
  162. }
  163. type getAction struct {
  164. Prefix string
  165. Key string
  166. Recursive bool
  167. }
  168. func (g *getAction) httpRequest(ep url.URL) *http.Request {
  169. u := v2KeysURL(ep, g.Prefix, g.Key)
  170. params := u.Query()
  171. params.Set("recursive", strconv.FormatBool(g.Recursive))
  172. u.RawQuery = params.Encode()
  173. req, _ := http.NewRequest("GET", u.String(), nil)
  174. return req
  175. }
  176. type waitAction struct {
  177. Prefix string
  178. Key string
  179. WaitIndex uint64
  180. Recursive bool
  181. }
  182. func (w *waitAction) httpRequest(ep url.URL) *http.Request {
  183. u := v2KeysURL(ep, w.Prefix, w.Key)
  184. params := u.Query()
  185. params.Set("wait", "true")
  186. params.Set("waitIndex", strconv.FormatUint(w.WaitIndex, 10))
  187. params.Set("recursive", strconv.FormatBool(w.Recursive))
  188. u.RawQuery = params.Encode()
  189. req, _ := http.NewRequest("GET", u.String(), nil)
  190. return req
  191. }
  192. type createAction struct {
  193. Prefix string
  194. Key string
  195. Value string
  196. TTL *uint64
  197. }
  198. func (c *createAction) httpRequest(ep url.URL) *http.Request {
  199. u := v2KeysURL(ep, c.Prefix, c.Key)
  200. params := u.Query()
  201. params.Set("prevExist", "false")
  202. u.RawQuery = params.Encode()
  203. form := url.Values{}
  204. form.Add("value", c.Value)
  205. if c.TTL != nil {
  206. form.Add("ttl", strconv.FormatUint(*c.TTL, 10))
  207. }
  208. body := strings.NewReader(form.Encode())
  209. req, _ := http.NewRequest("PUT", u.String(), body)
  210. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  211. return req
  212. }
  213. func unmarshalHTTPResponse(code int, body []byte) (res *Response, err error) {
  214. switch code {
  215. case http.StatusOK, http.StatusCreated:
  216. res, err = unmarshalSuccessfulResponse(body)
  217. default:
  218. err = unmarshalErrorResponse(code)
  219. }
  220. return
  221. }
  222. func unmarshalSuccessfulResponse(body []byte) (*Response, error) {
  223. var res Response
  224. err := json.Unmarshal(body, &res)
  225. if err != nil {
  226. return nil, err
  227. }
  228. return &res, nil
  229. }
  230. func unmarshalErrorResponse(code int) error {
  231. switch code {
  232. case http.StatusNotFound:
  233. return ErrKeyNoExist
  234. case http.StatusPreconditionFailed:
  235. return ErrKeyExists
  236. case http.StatusInternalServerError:
  237. // this isn't necessarily true
  238. return ErrNoLeader
  239. default:
  240. }
  241. return fmt.Errorf("unrecognized HTTP status code %d", code)
  242. }