keys.go 6.2 KB

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