GMHDBJD
GMHDBJD

Reputation: 149

How to delete key in etcd with revision

I want to delete a key in etcd while others put the key into etcd. So I want to specify the revision when delete. If key version is lower then the revision, then delete it. Otherwise do nothing.

// putInfoOp returns a PUT etcd operation for Info.
func putInfoOp(info Info) (clientv3.Op, error) {
    value, err := info.toJSON()
    if err != nil {
        return clientv3.Op{}, err
    }
    key := KeyAdapter.Encode(info.A, info.B)
    return clientv3.OpPut(key, value), nil
}

// deleteInfoOp returns a DELETE etcd operation for info.
func deleteInfoOp(info Info) clientv3.Op {
    return clientv3.OpDelete(KeyAdapter.Encode(
        info.A, info.B))
}

// my code
func handleinfo(info Info){
    putOp, _ := putInfoOp(info)
    _, rev, err := etcdutil.DoOpsInOneTxnWithRetry(cli, putOp)
    
    dosomething()
    
    delOp, _ := deleteInfoOp(Info) // how to delete it with rev?
}

Upvotes: 0

Views: 1243

Answers (1)

Related Questions