|
@@ -23,11 +23,18 @@ import (
|
|
|
"path"
|
|
"path"
|
|
|
"strconv"
|
|
"strconv"
|
|
|
"strings"
|
|
"strings"
|
|
|
- "time"
|
|
|
|
|
|
|
|
|
|
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
|
|
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+type PrevExistType string
|
|
|
|
|
+
|
|
|
|
|
+const (
|
|
|
|
|
+ PrevIgnore = PrevExistType("")
|
|
|
|
|
+ PrevExist = PrevExistType("true")
|
|
|
|
|
+ PrevNoExist = PrevExistType("false")
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
var (
|
|
var (
|
|
|
DefaultV2KeysPrefix = "/v2/keys"
|
|
DefaultV2KeysPrefix = "/v2/keys"
|
|
|
)
|
|
)
|
|
@@ -54,13 +61,17 @@ func NewDiscoveryKeysAPI(c HTTPClient) KeysAPI {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type KeysAPI interface {
|
|
type KeysAPI interface {
|
|
|
- Create(ctx context.Context, key, value string, ttl time.Duration) (*Response, error)
|
|
|
|
|
|
|
+ Create(ctx context.Context, key, value string) (*Response, error)
|
|
|
Get(ctx context.Context, key string) (*Response, error)
|
|
Get(ctx context.Context, key string) (*Response, error)
|
|
|
|
|
|
|
|
Watch(key string, idx uint64) Watcher
|
|
Watch(key string, idx uint64) Watcher
|
|
|
RecursiveWatch(key string, idx uint64) Watcher
|
|
RecursiveWatch(key string, idx uint64) Watcher
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+type SetOptions struct {
|
|
|
|
|
+ PrevExist PrevExistType
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
type Watcher interface {
|
|
type Watcher interface {
|
|
|
Next(context.Context) (*Response, error)
|
|
Next(context.Context) (*Response, error)
|
|
|
}
|
|
}
|
|
@@ -90,18 +101,17 @@ type httpKeysAPI struct {
|
|
|
prefix string
|
|
prefix string
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (k *httpKeysAPI) Create(ctx context.Context, key, val string, ttl time.Duration) (*Response, error) {
|
|
|
|
|
- create := &createAction{
|
|
|
|
|
|
|
+func (k *httpKeysAPI) Create(ctx context.Context, key, val string) (*Response, error) {
|
|
|
|
|
+ act := &setAction{
|
|
|
Prefix: k.prefix,
|
|
Prefix: k.prefix,
|
|
|
Key: key,
|
|
Key: key,
|
|
|
Value: val,
|
|
Value: val,
|
|
|
- }
|
|
|
|
|
- if ttl >= 0 {
|
|
|
|
|
- uttl := uint64(ttl.Seconds())
|
|
|
|
|
- create.TTL = &uttl
|
|
|
|
|
|
|
+ Options: SetOptions{
|
|
|
|
|
+ PrevExist: PrevNoExist,
|
|
|
|
|
+ },
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- resp, body, err := k.client.Do(ctx, create)
|
|
|
|
|
|
|
+ resp, body, err := k.client.Do(ctx, act)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
@@ -215,25 +225,24 @@ func (w *waitAction) HTTPRequest(ep url.URL) *http.Request {
|
|
|
return req
|
|
return req
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-type createAction struct {
|
|
|
|
|
- Prefix string
|
|
|
|
|
- Key string
|
|
|
|
|
- Value string
|
|
|
|
|
- TTL *uint64
|
|
|
|
|
|
|
+type setAction struct {
|
|
|
|
|
+ Prefix string
|
|
|
|
|
+ Key string
|
|
|
|
|
+ Value string
|
|
|
|
|
+ Options SetOptions
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (c *createAction) HTTPRequest(ep url.URL) *http.Request {
|
|
|
|
|
- u := v2KeysURL(ep, c.Prefix, c.Key)
|
|
|
|
|
|
|
+func (a *setAction) HTTPRequest(ep url.URL) *http.Request {
|
|
|
|
|
+ u := v2KeysURL(ep, a.Prefix, a.Key)
|
|
|
|
|
|
|
|
params := u.Query()
|
|
params := u.Query()
|
|
|
- params.Set("prevExist", "false")
|
|
|
|
|
|
|
+ if a.Options.PrevExist != PrevIgnore {
|
|
|
|
|
+ params.Set("prevExist", string(a.Options.PrevExist))
|
|
|
|
|
+ }
|
|
|
u.RawQuery = params.Encode()
|
|
u.RawQuery = params.Encode()
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
form := url.Values{}
|
|
|
- form.Add("value", c.Value)
|
|
|
|
|
- if c.TTL != nil {
|
|
|
|
|
- form.Add("ttl", strconv.FormatUint(*c.TTL, 10))
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ form.Add("value", a.Value)
|
|
|
body := strings.NewReader(form.Encode())
|
|
body := strings.NewReader(form.Encode())
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", u.String(), body)
|
|
req, _ := http.NewRequest("PUT", u.String(), body)
|