keys.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 uint64
  55. Recursive bool
  56. }
  57. type SetOptions struct {
  58. PrevValue string
  59. PrevIndex uint64
  60. PrevExist PrevExistType
  61. TTL time.Duration
  62. }
  63. type DeleteOptions struct {
  64. PrevValue string
  65. PrevIndex uint64
  66. Recursive bool
  67. }
  68. type Watcher interface {
  69. Next(context.Context) (*Response, error)
  70. }
  71. type Response struct {
  72. Action string `json:"action"`
  73. Node *Node `json:"node"`
  74. PrevNode *Node `json:"prevNode"`
  75. Index uint64
  76. }
  77. type Node struct {
  78. Key string `json:"key"`
  79. Value string `json:"value"`
  80. Nodes []*Node `json:"nodes"`
  81. ModifiedIndex uint64 `json:"modifiedIndex"`
  82. CreatedIndex uint64 `json:"createdIndex"`
  83. }
  84. func (n *Node) String() string {
  85. return fmt.Sprintf("{Key: %s, CreatedIndex: %d, ModifiedIndex: %d}", n.Key, n.CreatedIndex, n.ModifiedIndex)
  86. }
  87. type httpKeysAPI struct {
  88. client httpClient
  89. prefix string
  90. }
  91. func (k *httpKeysAPI) Set(ctx context.Context, key, val string, opts *SetOptions) (*Response, error) {
  92. act := &setAction{
  93. Prefix: k.prefix,
  94. Key: key,
  95. Value: val,
  96. }
  97. if opts != nil {
  98. act.PrevValue = opts.PrevValue
  99. act.PrevIndex = opts.PrevIndex
  100. act.PrevExist = opts.PrevExist
  101. act.TTL = opts.TTL
  102. }
  103. resp, body, err := k.client.Do(ctx, act)
  104. if err != nil {
  105. return nil, err
  106. }
  107. return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
  108. }
  109. func (k *httpKeysAPI) Create(ctx context.Context, key, val string) (*Response, error) {
  110. return k.Set(ctx, key, val, &SetOptions{PrevExist: PrevNoExist})
  111. }
  112. func (k *httpKeysAPI) Update(ctx context.Context, key, val string) (*Response, error) {
  113. return k.Set(ctx, key, val, &SetOptions{PrevExist: PrevExist})
  114. }
  115. func (k *httpKeysAPI) Delete(ctx context.Context, key string, opts *DeleteOptions) (*Response, error) {
  116. act := &deleteAction{
  117. Prefix: k.prefix,
  118. Key: key,
  119. }
  120. if opts != nil {
  121. act.PrevValue = opts.PrevValue
  122. act.PrevIndex = opts.PrevIndex
  123. act.Recursive = opts.Recursive
  124. }
  125. resp, body, err := k.client.Do(ctx, act)
  126. if err != nil {
  127. return nil, err
  128. }
  129. return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
  130. }
  131. func (k *httpKeysAPI) Get(ctx context.Context, key string) (*Response, error) {
  132. get := &getAction{
  133. Prefix: k.prefix,
  134. Key: key,
  135. Recursive: false,
  136. }
  137. resp, body, err := k.client.Do(ctx, get)
  138. if err != nil {
  139. return nil, err
  140. }
  141. return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
  142. }
  143. func (k *httpKeysAPI) RGet(ctx context.Context, key string) (*Response, error) {
  144. get := &getAction{
  145. Prefix: k.prefix,
  146. Key: key,
  147. Recursive: true,
  148. }
  149. resp, body, err := k.client.Do(ctx, get)
  150. if err != nil {
  151. return nil, err
  152. }
  153. return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
  154. }
  155. func (k *httpKeysAPI) Watcher(key string, opts *WatcherOptions) Watcher {
  156. act := waitAction{
  157. Prefix: k.prefix,
  158. Key: key,
  159. }
  160. if opts != nil {
  161. act.WaitIndex = opts.WaitIndex
  162. act.Recursive = opts.Recursive
  163. }
  164. return &httpWatcher{
  165. client: k.client,
  166. nextWait: act,
  167. }
  168. }
  169. type httpWatcher struct {
  170. client httpClient
  171. nextWait waitAction
  172. }
  173. func (hw *httpWatcher) Next(ctx context.Context) (*Response, error) {
  174. httpresp, body, err := hw.client.Do(ctx, &hw.nextWait)
  175. if err != nil {
  176. return nil, err
  177. }
  178. resp, err := unmarshalHTTPResponse(httpresp.StatusCode, httpresp.Header, body)
  179. if err != nil {
  180. return nil, err
  181. }
  182. hw.nextWait.WaitIndex = resp.Node.ModifiedIndex + 1
  183. return resp, nil
  184. }
  185. // v2KeysURL forms a URL representing the location of a key.
  186. // The endpoint argument represents the base URL of an etcd
  187. // server. The prefix is the path needed to route from the
  188. // provided endpoint's path to the root of the keys API
  189. // (typically "/v2/keys").
  190. func v2KeysURL(ep url.URL, prefix, key string) *url.URL {
  191. ep.Path = path.Join(ep.Path, prefix, key)
  192. return &ep
  193. }
  194. type getAction struct {
  195. Prefix string
  196. Key string
  197. Recursive bool
  198. }
  199. func (g *getAction) HTTPRequest(ep url.URL) *http.Request {
  200. u := v2KeysURL(ep, g.Prefix, g.Key)
  201. params := u.Query()
  202. params.Set("recursive", strconv.FormatBool(g.Recursive))
  203. u.RawQuery = params.Encode()
  204. req, _ := http.NewRequest("GET", u.String(), nil)
  205. return req
  206. }
  207. type waitAction struct {
  208. Prefix string
  209. Key string
  210. WaitIndex uint64
  211. Recursive bool
  212. }
  213. func (w *waitAction) HTTPRequest(ep url.URL) *http.Request {
  214. u := v2KeysURL(ep, w.Prefix, w.Key)
  215. params := u.Query()
  216. params.Set("wait", "true")
  217. params.Set("waitIndex", strconv.FormatUint(w.WaitIndex, 10))
  218. params.Set("recursive", strconv.FormatBool(w.Recursive))
  219. u.RawQuery = params.Encode()
  220. req, _ := http.NewRequest("GET", u.String(), nil)
  221. return req
  222. }
  223. type setAction struct {
  224. Prefix string
  225. Key string
  226. Value string
  227. PrevValue string
  228. PrevIndex uint64
  229. PrevExist PrevExistType
  230. TTL time.Duration
  231. }
  232. func (a *setAction) HTTPRequest(ep url.URL) *http.Request {
  233. u := v2KeysURL(ep, a.Prefix, a.Key)
  234. params := u.Query()
  235. if a.PrevValue != "" {
  236. params.Set("prevValue", a.PrevValue)
  237. }
  238. if a.PrevIndex != 0 {
  239. params.Set("prevIndex", strconv.FormatUint(a.PrevIndex, 10))
  240. }
  241. if a.PrevExist != PrevIgnore {
  242. params.Set("prevExist", string(a.PrevExist))
  243. }
  244. u.RawQuery = params.Encode()
  245. form := url.Values{}
  246. form.Add("value", a.Value)
  247. if a.TTL > 0 {
  248. form.Add("ttl", strconv.FormatUint(uint64(a.TTL.Seconds()), 10))
  249. }
  250. body := strings.NewReader(form.Encode())
  251. req, _ := http.NewRequest("PUT", u.String(), body)
  252. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  253. return req
  254. }
  255. type deleteAction struct {
  256. Prefix string
  257. Key string
  258. Value string
  259. PrevValue string
  260. PrevIndex uint64
  261. Recursive bool
  262. }
  263. func (a *deleteAction) HTTPRequest(ep url.URL) *http.Request {
  264. u := v2KeysURL(ep, a.Prefix, a.Key)
  265. params := u.Query()
  266. if a.PrevValue != "" {
  267. params.Set("prevValue", a.PrevValue)
  268. }
  269. if a.PrevIndex != 0 {
  270. params.Set("prevIndex", strconv.FormatUint(a.PrevIndex, 10))
  271. }
  272. if a.Recursive {
  273. params.Set("recursive", "true")
  274. }
  275. u.RawQuery = params.Encode()
  276. req, _ := http.NewRequest("DELETE", u.String(), nil)
  277. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  278. return req
  279. }
  280. func unmarshalHTTPResponse(code int, header http.Header, body []byte) (res *Response, err error) {
  281. switch code {
  282. case http.StatusOK, http.StatusCreated:
  283. res, err = unmarshalSuccessfulResponse(header, body)
  284. default:
  285. err = unmarshalErrorResponse(code)
  286. }
  287. return
  288. }
  289. func unmarshalSuccessfulResponse(header http.Header, body []byte) (*Response, error) {
  290. var res Response
  291. err := json.Unmarshal(body, &res)
  292. if err != nil {
  293. return nil, err
  294. }
  295. if header.Get("X-Etcd-Index") != "" {
  296. res.Index, err = strconv.ParseUint(header.Get("X-Etcd-Index"), 10, 64)
  297. }
  298. if err != nil {
  299. return nil, err
  300. }
  301. return &res, nil
  302. }
  303. func unmarshalErrorResponse(code int) error {
  304. switch code {
  305. case http.StatusNotFound:
  306. return ErrKeyNoExist
  307. case http.StatusPreconditionFailed:
  308. return ErrKeyExists
  309. case http.StatusInternalServerError:
  310. // this isn't necessarily true
  311. return ErrNoLeader
  312. case http.StatusGatewayTimeout:
  313. return ErrTimeout
  314. default:
  315. }
  316. return fmt.Errorf("unrecognized HTTP status code %d", code)
  317. }