From d379a45e11408c690a11f3ee0a601c0cef20da5e Mon Sep 17 00:00:00 2001 From: Ryan Koval Date: Thu, 26 Jan 2017 11:50:36 -0600 Subject: [PATCH] added cloudwatch log group lookup --- aws_workflow/aws.py | 18 ++++++++++++++++++ aws_workflow/aws_test.py | 9 ++++++--- aws_workflow/base.py | 39 ++++++++++++++++++++++++++++++++++++++ aws_workflow/cli.py | 12 ++++++++++++ quicklook_server.py | 5 +++++ templates/eb.html.j2 | 2 +- templates/logGroup.html.j2 | 32 +++++++++++++++++++++++++++++++ 7 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 templates/logGroup.html.j2 diff --git a/aws_workflow/aws.py b/aws_workflow/aws.py index bc32efc..33b0d39 100644 --- a/aws_workflow/aws.py +++ b/aws_workflow/aws.py @@ -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 diff --git a/aws_workflow/aws_test.py b/aws_workflow/aws_test.py index 4fa5811..02a437e 100644 --- a/aws_workflow/aws_test.py +++ b/aws_workflow/aws_test.py @@ -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 diff --git a/aws_workflow/base.py b/aws_workflow/base.py index f1ef043..4402dc2 100644 --- a/aws_workflow/base.py +++ b/aws_workflow/base.py @@ -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) + + diff --git a/aws_workflow/cli.py b/aws_workflow/cli.py index 7ad0dfb..6332ddd 100644 --- a/aws_workflow/cli.py +++ b/aws_workflow/cli.py @@ -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) @@ -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) diff --git a/quicklook_server.py b/quicklook_server.py index c807519..cbe34df 100644 --- a/quicklook_server.py +++ b/quicklook_server.py @@ -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')}), @@ -62,6 +66,7 @@ def make_app(): (r'/quicklook/redshift', RedshiftClusterQuicklookHandler), (r'/quicklook/lambda', LambdaQuicklookHandler), (r'/quicklook/eb', EbQuicklookHandler), + (r'/quicklook/logGroup', LogGroupQuicklookHandler), ]) diff --git a/templates/eb.html.j2 b/templates/eb.html.j2 index 293826f..d9c091b 100644 --- a/templates/eb.html.j2 +++ b/templates/eb.html.j2 @@ -40,7 +40,7 @@ Endpoint URL: - {{ eb.EndpountURL | default('N/A') }} + {{ eb.EndpointURL | default('N/A') }} Version Label: diff --git a/templates/logGroup.html.j2 b/templates/logGroup.html.j2 new file mode 100644 index 0000000..afc990b --- /dev/null +++ b/templates/logGroup.html.j2 @@ -0,0 +1,32 @@ + + + {{ title }} + + + +

{{ title }}

+ + + + + + + + + + + + + + + + + + + + + +
ARN:{{ logGroup.arn }}
Creation Time{{ logGroup.creationTime }}
Metric Filter Count{{ logGroup.metricFilterCount }}
Retention in Days{{ logGroup.retentionInDays }}
Stored Bytes{{ logGroup.storedBytes }}
+ + +