keys.go 6.4 KB

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