keys.go 8.9 KB

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