úterý 26. března 2019

Testing nonexistence in etcd v3+

In a project I use etcd as a persistent store of state information of the system. etcd is a great piece of software and suits my needs. Till today I tried to determin, how to code a transaction condition to test non existence.

Nothing is mentioned in the doc.

And it is suprisingly easy.

Just test version of the key/val. For nonexisting key it evaluates as equals to zero.

If you want to do a transaction testing existence of one key and non existence of the other you can use following  transaction:


#!/bin/sh

etcdctl del --prefix test-

etcdctl put test-xxx "ahoj"
etcdctl del test-yyy "lidi"

etcdctl txn -i <<__eof test-xxx="" ver=""> "0"
ver("test-yyy") = "0" 

put test-success yeah

put test-failure nay

__EOF

etcdctl get --prefix test-