Consul: Edit Consul KV keyspace

Created on 9 Aug 2018  路  2Comments  路  Source: hashicorp/consul

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!

typquestion

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:

$ 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:

  1. Exporting everything at the path foo/
  2. Piping the JSON output to 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.
  3. Pipe the modified JSON to the 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!

All 2 comments

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:

  1. Exporting everything at the path foo/
  2. Piping the JSON output to 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.
  3. Pipe the modified JSON to the 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 !

Was this page helpful?
0 / 5 - 0 ratings