Consul CRUD operation commands for DELETE and GET ALL?

I have consul entries in my kubernetes cluster. I need to perform CRUD operations. These are the commands I know, but I need to Get all and Delete and this has to be done with HTTP requests and not with consul cli.

GET curl -k -X GET <consul-url>/<key>?token=<decoded-consul-read-token>
GET ALL
CREATE/PUT curl -k --request PUT --data '<data>' <consul-url>/<key>?token=<decoded-consul-write-token>
DELETE

Anyone can help me out here to know the GET ALL and DELETE commands?

Hi @iamdempa

Welcome to the forums, and thanks for your question! :slight_smile:

Quick Question - Can you provide some use cases for each these additional HTTP verbs?

As for what is currently supported, here is a link to our supported HTTP Methods in our API Doc. The relevant section is this;

Consul’s API aims to be RESTful, although there are some exceptions. The API responds to the standard HTTP verbs GET, PUT, and DELETE.

I hope this helps, and welcome to the forums!
Best,
Jono

Hi @jsosulska thanks for the reply.

I need to provide customers with consul access via bastion. So they can operate freely on their own namespaces in the cluster. They don’t have the admin access but they can do basic CRUD operations.

Getting a one single key-value is possible and it is working. But I want them to list all the key-value pair in the kv store. So I need something to read all consul kv value pairs in his namespace context.

I saw this ?keys query parameter. But there are no adequate examples given in the documentation to us that. You can find it here

My normal GET command would look like this:

curl -k GET <consul-url>/v1/kv/test-namespace/test-value?token=<consul-read-token>

But this command only outputs one value. I need to get all the kv values with the single command as above. Can you help me?

Hi @iamdempa,

Hmm, this is the first time I’ve heard of giving access to customers via a bastion. Learn something new every day :smiley:

Since I can’t tell if you’re using OpenSource or our Enterprise tool, I’ll provide 2 routes.

Within Open Source Consul, we provide the recurse parameter on kv GETs which allows for larger response sets. I highly recommend using this with agent caching, so you are not performing recursive queries multiple times. The benefits of caching are outlined in the documentation, but I also recommend reading about Consul’s consistency model to see if your application can handle stale reads.

For other users who may be using Consul Enterprise, we also provide first class support for namespaces with ACL’s. The main advantage of Enterprise namespace support is that you can generate ACL tokens for your users, and let your users managed that Consul kv namespace themselves. The namespace is then a parameter that can be passed in the query.

I hope these responses help, and please feel free to add any additional detail!

Best,
Jono

Hi @jsosulska, thank you for this suggestion. But what I really want is to get all the kv store values using curl command (means your https api). In the normal circumstances I can only get one value at a time. I have to get all the values at once using the curl https command.

Hi @iamdempa.

Hmm, maybe I’m not understanding your use case well. Can you please try the following steps locally to verify functionality?

  1. In a terminal window, run consul agent -dev & to start the Consul process and background it.
  2. In a new terminal window, run the following commands to populate kv data:
consul kv put foo/bar 1
consul kv put foo/baz/bar 2
consul kv put abc/def 3
  1. Check the kv with curl -k 'localhost:8500/v1/kv/?recurse'

Quick note before we change your command - We document our HTTP header authentication to help with formatting.

To get all the keys and values for everything under test-namespace/, with the Authorization header, your new command would be curl -H 'X-Consul-Token: <consul token>' '<consul-url>/v1/kv/test-namespace/?recurse'.

Please let me know if this helps!
Best,
Jono

And to delete you can use the DELETE HTTP method:

curl \
    --request DELETE \
    http://127.0.0.1:8500/v1/kv/my-key

See https://www.consul.io/api/kv.html#delete-key

1 Like