diff --git a/client/keys.go b/client/keys.go index 68f15f56d..19cec1c87 100644 --- a/client/keys.go +++ b/client/keys.go @@ -23,6 +23,7 @@ import ( "path" "strconv" "strings" + "time" "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" ) @@ -82,6 +83,7 @@ type SetOptions struct { PrevValue string PrevIndex uint64 PrevExist PrevExistType + TTL time.Duration } type DeleteOptions struct { @@ -130,6 +132,7 @@ func (k *httpKeysAPI) Set(ctx context.Context, key, val string, opts *SetOptions act.PrevValue = opts.PrevValue act.PrevIndex = opts.PrevIndex act.PrevExist = opts.PrevExist + act.TTL = opts.TTL } resp, body, err := k.client.Do(ctx, act) @@ -289,6 +292,7 @@ type setAction struct { PrevValue string PrevIndex uint64 PrevExist PrevExistType + TTL time.Duration } func (a *setAction) HTTPRequest(ep url.URL) *http.Request { @@ -308,6 +312,9 @@ func (a *setAction) HTTPRequest(ep url.URL) *http.Request { form := url.Values{} form.Add("value", a.Value) + if a.TTL > 0 { + form.Add("ttl", strconv.FormatUint(uint64(a.TTL.Seconds()), 10)) + } body := strings.NewReader(form.Encode()) req, _ := http.NewRequest("PUT", u.String(), body) diff --git a/client/keys_test.go b/client/keys_test.go index 666133eab..d4ace0703 100644 --- a/client/keys_test.go +++ b/client/keys_test.go @@ -22,6 +22,7 @@ import ( "net/url" "reflect" "testing" + "time" ) func TestV2KeysURLHelper(t *testing.T) { @@ -303,6 +304,16 @@ func TestSetAction(t *testing.T) { wantURL: "http://example.com/foo?prevIndex=12", wantBody: "value=", }, + + // TTL is set + { + act: setAction{ + Key: "foo", + TTL: 3 * time.Minute, + }, + wantURL: "http://example.com/foo", + wantBody: "ttl=180&value=", + }, } for i, tt := range tests {