keys.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. Watch(key string, idx uint64) Watcher
  61. RWatch(key string, idx uint64) Watcher
  62. }
  63. type SetOptions struct {
  64. PrevValue string
  65. PrevExist PrevExistType
  66. }
  67. type DeleteOptions struct {
  68. PrevValue string
  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 Nodes []*Node
  81. type Node struct {
  82. Key string `json:"key"`
  83. Value string `json:"value"`
  84. Nodes Nodes `json:"nodes"`
  85. ModifiedIndex uint64 `json:"modifiedIndex"`
  86. CreatedIndex uint64 `json:"createdIndex"`
  87. }
  88. func (n *Node) String() string {
  89. return fmt.Sprintf("{Key: %s, CreatedIndex: %d, ModifiedIndex: %d}", n.Key, n.CreatedIndex, n.ModifiedIndex)
  90. }
  91. type httpKeysAPI struct {
  92. client HTTPClient
  93. prefix string
  94. }
  95. func (k *httpKeysAPI) Set(ctx context.Context, key, val string, opts SetOptions) (*Response, error) {
  96. act := &setAction{
  97. Prefix: k.prefix,
  98. Key: key,
  99. Value: val,
  100. Options: opts,
  101. }
  102. resp, body, err := k.client.Do(ctx, act)
  103. if err != nil {
  104. return nil, err
  105. }
  106. return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
  107. }
  108. func (k *httpKeysAPI) Create(ctx context.Context, key, val string) (*Response, error) {
  109. return k.Set(ctx, key, val, SetOptions{PrevExist: PrevNoExist})
  110. }
  111. func (k *httpKeysAPI) Update(ctx context.Context, key, val string) (*Response, error) {
  112. return k.Set(ctx, key, val, SetOptions{PrevExist: PrevExist})
  113. }
  114. func (k *httpKeysAPI) Delete(ctx context.Context, key string, opts DeleteOptions) (*Response, error) {
  115. act := &deleteAction{
  116. Prefix: k.prefix,
  117. Key: key,
  118. Options: opts,
  119. }
  120. resp, body, err := k.client.Do(ctx, act)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
  125. }
  126. func (k *httpKeysAPI) Get(ctx context.Context, key string) (*Response, error) {
  127. get := &getAction{
  128. Prefix: k.prefix,
  129. Key: key,
  130. Recursive: false,
  131. }
  132. resp, body, err := k.client.Do(ctx, get)
  133. if err != nil {
  134. return nil, err
  135. }
  136. return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
  137. }
  138. func (k *httpKeysAPI) RGet(ctx context.Context, key string) (*Response, error) {
  139. get := &getAction{
  140. Prefix: k.prefix,
  141. Key: key,
  142. Recursive: true,
  143. }
  144. resp, body, err := k.client.Do(ctx, get)
  145. if err != nil {
  146. return nil, err
  147. }
  148. return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
  149. }
  150. func (k *httpKeysAPI) Watch(key string, idx uint64) Watcher {
  151. return &httpWatcher{
  152. client: k.client,
  153. nextWait: waitAction{
  154. Prefix: k.prefix,
  155. Key: key,
  156. WaitIndex: idx,
  157. Recursive: false,
  158. },
  159. }
  160. }
  161. func (k *httpKeysAPI) RWatch(key string, idx uint64) Watcher {
  162. return &httpWatcher{
  163. client: k.client,
  164. nextWait: waitAction{
  165. Prefix: k.prefix,
  166. Key: key,
  167. WaitIndex: idx,
  168. Recursive: true,
  169. },
  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. Options SetOptions
  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.Options.PrevValue != "" {
  236. params.Set("prevValue", a.Options.PrevValue)
  237. }
  238. if a.Options.PrevExist != PrevIgnore {
  239. params.Set("prevExist", string(a.Options.PrevExist))
  240. }
  241. u.RawQuery = params.Encode()
  242. form := url.Values{}
  243. form.Add("value", a.Value)
  244. body := strings.NewReader(form.Encode())
  245. req, _ := http.NewRequest("PUT", u.String(), body)
  246. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  247. return req
  248. }
  249. type deleteAction struct {
  250. Prefix string
  251. Key string
  252. Value string
  253. Options DeleteOptions
  254. }
  255. func (a *deleteAction) HTTPRequest(ep url.URL) *http.Request {
  256. u := v2KeysURL(ep, a.Prefix, a.Key)
  257. params := u.Query()
  258. if a.Options.PrevValue != "" {
  259. params.Set("prevValue", a.Options.PrevValue)
  260. }
  261. if a.Options.Recursive {
  262. params.Set("recursive", "true")
  263. }
  264. u.RawQuery = params.Encode()
  265. req, _ := http.NewRequest("DELETE", u.String(), nil)
  266. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  267. return req
  268. }
  269. func unmarshalHTTPResponse(code int, header http.Header, body []byte) (res *Response, err error) {
  270. switch code {
  271. case http.StatusOK, http.StatusCreated:
  272. res, err = unmarshalSuccessfulResponse(header, body)
  273. default:
  274. err = unmarshalErrorResponse(code)
  275. }
  276. return
  277. }
  278. func unmarshalSuccessfulResponse(header http.Header, body []byte) (*Response, error) {
  279. var res Response
  280. err := json.Unmarshal(body, &res)
  281. if err != nil {
  282. return nil, err
  283. }
  284. if header.Get("X-Etcd-Index") != "" {
  285. res.Index, err = strconv.ParseUint(header.Get("X-Etcd-Index"), 10, 64)
  286. }
  287. if err != nil {
  288. return nil, err
  289. }
  290. return &res, nil
  291. }
  292. func unmarshalErrorResponse(code int) error {
  293. switch code {
  294. case http.StatusNotFound:
  295. return ErrKeyNoExist
  296. case http.StatusPreconditionFailed:
  297. return ErrKeyExists
  298. case http.StatusInternalServerError:
  299. // this isn't necessarily true
  300. return ErrNoLeader
  301. case http.StatusGatewayTimeout:
  302. return ErrTimeout
  303. default:
  304. }
  305. return fmt.Errorf("unrecognized HTTP status code %d", code)
  306. }