python-consul
python-consul copied to clipboard
sort kv.get by ModifyIndex
hi
It will be perfect if results returned by kv.get can be sorted by ModifyIndex
Currently i have such code
index, responce = self.consul_api.kv.get(path, recurse=True)
if not responce:
return
for resp in responce:
if resp['Value']:
try:
metadata = json.loads(resp['Value'])
if not isinstance(metadata, dict):
raise ValueError
except (ValueError, AttributeError):
key = resp['Key'].split("/")[-1]
metadata = {
key: resp['Value']
}
for k, v in metadata.items():
self.add_metadata(node_data, k, v)
and such structure in consul
{
"Key": "some_key",
"CreateIndex": 4344,
"ModifyIndex": 4412,
"LockIndex": 0,
"Flags": 0,
"Value": "{\"weight\":\"12\"},
"Session": ""
},
{
"Key": "some_key/weight",
"CreateIndex": 4401,
"ModifyIndex": 4401,
"LockIndex": 0,
"Flags": 0,
"Value": "4",
"Session": ""
}
looking to ModifyIndex we understand that value should be 4. but we got 12 instead.
Hi there. I don't quite understand what you're trying to do, but I think your best bet is to sort the list after you get the response from Consul
https://wiki.python.org/moin/HowTo/Sorting#Operator_Module_Functions