Skip to content

Commit e567a9c

Browse files
authored
Merge pull request gmr#108 from mrwacky42/feature/add-service-list
Add service list to cli
2 parents d5cb437 + c4fdf79 commit e567a9c

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ of being accepted.*
1212

1313
Consulate test suite has a couple of requirements:
1414

15-
* Dependencies from [requirements/testing.txt](requirements/testing.txt) are installed
15+
* Dependencies from [requires/testing.txt](requires/testing.txt) are installed
1616
* Local Docker and [docker-compose](https://docs.docker.com/compose/)
1717

1818
## Installing Dependencies
@@ -28,8 +28,8 @@ source env/bin/activate
2828
To install the dependencies needed to run Consulate tests, use
2929

3030
```bash
31-
pip install -r requirements/testing.txt
32-
```
31+
pip install -r requires/testing.txt
32+
```
3333

3434
## Starting the test dependency
3535

@@ -38,7 +38,7 @@ Prior to running tests, ensure that Consul is running via Docker using:
3838
```bash
3939
./bootstrap
4040
```
41-
41+
4242
This script uses [docker-compose](https://docs.docker.com/compose/) to launch a Consul server container that is
4343
pre-configured for the tests. In addition, it configures `build/test-environment` that is loaded
4444
by the tests with configuration information for connecting to Consul.
@@ -56,5 +56,5 @@ with ``pep8`` style prior to issuing your pull request. In addition, run
5656
``flake8`` to look for any style errors prior to submitting your PR.
5757

5858
Both are included when the test requirements are installed. If you are fixing
59-
formatting for existing code, please separate code-reformatting commits from
59+
formatting for existing code, please separate code-reformatting commits from
6060
functionality changes.

README.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ setting, and deleting keys from the KV database.
5353
--token TOKEN ACL token
5454
5555
Commands:
56-
{register,deregister,kv,run_once}
56+
{register,deregister,kv,run_once,services}
5757
register Register a service for this node
5858
deregister Deregister a service for this node
5959
kv Key/Value Database Utilities
6060
run_once Lock command
61+
services List services for this node
6162
6263
If the CONSUL_RPC_ADDR environment variable is set, it will be parsed and used
6364
for default values when connecting.
@@ -122,6 +123,17 @@ Locking Operations Help:
122123
-h, --help show this help message and exit
123124
-i, --interval hold the lock for INTERVAL seconds
124125
126+
Service listing Help:
127+
128+
.. code:: bash
129+
130+
usage: consulate services [-h] [-i INDENT]
131+
132+
optional arguments:
133+
-h, --help show this help message and exit
134+
-i INDENT, --indent INDENT
135+
The indent level for output
136+
125137
API Usage Examples
126138
------------------
127139
The following examples highlight the usage of Consulate and does not document

consulate/cli.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ def add_deregister_args(parser):
210210
registerp.add_argument('service_id', help='The service registration id')
211211

212212

213+
def add_services_args(parser):
214+
"""Add the services command and arguments.
215+
216+
:param argparse.Subparser parser: parser
217+
218+
"""
219+
# Service registration
220+
registerp = parser.add_parser('services',
221+
help='List services for this node')
222+
223+
registerp.add_argument('-i', '--indent', type=int, default=None, help='The indent level for output')
224+
213225
def parse_cli_args():
214226
"""Create the argument parser and add the arguments"""
215227
parser = argparse.ArgumentParser(description='CLI utilities for Consul',
@@ -239,6 +251,7 @@ def parse_cli_args():
239251
add_register_args(sparser)
240252
add_deregister_args(sparser)
241253
add_run_once_args(sparser)
254+
add_services_args(sparser)
242255
return parser.parse_args()
243256

244257

@@ -570,6 +583,21 @@ def run_once(consul, args):
570583
on_error(error_msg, error_code)
571584

572585

586+
def services(consul, args):
587+
"""Dump the list of services registered with Consul
588+
589+
:param consulate.api.Consul consul: The Consul instance
590+
:param argparser.namespace args: The cli args
591+
592+
"""
593+
594+
svcs = consul.agent.services()
595+
print(json.dumps(svcs,
596+
sort_keys=True,
597+
indent=args.indent,
598+
separators=(',', ': ')) + '\n')
599+
600+
573601
def main():
574602
"""Entrypoint for the consulate cli application"""
575603
args = parse_cli_args()
@@ -600,5 +628,7 @@ def main():
600628
register(consul, args)
601629
elif args.command == 'deregister':
602630
deregister(consul, args)
631+
elif args.command == 'services':
632+
services(consul, args)
603633
elif args.command == 'run_once':
604634
run_once(consul, args)

0 commit comments

Comments
 (0)