keys.go 8.7 KB

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