Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the API to get all the keys (not values) in etcd3? #362

Closed
vhosakot opened this issue Mar 3, 2018 · 5 comments
Closed

What is the API to get all the keys (not values) in etcd3? #362

vhosakot opened this issue Mar 3, 2018 · 5 comments

Comments

@vhosakot
Copy link

vhosakot commented Mar 3, 2018

The get_all() API returns the values of all the keys, but not the keys themselves.

After putting the following keys and values, what is the API to get a list of just the keys /foo, /bar, /baz and /qux?

>>> etcd.put("/foo", "a")
>>> etcd.put("/bar", "b")
>>> etcd.put("/baz", "c")
>>> etcd.put("/qux", "d")

I was not able to find this in the documentation in https://python-etcd3.readthedocs.io/en/latest/usage.html.

It is possible to get just the keys in etcd3 using etcdctl the following way:

ETCDCTL_API=3 etcdctl get --prefix=true "" | grep '/'
/bar
/baz
/foo
/qux

But, I wasn't able to find the python API to do the above. Let me know if I'm missing anything.

@vhosakot vhosakot changed the title What is the API to get all the keys (not values) in etcd3 What is the API to get all the keys (not values) in etcd3? Mar 3, 2018
@vhosakot
Copy link
Author

vhosakot commented Mar 5, 2018

/cc @kragniz

@vhosakot
Copy link
Author

/cc @kragniz any thoughts?

@kragniz
Copy link
Owner

kragniz commented Mar 12, 2018

There isn't a method to do this, but you can get all and extract only the keys:

>>> c = etcd3.client()
>>> [m.key for (_, m) in c.get_all()]
[b'/bar', b'/baz', b'/foo', b'/qux'] 

@kragniz
Copy link
Owner

kragniz commented Mar 12, 2018

Maybe a utility function for this would be useful, if you'd like to submit a pr

@vhosakot
Copy link
Author

Thanks @kragniz, that works!

>>> etcd.put("/foo", "a")
>>> etcd.put("/bar", "b")
>>> etcd.put("/baz", "c")
>>> etcd.put("/qux", "d")

>>> [m.key for (_, m) in etcd.get_all()]
['/bar', '/baz', '/foo', '/qux']

>>> for (_,m) in etcd.get_all():
...     print m.key
... 
/bar
/baz
/foo
/qux

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants