Skip to content

Commit 1072295

Browse files
authored
Add note and user groups commands (#185)
1 parent 7300646 commit 1072295

4 files changed

Lines changed: 80 additions & 2 deletions

File tree

alertaclient/commands/cmd_group.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import sys
2+
3+
import click
4+
5+
6+
@click.command('group', short_help='Create user group')
7+
@click.option('--name', help='Group name')
8+
@click.option('--text', help='Description of user group')
9+
@click.option('--delete', '-D', metavar='ID', help='Delete user group using ID')
10+
@click.pass_obj
11+
def cli(obj, name, text, delete):
12+
"""Create or delete a user group."""
13+
client = obj['client']
14+
if delete:
15+
client.delete_group(delete)
16+
else:
17+
try:
18+
group = client.create_group(name, text)
19+
except Exception as e:
20+
click.echo('ERROR: {}'.format(e))
21+
sys.exit(1)
22+
click.echo(group.id)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import json
3+
4+
import click
5+
from tabulate import tabulate
6+
7+
8+
@click.command('groups', short_help='List user groups')
9+
@click.pass_obj
10+
def cli(obj):
11+
"""List groups."""
12+
client = obj['client']
13+
14+
if obj['output'] == 'json':
15+
r = client.http.get('/groups')
16+
click.echo(json.dumps(r['groups'], sort_keys=True, indent=4, ensure_ascii=False))
17+
else:
18+
headers = {'id': 'ID', 'name': 'NAME', 'count': 'USERS', 'text': 'DESCRIPTION'}
19+
click.echo(tabulate([g.tabular() for g in client.get_users_groups()], headers=headers, tablefmt=obj['output']))

alertaclient/commands/cmd_note.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import click
2+
3+
from alertaclient.utils import build_query
4+
5+
6+
@click.command('note', short_help='Add note to alerts')
7+
@click.option('--ids', '-i', metavar='UUID', multiple=True, help='List of alert IDs (can use short 8-char id)')
8+
@click.option('--query', '-q', 'query', metavar='QUERY', help='severity:"warning" AND resource:web')
9+
@click.option('--filter', '-f', 'filters', metavar='FILTER', multiple=True, help='KEY=VALUE eg. serverity=warning resource=web')
10+
@click.option('--text', required=True, help='Note or message')
11+
@click.pass_obj
12+
def cli(obj, ids, query, filters, text):
13+
"""Add note to alerts."""
14+
client = obj['client']
15+
if ids:
16+
total = len(ids)
17+
else:
18+
if query:
19+
query = [('q', query)]
20+
else:
21+
query = build_query(filters)
22+
total, _, _ = client.get_count(query)
23+
ids = [a.id for a in client.get_alerts(query)]
24+
25+
with click.progressbar(ids, label='Add note to {} alerts'.format(total)) as bar:
26+
for id in bar:
27+
client.add_note(id, note=text)

alertaclient/models/group.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
from typing import Any, Dict, List, Optional, Tuple, Union
2+
from typing import Any, Dict
33
from uuid import uuid4
44

55
JSON = Dict[str, Any]
@@ -42,6 +42,16 @@ def __repr__(self) -> str:
4242
@classmethod
4343
def parse(cls, json: JSON) -> 'Group':
4444
return Group(
45+
id=json.get('id', None),
4546
name=json.get('name', None),
46-
text=json.get('text', None)
47+
text=json.get('text', None),
48+
count=json.get('count', 0)
4749
)
50+
51+
def tabular(self):
52+
return {
53+
'id': self.id,
54+
'name': self.name,
55+
'text': self.text,
56+
'count': self.count
57+
}

0 commit comments

Comments
 (0)