From 0a078c513026fb4b4dcfbbbe2201448690e0ff07 Mon Sep 17 00:00:00 2001 From: Ryan Koval Date: Tue, 27 Dec 2016 15:33:16 -0600 Subject: [PATCH] added lambda function lookup --- README.md | 1 + aws_workflow/aws.py | 17 ++++++++ aws_workflow/aws_test.py | 5 +++ aws_workflow/base.py | 25 ++++++++++++ aws_workflow/cli.py | 1 + quicklook_server.py | 5 +++ templates/lambda.html.j2 | 84 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 138 insertions(+) create mode 100644 templates/lambda.html.j2 diff --git a/README.md b/README.md index 5261dc5..aa31244 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ Below is a table describing the currently supported AWS resource lookups and wha | :---: | --- | --- | --- | --- | --- | | ![](icons/cfn_stack.png)CloudFormation | Open stack in AWS console | | | | Quicklook | | ![](icons/ec2_instance.png)
EC2 | Copies _private_ IP | Open instance in AWS console | Copies _public_ IP (if available) | | Quicklook | +| ![](icons/services/lambda.png)
Lambda | Open lambda in AWS console | | | | Quicklook | | ![](icons/db_instance.png)![](icons/db_cluster.png)
RDS | Copy endpoint URL | Open cluster or node in AWS Console | | | Quicklook | | ![](icons/services/redshift.png)
Redshift | Open cluster in AWS console | Copies first node's _private_ IP (if available) | Copies first node's _public_ IP (if available) | | Quicklook | | ![](icons/s3_bucket.png)
S3 | Open bucket in AWS Console | | | | Quicklook | diff --git a/aws_workflow/aws.py b/aws_workflow/aws.py index fd1a20c..5b4d63a 100644 --- a/aws_workflow/aws.py +++ b/aws_workflow/aws.py @@ -170,3 +170,20 @@ def get_redshift_clusters(): break return items + +def get_lambda_functions(): + client = boto3.client('lambda') + next_token = {} + items = [] + while True: + log.debug('calling list_functions') + response = client.list_functions(**next_token) + for item in response['Functions']: + item['facets'] = {} + item['facets']['name'] = item['FunctionName'] + items.append(item) + if 'NextMarker' in response: + next_token['NextToken'] = response['NextMarker'] + else: + break + return items diff --git a/aws_workflow/aws_test.py b/aws_workflow/aws_test.py index 8cf3afb..76425a2 100644 --- a/aws_workflow/aws_test.py +++ b/aws_workflow/aws_test.py @@ -35,3 +35,8 @@ def test_get_sqs_queues(): def test_get_redshift_clusters(): clusters = get_redshift_clusters() assert clusters + + +def test_get_lambda_functions(): + items = get_lambda_functions() + assert items diff --git a/aws_workflow/base.py b/aws_workflow/base.py index d6947aa..34832a3 100644 --- a/aws_workflow/base.py +++ b/aws_workflow/base.py @@ -331,3 +331,28 @@ def populate_menu_item(self, wf, cluster, title, uid, region_name, quicklookurl) altmod.setvar('action', 'copy-to-clipboard,post-notification') altmod.setvar('notification_title', 'Copied Public IP of Redshift Node') altmod.setvar('notification_text', '%s of %s' % (public_ip, title)) + + +class FunctionFinder(Finder): + item_identifier = 'lambda' + aws_list_function_name = 'get_lambda_functions' + + def create_title(self, item): + return item['FunctionName'] + + def filter_items(self, wf, items, terms): + return wf.filter(' '.join(terms), items, key=lambda item: unicode(item['FunctionName'])) + + def populate_menu_item(self, wf, function, title, uid, region_name, quicklookurl): + url = 'https://%s.console.aws.amazon.com/lambda/home?region=%s#/functions/%s?tab=code' % (region_name, region_name, title) + item = wf.add_item( + title, + subtitle='open in AWS console (runtime: %s)' % function.get('Runtime', 'N/A'), + arg=url, + valid=True, + uid=uid, + icon='icons/services/lambda.png', + type='default', + quicklookurl=quicklookurl + ) + item.setvar('action', 'open-url') diff --git a/aws_workflow/cli.py b/aws_workflow/cli.py index 2daa296..3346512 100644 --- a/aws_workflow/cli.py +++ b/aws_workflow/cli.py @@ -359,6 +359,7 @@ def search(quicklook_port, query, wf, profile, region): BucketFinder(), Ec2Finder(), RedshiftClusterFinder(), + FunctionFinder(), ] for finder in finders: finder.find(wf, profile, region, terms, facets, quicklook_baseurl) diff --git a/quicklook_server.py b/quicklook_server.py index aa5c087..b8738ba 100644 --- a/quicklook_server.py +++ b/quicklook_server.py @@ -43,6 +43,10 @@ class RedshiftClusterQuicklookHandler(BaseHandler): template = 'redshift.html.j2' +class LambdaQuicklookHandler(BaseHandler): + template = 'lambda.html.j2' + + def make_app(): return tornado.web.Application([ (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': os.path.join(os.getcwd(), 'static')}), @@ -52,6 +56,7 @@ def make_app(): (r'/quicklook/cfn', CfnQuicklookHandler), (r'/quicklook/sqs', SqsQuicklookHandler), (r'/quicklook/redshift', RedshiftClusterQuicklookHandler), + (r'/quicklook/lambda', LambdaQuicklookHandler), ]) diff --git a/templates/lambda.html.j2 b/templates/lambda.html.j2 new file mode 100644 index 0000000..734df5e --- /dev/null +++ b/templates/lambda.html.j2 @@ -0,0 +1,84 @@ + + + {{ title }} + + + +

{{ title }}

+ + + + + + + + + + + + + + + + + + + + + + {% if 'Environment' in lambda and 'Error' in lambda.Environment %} + + + + + {% endif %} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {% if 'DeadLetterConfig' in lambda %} + + + + + {% endif %} + {% if 'Environment' in lambda and 'Variables' in lambda.Environment %} + + + + + {% endif %} +
Name:{{ lambda.FunctionName}}
Description:{{ lambda.Description }}
ARN:{{ lambda.FunctionArn }}
Version:{{ lambda.Version}}
Last Modified:{{ lambda.LastModified }}
Error Status:{{ lambda.Environment.Error.ErrorCode }} - {{ lambda.Environment.Error.Message }}
Runtime:{{ lambda.Runtime }}
Role:{{ lambda.Role }}
Handler:{{ lambda.Handler }}
Timeout:{{ lambda.Timeout }} seconds
Memory Size:{{ lambda.MemorySize }}MB
Code SHA256:{{ lambda.CodeSha256 }}
MKS Key ARN:{{ lambda.KMSKeyArn | default('N/A') }}
Dead Letter Target ARN:{{ lambda.DeadLetterConfig.TargetArn | default('N/A') }}
Environment: +
+ {% for key, value in lambda.Environment.Variables.items() %} +
{{ key }}
+
{{ value }}
+ {% endfor %} +
+
+ +