keys.go 8.7 KB

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