k3s/vendor/github.com/rancher/kine/pkg/server/update.go
Darren Shepherd a005219c5b Update vendor
2019-08-22 09:14:47 -07:00

73 lines
1.6 KiB
Go

package server
import (
"context"
"github.com/coreos/etcd/etcdserver/etcdserverpb"
)
func isUpdate(txn *etcdserverpb.TxnRequest) (int64, string, []byte, int64, bool) {
if len(txn.Compare) == 1 &&
txn.Compare[0].Target == etcdserverpb.Compare_MOD &&
txn.Compare[0].Result == etcdserverpb.Compare_EQUAL &&
len(txn.Success) == 1 &&
txn.Success[0].GetRequestPut() != nil &&
len(txn.Failure) == 1 &&
txn.Failure[0].GetRequestRange() != nil {
return txn.Compare[0].GetModRevision(),
string(txn.Compare[0].Key),
txn.Success[0].GetRequestPut().Value,
txn.Success[0].GetRequestPut().Lease,
true
}
return 0, "", nil, 0, false
}
func (l *LimitedServer) update(ctx context.Context, rev int64, key string, value []byte, lease int64) (*etcdserverpb.TxnResponse, error) {
var (
kv *KeyValue
ok bool
err error
)
if rev == 0 {
rev, err = l.backend.Create(ctx, key, value, lease)
ok = true
} else {
rev, kv, ok, err = l.backend.Update(ctx, key, value, rev, lease)
}
if err != nil {
return nil, err
}
resp := &etcdserverpb.TxnResponse{
Header: txnHeader(rev),
Succeeded: ok,
}
if ok {
resp.Responses = []*etcdserverpb.ResponseOp{
{
Response: &etcdserverpb.ResponseOp_ResponsePut{
ResponsePut: &etcdserverpb.PutResponse{
Header: txnHeader(rev),
},
},
},
}
} else {
resp.Responses = []*etcdserverpb.ResponseOp{
{
Response: &etcdserverpb.ResponseOp_ResponseRange{
ResponseRange: &etcdserverpb.RangeResponse{
Header: txnHeader(rev),
Kvs: toKVs(kv),
},
},
},
}
}
return resp, nil
}