diff options
author | Giuseppe Lavagetto <lavagetto@users.noreply.github.com> | 2015-06-10 12:00:21 +0200 |
---|---|---|
committer | Giuseppe Lavagetto <lavagetto@users.noreply.github.com> | 2015-06-10 12:00:21 +0200 |
commit | ac25bd7ba21963c02457aba023fef0a6a885e46d (patch) | |
tree | e704f48597ce82ba366ab1dab7777cd32e087d75 /src | |
parent | 9f676c67812d0bb75eefb13a7336a8947073e137 (diff) | |
parent | e68572e5297abbeacac98366d5a0601f2f1c27cc (diff) |
Merge pull request #97 from offscale/master
Implemented `pop`
Diffstat (limited to 'src')
-rw-r--r-- | src/etcd/__init__.py | 2 | ||||
-rw-r--r-- | src/etcd/client.py | 31 | ||||
-rw-r--r-- | src/etcd/tests/unit/test_request.py | 20 |
3 files changed, 53 insertions, 0 deletions
diff --git a/src/etcd/__init__.py b/src/etcd/__init__.py index 0d877d6..370be42 100644 --- a/src/etcd/__init__.py +++ b/src/etcd/__init__.py @@ -36,6 +36,8 @@ class EtcdResult(object): node (dict): The dictionary containing all node information. + prevNode (dict): The dictionary containing previous node information. + """ self.action = action for (key, default) in self._node_props.items(): diff --git a/src/etcd/client.py b/src/etcd/client.py index e4376b2..a578add 100644 --- a/src/etcd/client.py +++ b/src/etcd/client.py @@ -523,6 +523,37 @@ class Client(object): self.key_endpoint + key, self._MDELETE, params=kwds) return self._result_from_response(response) + def pop(self, key, recursive=None, dir=None, **kwdargs): + """ + Remove specified key from etcd and return the corresponding value. + + Args: + + key (str): Key. + + recursive (bool): if we want to recursively delete a directory, set + it to true + + dir (bool): if we want to delete a directory, set it to true + + prevValue (str): compare key to this value, and swap only if + corresponding (optional). + + prevIndex (int): modify key only if actual modifiedIndex matches the + provided one (optional). + + Returns: + client.EtcdResult + + Raises: + KeyValue: If the key doesn't exists. + + >>> print client.pop('/key').value + 'value' + + """ + return self.delete(key=key, recursive=recursive, dir=dir, **kwdargs)._prev_node + # Higher-level methods on top of the basic primitives def test_and_set(self, key, value, prev_value, ttl=None): """ diff --git a/src/etcd/tests/unit/test_request.py b/src/etcd/tests/unit/test_request.py index f30e0d1..beee6ec 100644 --- a/src/etcd/tests/unit/test_request.py +++ b/src/etcd/tests/unit/test_request.py @@ -310,6 +310,26 @@ class TestClientApiInterface(TestClientApiBase): res = self.client.delete('/testKey') self.assertEquals(res, etcd.EtcdResult(**d)) + def test_pop(self): + """ Can pop a value """ + d = { + u'action': u'delete', + u'node': { + u'key': u'/testkey', + u'modifiedIndex': 3, + u'createdIndex': 2 + }, + u'prevNode': {u'newKey': False, u'createdIndex': None, + u'modifiedIndex': 190, u'value': u'test', u'expiration': None, + u'key': u'/testkey', u'ttl': None, u'dir': False} + } + + self._mock_api(200, d) + res = self.client.pop(d['node']['key']) + self.assertEquals({attr: getattr(res, attr) for attr in dir(res) + if attr in etcd.EtcdResult._node_props}, d['prevNode']) + self.assertEqual(res.value, d['prevNode']['value']) + def test_read(self): """ Can get a value """ d = { |