Browse Source

client: accept TTL through KeysAPI.Set

Brian Waldon 11 years ago
parent
commit
942f0f6b9e
2 changed files with 18 additions and 0 deletions
  1. 7 0
      client/keys.go
  2. 11 0
      client/keys_test.go

+ 7 - 0
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)

+ 11 - 0
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 {