Merge pull request #2533 from ianyon/lambda-get-by-arn

Feature - Lambda get by arn and update/get function environment
This commit is contained in:
Steve Pulec 2019-11-04 22:53:20 -06:00 committed by GitHub
commit 4a4ca2cc4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 8 deletions

View File

@ -304,6 +304,8 @@ class LambdaFunction(BaseModel):
self.timeout = value self.timeout = value
elif key == "VpcConfig": elif key == "VpcConfig":
self.vpc_config = value self.vpc_config = value
elif key == "Environment":
self.environment_vars = value["Variables"]
return self.get_configuration() return self.get_configuration()
@ -634,7 +636,7 @@ class LambdaStorage(object):
def _get_alias(self, name, alias): def _get_alias(self, name, alias):
return self._functions[name]["alias"].get(alias, None) return self._functions[name]["alias"].get(alias, None)
def get_function(self, name, qualifier=None): def get_function_by_name(self, name, qualifier=None):
if name not in self._functions: if name not in self._functions:
return None return None
@ -657,8 +659,8 @@ class LambdaStorage(object):
def get_arn(self, arn): def get_arn(self, arn):
return self._arns.get(arn, None) return self._arns.get(arn, None)
def get_function_by_name_or_arn(self, input): def get_function_by_name_or_arn(self, input, qualifier=None):
return self.get_function(input) or self.get_arn(input) return self.get_function_by_name(input, qualifier) or self.get_arn(input)
def put_function(self, fn): def put_function(self, fn):
""" """
@ -719,7 +721,7 @@ class LambdaStorage(object):
return True return True
else: else:
fn = self.get_function(name, qualifier) fn = self.get_function_by_name(name, qualifier)
if fn: if fn:
self._functions[name]["versions"].remove(fn) self._functions[name]["versions"].remove(fn)
@ -822,8 +824,10 @@ class LambdaBackend(BaseBackend):
def publish_function(self, function_name): def publish_function(self, function_name):
return self._lambdas.publish_function(function_name) return self._lambdas.publish_function(function_name)
def get_function(self, function_name, qualifier=None): def get_function(self, function_name_or_arn, qualifier=None):
return self._lambdas.get_function(function_name, qualifier) return self._lambdas.get_function_by_name_or_arn(
function_name_or_arn, qualifier
)
def list_versions_by_function(self, function_name): def list_versions_by_function(self, function_name):
return self._lambdas.list_versions_by_function(function_name) return self._lambdas.list_versions_by_function(function_name)
@ -928,7 +932,7 @@ class LambdaBackend(BaseBackend):
} }
] ]
} }
func = self._lambdas.get_function(function_name, qualifier) func = self._lambdas.get_function_by_name_or_arn(function_name, qualifier)
func.invoke(json.dumps(event), {}, {}) func.invoke(json.dumps(event), {}, {})
def send_dynamodb_items(self, function_arn, items, source): def send_dynamodb_items(self, function_arn, items, source):

View File

@ -286,7 +286,7 @@ class LambdaResponse(BaseResponse):
return 404, {}, "{}" return 404, {}, "{}"
def _get_function(self, request, full_url, headers): def _get_function(self, request, full_url, headers):
function_name = self.path.rsplit("/", 1)[-1] function_name = unquote(self.path.rsplit("/", 1)[-1])
qualifier = self._get_param("Qualifier", None) qualifier = self._get_param("Qualifier", None)
fn = self.lambda_backend.get_function(function_name, qualifier) fn = self.lambda_backend.get_function(function_name, qualifier)

View File

@ -388,6 +388,7 @@ def test_get_function():
Timeout=3, Timeout=3,
MemorySize=128, MemorySize=128,
Publish=True, Publish=True,
Environment={"Variables": {"test_variable": "test_value"}},
) )
result = conn.get_function(FunctionName="testFunction") result = conn.get_function(FunctionName="testFunction")
@ -416,6 +417,11 @@ def test_get_function():
result["Configuration"]["Timeout"].should.equal(3) result["Configuration"]["Timeout"].should.equal(3)
result["Configuration"]["Version"].should.equal("$LATEST") result["Configuration"]["Version"].should.equal("$LATEST")
result["Configuration"].should.contain("VpcConfig") result["Configuration"].should.contain("VpcConfig")
result["Configuration"].should.contain("Environment")
result["Configuration"]["Environment"].should.contain("Variables")
result["Configuration"]["Environment"]["Variables"].should.equal(
{"test_variable": "test_value"}
)
# Test get function with # Test get function with
result = conn.get_function(FunctionName="testFunction", Qualifier="$LATEST") result = conn.get_function(FunctionName="testFunction", Qualifier="$LATEST")
@ -429,6 +435,33 @@ def test_get_function():
conn.get_function(FunctionName="junk", Qualifier="$LATEST") conn.get_function(FunctionName="junk", Qualifier="$LATEST")
@mock_lambda
@mock_s3
def test_get_function_by_arn():
bucket_name = "test-bucket"
s3_conn = boto3.client("s3", "us-east-1")
s3_conn.create_bucket(Bucket=bucket_name)
zip_content = get_test_zip_file2()
s3_conn.put_object(Bucket=bucket_name, Key="test.zip", Body=zip_content)
conn = boto3.client("lambda", "us-east-1")
fnc = conn.create_function(
FunctionName="testFunction",
Runtime="python2.7",
Role="test-iam-role",
Handler="lambda_function.lambda_handler",
Code={"S3Bucket": bucket_name, "S3Key": "test.zip"},
Description="test lambda function",
Timeout=3,
MemorySize=128,
Publish=True,
)
result = conn.get_function(FunctionName=fnc["FunctionArn"])
result["Configuration"]["FunctionName"].should.equal("testFunction")
@mock_lambda @mock_lambda
@mock_s3 @mock_s3
def test_delete_function(): def test_delete_function():
@ -1322,6 +1355,7 @@ def test_update_configuration():
Timeout=3, Timeout=3,
MemorySize=128, MemorySize=128,
Publish=True, Publish=True,
Environment={"Variables": {"test_old_environment": "test_old_value"}},
) )
assert fxn["Description"] == "test lambda function" assert fxn["Description"] == "test lambda function"
@ -1336,6 +1370,7 @@ def test_update_configuration():
Handler="lambda_function.new_lambda_handler", Handler="lambda_function.new_lambda_handler",
Runtime="python3.6", Runtime="python3.6",
Timeout=7, Timeout=7,
Environment={"Variables": {"test_environment": "test_value"}},
) )
assert updated_config["ResponseMetadata"]["HTTPStatusCode"] == 200 assert updated_config["ResponseMetadata"]["HTTPStatusCode"] == 200
@ -1344,6 +1379,9 @@ def test_update_configuration():
assert updated_config["MemorySize"] == 128 assert updated_config["MemorySize"] == 128
assert updated_config["Runtime"] == "python3.6" assert updated_config["Runtime"] == "python3.6"
assert updated_config["Timeout"] == 7 assert updated_config["Timeout"] == 7
assert updated_config["Environment"]["Variables"] == {
"test_environment": "test_value"
}
@mock_lambda @mock_lambda