keys.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package client
  15. import (
  16. "encoding/json"
  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. type PrevExistType string
  27. const (
  28. PrevIgnore = PrevExistType("")
  29. PrevExist = PrevExistType("true")
  30. PrevNoExist = PrevExistType("false")
  31. )
  32. var (
  33. defaultV2KeysPrefix = "/v2/keys"
  34. )
  35. func NewKeysAPI(c Client) KeysAPI {
  36. return NewKeysAPIWithPrefix(c, defaultV2KeysPrefix)
  37. }
  38. func NewKeysAPIWithPrefix(c Client, p string) KeysAPI {
  39. return &httpKeysAPI{
  40. client: c,
  41. prefix: p,
  42. }
  43. }
  44. type KeysAPI interface {
  45. Set(ctx context.Context, key, value string, opts *SetOptions) (*Response, error)
  46. Create(ctx context.Context, key, value string) (*Response, error)
  47. Update(ctx context.Context, key, value string) (*Response, error)
  48. Delete(ctx context.Context, key string, opts *DeleteOptions) (*Response, error)
  49. Get(ctx context.Context, key string) (*Response, error)
  50. RGet(ctx context.Context, key string) (*Response, error)
  51. Watcher(key string, opts *WatcherOptions) Watcher
  52. }
  53. type WatcherOptions struct {
  54. // WaitIndex defines the index after-which the Watcher should
  55. // start emitting events. For example, if a value of 5 is
  56. // provided, the first event will have an index >= 6.
  57. //
  58. // Setting WaitIndex to 0 (default) means that the Watcher
  59. // should start watching for events starting at the current
  60. // index, whatever that may be.
  61. WaitIndex uint64
  62. // Recursive specifices whether or not the Watcher should emit
  63. // events that occur in children of the given keyspace. If set
  64. // to false (default), events will be limited to those that
  65. // occur for the exact key.
  66. Recursive bool
  67. }
  68. type SetOptions struct {
  69. // PrevValue specifies what the current value of the Node must
  70. // be in order for the Set operation to succeed.
  71. //
  72. // Leaving this field empty means that the caller wishes to
  73. // ignore the current value of the Node. This cannot be used
  74. // to compare the Node's current value to an empty string.
  75. PrevValue string
  76. // PrevIndex indicates what the current ModifiedIndex of the
  77. // Node must be in order for the Set operation to succeed.
  78. //
  79. // If PrevIndex is set to 0 (default), no comparison is made.
  80. PrevIndex uint64
  81. // PrevExist specifies whether the Node must currently exist
  82. // (PrevExist) or not (PrevNoExist). If the caller does not
  83. // care about existence, set PrevExist to PrevIgnore, or simply
  84. // leave it unset.
  85. PrevExist PrevExistType
  86. // TTL defines a period of time after-which the Node should
  87. // expire and no longer exist. Values <= 0 are ignored. Given
  88. // that the zero-value is ignored, TTL cannot be used to set
  89. // a TTL of 0.
  90. TTL time.Duration
  91. }
  92. type DeleteOptions struct {
  93. // PrevValue specifies what the current value of the Node must
  94. // be in order for the Delete operation to succeed.
  95. //
  96. // Leaving this field empty means that the caller wishes to
  97. // ignore the current value of the Node. This cannot be used
  98. // to compare the Node's current value to an empty string.
  99. PrevValue string
  100. // PrevIndex indicates what the current ModifiedIndex of the
  101. // Node must be in order for the Delete operation to succeed.
  102. //
  103. // If PrevIndex is set to 0 (default), no comparison is made.
  104. PrevIndex uint64
  105. // Recursive defines whether or not all children of the Node
  106. // should be deleted. If set to true, all children of the Node
  107. // identified by the given key will be deleted. If left unset
  108. // or explicitly set to false, only a single Node will be
  109. // deleted.
  110. Recursive bool
  111. }
  112. type Watcher interface {
  113. Next(context.Context) (*Response, error)
  114. }
  115. type Response struct {
  116. Action string `json:"action"`
  117. Node *Node `json:"node"`
  118. PrevNode *Node `json:"prevNode"`
  119. Index uint64
  120. }
  121. type Node struct {
  122. Key string `json:"key"`
  123. Value string `json:"value"`
  124. Nodes []*Node `json:"nodes"`
  125. ModifiedIndex uint64 `json:"modifiedIndex"`
  126. CreatedIndex uint64 `json:"createdIndex"`
  127. }
  128. func (n *Node) String() string {
  129. return fmt.Sprintf("{Key: %s, CreatedIndex: %d, ModifiedIndex: %d}", n.Key, n.CreatedIndex, n.ModifiedIndex)
  130. }
  131. type httpKeysAPI struct {
  132. client httpClient
  133. prefix string
  134. }
  135. func (k *httpKeysAPI) Set(ctx context.Context, key, val string, opts *SetOptions) (*Response, error) {
  136. act := &setAction{
  137. Prefix: k.prefix,
  138. Key: key,
  139. Value: val,
  140. }
  141. if opts != nil {
  142. act.PrevValue = opts.PrevValue
  143. act.PrevIndex = opts.PrevIndex
  144. act.PrevExist = opts.PrevExist
  145. act.TTL = opts.TTL
  146. }
  147. resp, body, err := k.client.Do(ctx, act)
  148. if err != nil {
  149. return nil, err
  150. }
  151. return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
  152. }
  153. func (k *httpKeysAPI) Create(ctx context.Context, key, val string) (*Response, error) {
  154. return k.Set(ctx, key, val, &SetOptions{PrevExist: PrevNoExist})
  155. }
  156. func (k *httpKeysAPI) Update(ctx context.Context, key, val string) (*Response, error) {
  157. return k.Set(ctx, key, val, &SetOptions{PrevExist: PrevExist})
  158. }
  159. func (k *httpKeysAPI) Delete(ctx context.Context, key string, opts *DeleteOptions) (*Response, error) {
  160. act := &deleteAction{
  161. Prefix: k.prefix,
  162. Key: key,
  163. }
  164. if opts != nil {
  165. act.PrevValue = opts.PrevValue
  166. act.PrevIndex = opts.PrevIndex
  167. act.Recursive = opts.Recursive
  168. }
  169. resp, body, err := k.client.Do(ctx, act)
  170. if err != nil {
  171. return nil, err
  172. }
  173. return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
  174. }
  175. func (k *httpKeysAPI) Get(ctx context.Context, key string) (*Response, error) {
  176. get := &getAction{
  177. Prefix: k.prefix,
  178. Key: key,
  179. Recursive: false,
  180. }
  181. resp, body, err := k.client.Do(ctx, get)
  182. if err != nil {
  183. return nil, err
  184. }
  185. return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
  186. }
  187. func (k *httpKeysAPI) RGet(ctx context.Context, key string) (*Response, error) {
  188. get := &getAction{
  189. Prefix: k.prefix,
  190. Key: key,
  191. Recursive: true,
  192. }
  193. resp, body, err := k.client.Do(ctx, get)
  194. if err != nil {
  195. return nil, err
  196. }
  197. return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
  198. }
  199. func (k *httpKeysAPI) Watcher(key string, opts *WatcherOptions) Watcher {
  200. act := waitAction{
  201. Prefix: k.prefix,
  202. Key: key,
  203. }
  204. if opts != nil {
  205. act.WaitIndex = opts.WaitIndex
  206. act.Recursive = opts.Recursive
  207. }
  208. return &httpWatcher{
  209. client: k.client,
  210. nextWait: act,
  211. }
  212. }
  213. type httpWatcher struct {
  214. client httpClient
  215. nextWait waitAction
  216. }
  217. func (hw *httpWatcher) Next(ctx context.Context) (*Response, error) {
  218. httpresp, body, err := hw.client.Do(ctx, &hw.nextWait)
  219. if err != nil {
  220. return nil, err
  221. }
  222. resp, err := unmarshalHTTPResponse(httpresp.StatusCode, httpresp.Header, body)
  223. if err != nil {
  224. return nil, err
  225. }
  226. hw.nextWait.WaitIndex = resp.Node.ModifiedIndex + 1
  227. return resp, nil
  228. }
  229. // v2KeysURL forms a URL representing the location of a key.
  230. // The endpoint argument represents the base URL of an etcd
  231. // server. The prefix is the path needed to route from the
  232. // provided endpoint's path to the root of the keys API
  233. // (typically "/v2/keys").
  234. func v2KeysURL(ep url.URL, prefix, key string) *url.URL {
  235. ep.Path = path.Join(ep.Path, prefix, key)
  236. return &ep
  237. }
  238. type getAction struct {
  239. Prefix string
  240. Key string
  241. Recursive bool
  242. }
  243. func (g *getAction) HTTPRequest(ep url.URL) *http.Request {
  244. u := v2KeysURL(ep, g.Prefix, g.Key)
  245. params := u.Query()
  246. params.Set("recursive", strconv.FormatBool(g.Recursive))
  247. u.RawQuery = params.Encode()
  248. req, _ := http.NewRequest("GET", u.String(), nil)
  249. return req
  250. }
  251. type waitAction struct {
  252. Prefix string
  253. Key string
  254. WaitIndex uint64
  255. Recursive bool
  256. }
  257. func (w *waitAction) HTTPRequest(ep url.URL) *http.Request {
  258. u := v2KeysURL(ep, w.Prefix, w.Key)
  259. params := u.Query()
  260. params.Set("wait", "true")
  261. params.Set("waitIndex", strconv.FormatUint(w.WaitIndex, 10))
  262. params.Set("recursive", strconv.FormatBool(w.Recursive))
  263. u.RawQuery = params.Encode()
  264. req, _ := http.NewRequest("GET", u.String(), nil)
  265. return req
  266. }
  267. type setAction struct {
  268. Prefix string
  269. Key string
  270. Value string
  271. PrevValue string
  272. PrevIndex uint64
  273. PrevExist PrevExistType
  274. TTL time.Duration
  275. }
  276. func (a *setAction) HTTPRequest(ep url.URL) *http.Request {
  277. u := v2KeysURL(ep, a.Prefix, a.Key)
  278. params := u.Query()
  279. if a.PrevValue != "" {
  280. params.Set("prevValue", a.PrevValue)
  281. }
  282. if a.PrevIndex != 0 {
  283. params.Set("prevIndex", strconv.FormatUint(a.PrevIndex, 10))
  284. }
  285. if a.PrevExist != PrevIgnore {
  286. params.Set("prevExist", string(a.PrevExist))
  287. }
  288. u.RawQuery = params.Encode()
  289. form := url.Values{}
  290. form.Add("value", a.Value)
  291. if a.TTL > 0 {
  292. form.Add("ttl", strconv.FormatUint(uint64(a.TTL.Seconds()), 10))
  293. }
  294. body := strings.NewReader(form.Encode())
  295. req, _ := http.NewRequest("PUT", u.String(), body)
  296. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  297. return req
  298. }
  299. type deleteAction struct {
  300. Prefix string
  301. Key string
  302. Value string
  303. PrevValue string
  304. PrevIndex uint64
  305. Recursive bool
  306. }
  307. func (a *deleteAction) HTTPRequest(ep url.URL) *http.Request {
  308. u := v2KeysURL(ep, a.Prefix, a.Key)
  309. params := u.Query()
  310. if a.PrevValue != "" {
  311. params.Set("prevValue", a.PrevValue)
  312. }
  313. if a.PrevIndex != 0 {
  314. params.Set("prevIndex", strconv.FormatUint(a.PrevIndex, 10))
  315. }
  316. if a.Recursive {
  317. params.Set("recursive", "true")
  318. }
  319. u.RawQuery = params.Encode()
  320. req, _ := http.NewRequest("DELETE", u.String(), nil)
  321. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  322. return req
  323. }
  324. func unmarshalHTTPResponse(code int, header http.Header, body []byte) (res *Response, err error) {
  325. switch code {
  326. case http.StatusOK, http.StatusCreated:
  327. res, err = unmarshalSuccessfulResponse(header, body)
  328. default:
  329. err = unmarshalErrorResponse(code)
  330. }
  331. return
  332. }
  333. func unmarshalSuccessfulResponse(header http.Header, body []byte) (*Response, error) {
  334. var res Response
  335. err := json.Unmarshal(body, &res)
  336. if err != nil {
  337. return nil, err
  338. }
  339. if header.Get("X-Etcd-Index") != "" {
  340. res.Index, err = strconv.ParseUint(header.Get("X-Etcd-Index"), 10, 64)
  341. }
  342. if err != nil {
  343. return nil, err
  344. }
  345. return &res, nil
  346. }
  347. func unmarshalErrorResponse(code int) error {
  348. switch code {
  349. case http.StatusNotFound:
  350. return ErrKeyNoExist
  351. case http.StatusPreconditionFailed:
  352. return ErrKeyExists
  353. case http.StatusInternalServerError:
  354. // this isn't necessarily true
  355. return ErrNoLeader
  356. case http.StatusGatewayTimeout:
  357. return ErrTimeout
  358. default:
  359. }
  360. return fmt.Errorf("unrecognized HTTP status code %d", code)
  361. }