Hi folks,
I have a question. Is it possible to edit KV keyspace or move it to under another keyspace.
Example 1:
I have a following:
_/kv/qa/foo
/kv/qa/bar_
I'd like to rename qa keyspace into the dev keyspace:
_/kv/dev/foo
/kv/dev/bar_
Example 2:
I have a following:
_/kv/qa/foo
/kv/qa/bar_
I'd like to move a qa keyspace under a dev keyspace:
_/kv/dev/qa/foo
/kv/dev/qa/bar_
Thanks!
Hey @rudziankou, you can migrate an entire prefix using the import/export commands.
Here's an example using jq:
Given a data like this:
$ consul kv get -recurse foo/
foo/bar/bat:foo
foo/bar/baz:foo
We move foo/ to new/:
$ consul kv export foo/ | jq -r '[.[] | .key = (.key|sub("^foo/"; "new/"))]' | consul kv import -
Imported: new/bar/bat
Imported: new/bar/baz
To break this down a bit, we are:
foo/jq and doing a string substitution on the key. This is a simplistic example assuming a specific key structure, but you get the idea and jq could do many other things here as necessary.import command.Here's the result:
$ consul kv get -recurse new/
new/bar/bat:foo
new/bar/baz:foo
Then delete the old kv once you've confirmed the import:
$ consul kv delete -recurse foo/
Success! Deleted keys with prefix: foo/
This general pattern can be applied with other JSON manipulation tools, too. Always recommend experimenting with this stuff in test environments. Hope that helps!
Thanks @pearkes !
Most helpful comment
Hey @rudziankou, you can migrate an entire prefix using the import/export commands.
Here's an example using jq:
Given a data like this:
We move
foo/tonew/:To break this down a bit, we are:
foo/jqand doing a string substitution on thekey. This is a simplistic example assuming a specific key structure, but you get the idea and jq could do many other things here as necessary.importcommand.Here's the result:
Then delete the old kv once you've confirmed the import:
This general pattern can be applied with other JSON manipulation tools, too. Always recommend experimenting with this stuff in test environments. Hope that helps!