mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
client: accept TTL through KeysAPI.Set
This commit is contained in:
parent
3d53e9bfaa
commit
942f0f6b9e
@ -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)
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user