Skip to content

Commit c4ecfe0

Browse files
authored
Add support for JSON output format (#115)
1 parent 0cec04d commit c4ecfe0

9 files changed

Lines changed: 65 additions & 4 deletions

File tree

alertaclient/cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def get_command(self, ctx, name):
4040
@click.option('--profile', metavar='<PROFILE>', help='Configuration profile.')
4141
@click.option('--endpoint-url', metavar='<URL>', help='API endpoint URL.')
4242
@click.option('--output', 'output', metavar='<FORMAT>', help='Output format. eg. simple, grid, psql, presto, rst')
43+
@click.option('--json', 'output', flag_value='json', help='Output in JSON format. Shortcut for "--output json"')
4344
@click.option('--color/--no-color', help='Color-coded output based on severity.')
4445
@click.option('--debug', is_flag=True, help='Debug mode.')
4546
@click.pass_context

alertaclient/commands/cmd_blackouts.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

2-
2+
import sys
33
import click
4+
import json
45

56
from tabulate import tabulate
67

@@ -11,6 +12,12 @@
1112
def cli(obj, purge):
1213
"""List alert suppressions."""
1314
client = obj['client']
15+
16+
if obj['output'] == 'json':
17+
r = client.http.get('/blackouts')
18+
click.echo(json.dumps(r['blackouts'], sort_keys=True, indent=4, ensure_ascii=False))
19+
sys.exit(0)
20+
1421
timezone = obj['timezone']
1522
headers = {
1623
'id': 'ID', 'priority': 'P', 'environment': 'ENVIRONMENT', 'service': 'SERVICE', 'resource': 'RESOURCE',

alertaclient/commands/cmd_customers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

2-
2+
import sys
33
import click
4+
import json
45

56
from tabulate import tabulate
67

@@ -10,5 +11,11 @@
1011
def cli(obj):
1112
"""List customer lookups."""
1213
client = obj['client']
14+
15+
if obj['output'] == 'json':
16+
r = client.http.get('/customers')
17+
click.echo(json.dumps(r['customers'], sort_keys=True, indent=4, ensure_ascii=False))
18+
sys.exit(0)
19+
1320
headers = {'id': 'ID', 'customer': 'CUSTOMER', 'match': 'GROUP'}
1421
click.echo(tabulate([c.tabular() for c in client.get_customers()], headers=headers, tablefmt=obj['output']))

alertaclient/commands/cmd_heartbeats.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

2+
import sys
23
import click
4+
import json
35

46
from tabulate import tabulate
57
from alertaclient.models.heartbeat import MAX_LATENCY
@@ -13,6 +15,12 @@
1315
def cli(obj, alert, severity, purge):
1416
"""List heartbeats."""
1517
client = obj['client']
18+
19+
if obj['output'] == 'json':
20+
r = client.http.get('/heartbeats')
21+
click.echo(json.dumps(r['heartbeats'], sort_keys=True, indent=4, ensure_ascii=False))
22+
sys.exit(0)
23+
1624
timezone = obj['timezone']
1725
headers = {
1826
'id': 'ID', 'origin': 'ORIGIN', 'customer': 'CUSTOMER', 'tags': 'TAGS', 'createTime': 'CREATED',

alertaclient/commands/cmd_history.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11

2+
import sys
23
import click
4+
import json
5+
36
from tabulate import tabulate
47

58
from alertaclient.utils import build_query
@@ -12,6 +15,12 @@
1215
def cli(obj, ids, filters):
1316
"""Show status and severity changes for alerts."""
1417
client = obj['client']
18+
19+
if obj['output'] == 'json':
20+
r = client.http.get('/alerts/history')
21+
click.echo(json.dumps(r['history'], sort_keys=True, indent=4, ensure_ascii=False))
22+
sys.exit(0)
23+
1524
timezone = obj['timezone']
1625
if ids:
1726
query = [('id', x) for x in ids]

alertaclient/commands/cmd_keys.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

2+
import sys
23
import click
4+
import json
35

46
from tabulate import tabulate
57

@@ -9,6 +11,12 @@
911
def cli(obj):
1012
"""List API keys."""
1113
client = obj['client']
14+
15+
if obj['output'] == 'json':
16+
r = client.http.get('/keys')
17+
click.echo(json.dumps(r['keys'], sort_keys=True, indent=4, ensure_ascii=False))
18+
sys.exit(0)
19+
1220
timezone = obj['timezone']
1321
headers = {
1422
'id': 'ID', 'key': 'API KEY', 'user': 'USER', 'scopes': 'SCOPES', 'text': 'TEXT',

alertaclient/commands/cmd_perms.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

2-
2+
import sys
33
import click
4+
import json
45

56
from tabulate import tabulate
67

@@ -10,5 +11,11 @@
1011
def cli(obj):
1112
"""List permissions."""
1213
client = obj['client']
14+
15+
if obj['output'] == 'json':
16+
r = client.http.get('/perms')
17+
click.echo(json.dumps(r['permissions'], sort_keys=True, indent=4, ensure_ascii=False))
18+
sys.exit(0)
19+
1320
headers = {'id': 'ID', 'scopes': 'SCOPES', 'match': 'ROLE'}
1421
click.echo(tabulate([p.tabular() for p in client.get_perms()], headers=headers, tablefmt=obj['output']))

alertaclient/commands/cmd_query.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
import sys
3+
import json
24
import click
35
from tabulate import tabulate
46

@@ -34,6 +36,11 @@ def cli(obj, ids, filters, display, from_date=None):
3436
query.append(('from-date', from_date))
3537

3638
r = client.http.get('/alerts', query)
39+
40+
if obj['output'] == 'json':
41+
print(json.dumps(r['alerts']))
42+
sys.exit(0)
43+
3744
alerts = [Alert.parse(a) for a in r['alerts']]
3845
last_time = r['lastTime']
3946
auto_refresh = r['autoRefresh']

alertaclient/commands/cmd_users.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

2-
2+
import sys
33
import click
4+
import json
45

56
from tabulate import tabulate
67

@@ -10,6 +11,12 @@
1011
def cli(obj):
1112
"""List users."""
1213
client = obj['client']
14+
15+
if obj['output'] == 'json':
16+
r = client.http.get('/users')
17+
click.echo(json.dumps(r['users'], sort_keys=True, indent=4, ensure_ascii=False))
18+
sys.exit(0)
19+
1320
timezone = obj['timezone']
1421
headers = {'id': 'ID', 'name': 'USER', 'email': 'EMAIL', 'roles': 'ROLES', 'status': 'STATUS', 'text': 'TEXT',
1522
'createTime': 'CREATED', 'updateTime': 'LAST UPDATED', 'lastLogin': 'LAST LOGIN', 'email_verified': 'VERIFIED'}

0 commit comments

Comments
 (0)