Consul namespace query

I have enterprise consul installed and it has multiple namespaces.
I want to use python module to query for specified namespace. Basically if I have namespaces: ns_1, ns_2 … etc. I want to get the service definition and their respective meta for ns_1, ns_2 individually.

The input parameter would be the namespace name (ns_1/ns_2).
Can someone plz help me on how to accomplish this.

–Thanks

Hi @rudrapbiswas you could accomplish this with two API calls. You would first use the list services /catalog API. This would return all the services in the specified namespace. Once you have the list of all services and their service_id, you can then use the get service definition API endpoint to get the service definition and respective metadata.

There are some community python tools worth checking out. However, be aware that the community tools might not be up-to-date. Hope this helps.

Thanks for the response.
My first hurdle is to filter out the namespace. How can I only filter the services for a particular namespace?
And I need to use Python’s consul module. Through carl, I am able to, but not thru Python.

–Thanks

I’m not familiar with the Python Consul module. But to answer your question. The API endpoint to list services accept a query parameter for namespace. I’m assuming you already know what namespaces you have available. Unless you are asking how to get all available namespaces?

Yes, you are right. I have the namespaces available to me. I just need to pass them as a parameter and get rest of the data.

–Thanks

Good, then in that case you can simply use the Consul API directly with a pythons http package library. I have used requests before, and it is a popular http package library that is user friendly. As for the Python Consul module, I would take a peek at its docs and see if the namespace parameter is available for the list services method. Otherwise, you need to craft the HTTP call yourself in python.

1 Like