Skip to content

Commit abf8e41

Browse files
ramisa2108Ramisa Alam
and
Ramisa Alam
authored
feat: add tenant-id to lambda context (#43)
Co-authored-by: Ramisa Alam <[email protected]>
2 parents 26a442f + bc5c6ca commit abf8e41

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

lib/aws_lambda_ric/lambda_context.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class LambdaContext
44
attr_reader :aws_request_id, :invoked_function_arn, :log_group_name,
55
:log_stream_name, :function_name, :memory_limit_in_mb, :function_version,
6-
:identity, :client_context, :deadline_ms
6+
:identity, :tenant_id, :client_context, :deadline_ms
77

88
def initialize(request)
99
@clock_diff = Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond) - Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
@@ -16,6 +16,7 @@ def initialize(request)
1616
@memory_limit_in_mb = ENV['AWS_LAMBDA_FUNCTION_MEMORY_SIZE']
1717
@function_version = ENV['AWS_LAMBDA_FUNCTION_VERSION']
1818
@identity = JSON.parse(request['Lambda-Runtime-Cognito-Identity']) unless request['Lambda-Runtime-Cognito-Identity'].to_s.empty?
19+
@tenant_id = request['Lambda-Runtime-Aws-Tenant-Id'] unless request['Lambda-Runtime-Aws-Tenant-Id'].to_s.empty?
1920
@client_context = JSON.parse(request['Lambda-Runtime-Client-Context']) unless request['Lambda-Runtime-Client-Context'].to_s.empty?
2021
end
2122

test/unit/lambda_server_test.rb

+58
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,62 @@ def test_post_invocation_error_with_too_large_xray_cause
5353

5454
assert_mock post_mock
5555
end
56+
57+
def mock_next_invocation_response()
58+
mock_response = Net::HTTPSuccess.new(1.0, '200', 'OK')
59+
mock_response['Lambda-Runtime-Aws-Request-Id'] = @request_id
60+
mock_response
61+
end
62+
63+
def mock_next_invocation_request(mock_response)
64+
get_mock = Minitest::Mock.new
65+
get_mock.expect(:read_timeout=, nil, [RapidClient::LONG_TIMEOUT_MS])
66+
get_mock.expect(:start, mock_response) do |&block|
67+
block.call(get_mock)
68+
end
69+
get_mock.expect(:get, mock_response, ['/2018-06-01/runtime/invocation/next', {'User-Agent' => @mock_user_agent}])
70+
get_mock
71+
end
72+
73+
def assert_next_invocation(get_mock, expected_tenant_id)
74+
Net::HTTP.stub(:new, get_mock, ['127.0.0.1', 9001]) do
75+
request_id, response = @under_test.next_invocation
76+
assert_equal @request_id, request_id
77+
assert_equal expected_tenant_id, response['Lambda-Runtime-Aws-Tenant-Id']
78+
end
79+
end
80+
81+
def test_next_invocation_without_tenant_id_header
82+
mock_response = mock_next_invocation_response()
83+
get_mock = mock_next_invocation_request(mock_response)
84+
assert_next_invocation(get_mock, nil)
85+
assert_mock get_mock
86+
end
87+
88+
def test_next_invocation_with_tenant_id_header
89+
mock_response = mock_next_invocation_response()
90+
mock_response['Lambda-Runtime-Aws-Tenant-Id'] = 'blue'
91+
92+
get_mock = mock_next_invocation_request(mock_response)
93+
assert_next_invocation(get_mock, 'blue')
94+
assert_mock get_mock
95+
end
96+
97+
def test_next_invocation_with_empty_tenant_id_header
98+
mock_response = mock_next_invocation_response()
99+
mock_response['Lambda-Runtime-Aws-Tenant-Id'] = ''
100+
101+
get_mock = mock_next_invocation_request(mock_response)
102+
assert_next_invocation(get_mock, '')
103+
assert_mock get_mock
104+
end
105+
106+
def test_next_invocation_with_null_tenant_id_header
107+
mock_response = mock_next_invocation_response()
108+
mock_response['Lambda-Runtime-Aws-Tenant-Id'] = nil
109+
110+
get_mock = mock_next_invocation_request(mock_response)
111+
assert_next_invocation(get_mock, nil)
112+
assert_mock get_mock
113+
end
56114
end

test/unit/resources/runtime_handlers/ctx.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ def get_context(event:,context:)
99
log_group_name: context.log_group_name,
1010
log_stream_name: context.log_stream_name,
1111
memory_limit_in_mb: context.memory_limit_in_mb,
12-
function_version: context.function_version
12+
function_version: context.function_version,
13+
tenant_id: context.tenant_id
1314
}
1415
end
1516

0 commit comments

Comments
 (0)