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"
|
"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"
|
||||||
)
|
)
|
||||||
@ -82,6 +83,7 @@ type SetOptions struct {
|
|||||||
PrevValue string
|
PrevValue string
|
||||||
PrevIndex uint64
|
PrevIndex uint64
|
||||||
PrevExist PrevExistType
|
PrevExist PrevExistType
|
||||||
|
TTL time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeleteOptions struct {
|
type DeleteOptions struct {
|
||||||
@ -130,6 +132,7 @@ func (k *httpKeysAPI) Set(ctx context.Context, key, val string, opts *SetOptions
|
|||||||
act.PrevValue = opts.PrevValue
|
act.PrevValue = opts.PrevValue
|
||||||
act.PrevIndex = opts.PrevIndex
|
act.PrevIndex = opts.PrevIndex
|
||||||
act.PrevExist = opts.PrevExist
|
act.PrevExist = opts.PrevExist
|
||||||
|
act.TTL = opts.TTL
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, body, err := k.client.Do(ctx, act)
|
resp, body, err := k.client.Do(ctx, act)
|
||||||
@ -289,6 +292,7 @@ type setAction struct {
|
|||||||
PrevValue string
|
PrevValue string
|
||||||
PrevIndex uint64
|
PrevIndex uint64
|
||||||
PrevExist PrevExistType
|
PrevExist PrevExistType
|
||||||
|
TTL time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *setAction) HTTPRequest(ep url.URL) *http.Request {
|
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 := url.Values{}
|
||||||
form.Add("value", a.Value)
|
form.Add("value", a.Value)
|
||||||
|
if a.TTL > 0 {
|
||||||
|
form.Add("ttl", strconv.FormatUint(uint64(a.TTL.Seconds()), 10))
|
||||||
|
}
|
||||||
body := strings.NewReader(form.Encode())
|
body := strings.NewReader(form.Encode())
|
||||||
|
|
||||||
req, _ := http.NewRequest("PUT", u.String(), body)
|
req, _ := http.NewRequest("PUT", u.String(), body)
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestV2KeysURLHelper(t *testing.T) {
|
func TestV2KeysURLHelper(t *testing.T) {
|
||||||
@ -303,6 +304,16 @@ func TestSetAction(t *testing.T) {
|
|||||||
wantURL: "http://example.com/foo?prevIndex=12",
|
wantURL: "http://example.com/foo?prevIndex=12",
|
||||||
wantBody: "value=",
|
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 {
|
for i, tt := range tests {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user