Skip to content

Commit

Permalink
added cloudwatch log group lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Koval committed Jan 26, 2017
1 parent 0f41301 commit d379a45
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 4 deletions.
18 changes: 18 additions & 0 deletions aws_workflow/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,21 @@ def get_beanstalk_environments():
item['facets']['name'] = item['EnvironmentName']
items.append(item)
return items


def get_cloudwatch_log_groups():
client = boto3.client('logs')
next_token = {}
items = []
while True:
log.debug('calling cloudwatch_streams')
response = client.describe_log_groups(**next_token)
for item in response['logGroups']:
item['facets'] = {}
item['facets']['name'] = item['logGroupName']
items.append(item)
if 'nextToken' in response:
next_token['nextToken'] = response['nextToken']
else:
break
return items
9 changes: 6 additions & 3 deletions aws_workflow/aws_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def test_get_lambda_functions():

def test_get_beanstalk_environments():
items = get_beanstalk_environments()
printer = pprint.PrettyPrinter()
printer.pprint(items)
assert not items
assert items


def test_get_cloudwatch_log_groups():
items = get_cloudwatch_log_groups()
assert items
39 changes: 39 additions & 0 deletions aws_workflow/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,42 @@ def populate_menu_item(self, wf, env, title, uid, region_name, quicklookurl):
cmdmod.setvar('action', 'copy-to-clipboard,post-notification')
cmdmod.setvar('notification_title', 'Copied Load Balancer URL')
cmdmod.setvar('notification_text', load_balancer_url)


class LogGroupFinder(Finder):
item_identifier = 'logGroup'
aws_list_function_name = 'get_cloudwatch_log_groups'

def create_title(self, item):
return item['logGroupName']

def filter_items(self, wf, items, terms):
return wf.filter(' '.join(terms), items, match_on=MATCH_ALL ^ MATCH_ALLCHARS, key=lambda item: unicode(item['logGroupName']))

def populate_menu_item(self, wf, group, title, uid, region_name, quicklookurl):
group_name = self.create_title(group)
url = 'https://%s.console.aws.amazon.com/cloudwatch/home?region=%s#logEventViewer:group=%s;start=P1D' % (region_name, region_name, group_name)
item = wf.add_item(
title,
subtitle='open in AWS console',
arg=url,
valid=True,
uid=uid,
icon='icons/services/cloudwatch.png',
type='default',
quicklookurl=quicklookurl
)
item.setvar('action', 'open-url')

awslogs_command = 'awslogs get %s -w --aws_region %s' % (group_name, region_name)
cmdmod = item.add_modifier(
'cmd',
subtitle='copy `awslogs` tail command to clipboard',
arg=awslogs_command,
valid=True,
)
cmdmod.setvar('action', 'copy-to-clipboard,post-notification')
cmdmod.setvar('notification_title', 'Copied to Clipboard')
cmdmod.setvar('notification_text', awslogs_command)


12 changes: 12 additions & 0 deletions aws_workflow/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ def root(ctx, query):
def wf_commands():
'''run a workflow command'''

@root.group('@')
def resource_commands():
'''search within a particular resource'''

@resource_commands.command('ec2')
@click.argument('query', required=False)
@pass_wf
@pass_complete
def hooray(query, wf, complete):
'''set the active profile (currently active: )'''
wf.send_feedback()

@wf_commands.command('profile')
@click.argument('query', required=False)
Expand Down Expand Up @@ -363,6 +374,7 @@ def search(quicklook_port, query, wf, profile, region):
RedshiftClusterFinder(),
FunctionFinder(),
EnvironmentFinder(),
LogGroupFinder(),
]
for finder in finders:
finder.find(wf, profile, region, terms, facets, quicklook_baseurl)
Expand Down
5 changes: 5 additions & 0 deletions quicklook_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class EbQuicklookHandler(BaseHandler):
template = 'eb.html.j2'


class LogGroupQuicklookHandler(BaseHandler):
template = 'logGroup.html.j2'


def make_app():
return tornado.web.Application([
(r'/static/(.*)', tornado.web.StaticFileHandler, {'path': os.path.join(os.getcwd(), 'static')}),
Expand All @@ -62,6 +66,7 @@ def make_app():
(r'/quicklook/redshift', RedshiftClusterQuicklookHandler),
(r'/quicklook/lambda', LambdaQuicklookHandler),
(r'/quicklook/eb', EbQuicklookHandler),
(r'/quicklook/logGroup', LogGroupQuicklookHandler),
])


Expand Down
2 changes: 1 addition & 1 deletion templates/eb.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</tr>
<tr>
<td>Endpoint URL:</td>
<td>{{ eb.EndpountURL | default('N/A') }}</td>
<td>{{ eb.EndpointURL | default('N/A') }}</td>
</tr>
<tr>
<td>Version Label:</td>
Expand Down
32 changes: 32 additions & 0 deletions templates/logGroup.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<html>
<head>
<title>{{ title }}</title>
<link rel="stylesheet" type="text/css" href="/static/css/main.css">
</head>
<body>
<h1>{{ title }}</h1>
<table>
<tr>
<td>ARN:</td>
<td>{{ logGroup.arn }}</td>
</tr>
<tr>
<td>Creation Time</td>
<td>{{ logGroup.creationTime }}</td>
</tr>
<tr>
<td>Metric Filter Count</td>
<td>{{ logGroup.metricFilterCount }}</td>
</tr>
<tr>
<td>Retention in Days</td>
<td>{{ logGroup.retentionInDays }}</td>
</tr>
<tr>
<td>Stored Bytes</td>
<td>{{ logGroup.storedBytes }}</td>
</tr>
</table>
</body>
</html>

0 comments on commit d379a45

Please sign in to comment.