client.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package client
  14. import (
  15. "errors"
  16. "fmt"
  17. "time"
  18. )
  19. var (
  20. ErrUnavailable = errors.New("client: no available etcd endpoints")
  21. ErrNoLeader = errors.New("client: no leader")
  22. ErrKeyNoExist = errors.New("client: key does not exist")
  23. ErrKeyExists = errors.New("client: key already exists")
  24. )
  25. type Client interface {
  26. Create(key, value string, ttl time.Duration) (*Response, error)
  27. Get(key string) (*Response, error)
  28. Watch(key string, idx uint64) Watcher
  29. RecursiveWatch(key string, idx uint64) Watcher
  30. }
  31. type Watcher interface {
  32. Next() (*Response, error)
  33. }
  34. type Response struct {
  35. Action string `json:"action"`
  36. Node *Node `json:"node"`
  37. PrevNode *Node `json:"prevNode"`
  38. }
  39. type Nodes []*Node
  40. type Node struct {
  41. Key string `json:"key"`
  42. Value string `json:"value"`
  43. Nodes Nodes `json:"nodes"`
  44. ModifiedIndex uint64 `json:"modifiedIndex"`
  45. CreatedIndex uint64 `json:"createdIndex"`
  46. }
  47. func (n *Node) String() string {
  48. return fmt.Sprintf("{Key: %s, CreatedIndex: %d, ModifiedIndex: %d}", n.Key, n.CreatedIndex, n.ModifiedIndex)
  49. }