mantl / consul-cli

Command line interface to Consul HTTP API
Apache License 2.0
251 stars 67 forks source link

kv bulkload doesn't accept JSONs generated by kv read #43

Closed SoTMaR closed 7 years ago

SoTMaR commented 7 years ago

Hi guys,

I need some assistance...

I'm trying to backup/restore my consul KV store via consul-cli and facing the following problem.

First, I "backup" the current KV store with consul-cli kv read:

(as JSON) consul-cli kv read "" --consul=XXX.YYY.com:8500 --recurse=true --format=json > export.json

RESULT [{"Key":"project/","CreateIndex":1730,"ModifyIndex":1730,"LockIndex":0,"Flags":0,"Value":"","Session":""},{"Key":"project/environment/some.other.property","CreateIndex":1736,"ModifyIndex":1736,"LockIndex":0,"Flags":0,"Value":"some.other.value","Session":""},{"Key":"project/environment/some.property","CreateIndex":1733,"ModifyIndex":1733,"LockIndex":0,"Flags":0,"Value":"some.value","Session":""}]

(as "pretty" JSON) consul-cli kv read "" --consul=XXX.YYY.com:8500 --recurse=true --format=prettyjson > prettyexport.json

RESULT

[
  {
    "Key": "project/",
    "CreateIndex": 1730,
    "ModifyIndex": 1730,
    "LockIndex": 0,
    "Flags": 0,
    "Value": "",
    "Session": ""
  },
  {
    "Key": "project/environment/some.other.property",
    "CreateIndex": 1736,
    "ModifyIndex": 1736,
    "LockIndex": 0,
    "Flags": 0,
    "Value": "some.other.value",
    "Session": ""
  },
  {
    "Key": "project/environment/some.property",
    "CreateIndex": 1733,
    "ModifyIndex": 1733,
    "LockIndex": 0,
    "Flags": 0,
    "Value": "some.value",
    "Session": ""
  }
]

Then, I try to restore those same backups with consul-cli kv bulkload:

(as JSON)

consul-cli kv bulkload --consul=XXX.YYY.com:8500 --json=export.json
Error: json: cannot unmarshal array into Go value of type map[string]interface {}

(as "pretty" JSON)

consul-cli kv bulkload --consul=XXX.YYY:8500 --json=prettyexport.json
Error: json: cannot unmarshal array into Go value of type map[string]interface {}

I would have expected to be able to use the result of the read as input for the bulkload but it doesn't seem to be the case. What am I doing wrong?

Thank you in advance, SoTMaR

agperson commented 7 years ago

See the example in the wiki for how to do this. Bulkload is expecting key=value only without any other data.

SoTMaR commented 7 years ago

Hi @agperson,

Thank you very much for your answer!

I tried that in the past without success:

cat prettyexport.json | jq from_entries
jq: error: Cannot use null as object key
jq: error: Cannot use null as object key
jq: error: Cannot use null as object key
null

Nevertheless, thanks to your comment and this jq issue, I found out that my CentOS was using jq 1.3 but the fix was added to later versions. I got jq 1.5 and everything is now working as expected.

Thanks again!

Cheers, SoTMaR