Skip to content

Commit a381b69

Browse files
authored
Handle / in lambda handlers (#79)
* handle / in lambda handlers and replace them with . * handle names with `/` * try to fix lint? * add more tests and move function to its own file * fix lint * format again * format tests
1 parent 11bb3f2 commit a381b69

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

datadog_lambda/handler.py

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

99
import os
1010
from datadog_lambda.wrapper import datadog_lambda_wrapper
11+
from datadog_lambda.module_name import modify_module_name
1112

1213

1314
class HandlerError(Exception):
@@ -23,6 +24,8 @@ class HandlerError(Exception):
2324
if len(parts) != 2:
2425
raise HandlerError("Value %s for DD_LAMBDA_HANDLER has invalid format." % path)
2526

27+
2628
(mod_name, handler_name) = parts
27-
handler_module = import_module(mod_name)
29+
modified_mod_name = modify_module_name(mod_name)
30+
handler_module = import_module(modified_mod_name)
2831
handler = datadog_lambda_wrapper(getattr(handler_module, handler_name))

datadog_lambda/module_name.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def modify_module_name(module_name):
2+
"""Returns a valid modified module to get imported
3+
"""
4+
return ".".join(module_name.split("/"))

tests/test_module_name.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import sys
2+
import os
3+
import unittest
4+
5+
try:
6+
from unittest.mock import patch, call, MagicMock
7+
except ImportError:
8+
from mock import patch, call, MagicMock
9+
10+
from datadog_lambda.module_name import modify_module_name
11+
12+
13+
class TestModifyModuleName(unittest.TestCase):
14+
def test_modify_module_name(self):
15+
self.assertEqual(
16+
modify_module_name("lambda/handler/name.bar"), "lambda.handler.name.bar"
17+
)
18+
self.assertEqual(
19+
modify_module_name("lambda.handler/name.biz"), "lambda.handler.name.biz"
20+
)
21+
self.assertEqual(modify_module_name("foo.handler"), "foo.handler")

0 commit comments

Comments
 (0)