|
|
|
@ -54,8 +54,55 @@ etcd_kv_t etcd_state_client_t::parse_etcd_kv(const json11::Json & kv_json) |
|
|
|
|
return kv; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
json11::Json etcd_state_client_t::etcd_encode_actions(const json11::Json & items) |
|
|
|
|
{ |
|
|
|
|
json11::Json::array encoded; |
|
|
|
|
for (auto & v: items.array_items()) |
|
|
|
|
{ |
|
|
|
|
json11::Json::object act; |
|
|
|
|
for (auto & kv: v.object_items()) |
|
|
|
|
{ |
|
|
|
|
if (kv.first == "key" || kv.first == "range_end") |
|
|
|
|
act[kv.first] = base64_encode(etcd_prefix+kv.second.string_value()); |
|
|
|
|
else if (kv.first == "value") |
|
|
|
|
act[kv.first] = base64_encode(kv.second.is_string() ? kv.second.string_value() : kv.second.dump()); |
|
|
|
|
else |
|
|
|
|
act[kv.first] = kv.second; |
|
|
|
|
} |
|
|
|
|
encoded.push_back(act); |
|
|
|
|
} |
|
|
|
|
return encoded; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void etcd_state_client_t::etcd_txn(json11::Json txn, int timeout, int retries, int interval, std::function<void(std::string, json11::Json)> callback) |
|
|
|
|
{ |
|
|
|
|
// FIXME: json11 is immutable which is very inconvenient for such cases
|
|
|
|
|
json11::Json::object encoded; |
|
|
|
|
if (txn["compare"].is_array()) |
|
|
|
|
{ |
|
|
|
|
json11::Json::array compare; |
|
|
|
|
for (auto & v: txn["compare"].array_items()) |
|
|
|
|
{ |
|
|
|
|
json11::Json::object cmp; |
|
|
|
|
for (auto & kv: v.object_items()) |
|
|
|
|
{ |
|
|
|
|
if (kv.first == "key") |
|
|
|
|
cmp[kv.first] = base64_encode(etcd_prefix+kv.second.string_value()); |
|
|
|
|
else |
|
|
|
|
cmp[kv.first] = kv.second; |
|
|
|
|
} |
|
|
|
|
compare.push_back(cmp); |
|
|
|
|
} |
|
|
|
|
encoded["compare"] = compare; |
|
|
|
|
} |
|
|
|
|
if (txn["failure"].is_array()) |
|
|
|
|
{ |
|
|
|
|
encoded["failure"] = etcd_encode_actions(txn["failure"]); |
|
|
|
|
} |
|
|
|
|
if (txn["success"].is_array()) |
|
|
|
|
{ |
|
|
|
|
encoded["success"] = etcd_encode_actions(txn["success"]); |
|
|
|
|
} |
|
|
|
|
etcd_call("/kv/txn", txn, timeout, retries, interval, callback); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|