From 58abe3eb9b969539a83427cf2503a861a2f1fd48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilsen=20Hern=C3=A1ndez?= Date: Sun, 11 Sep 2022 13:51:12 -0400 Subject: [PATCH 1/6] Add ability to tag functions Closes #78 --- docs/functions/customization.md | 21 ++++++++++++++++++++- src/LambdaFunction.php | 13 ++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/docs/functions/customization.md b/docs/functions/customization.md index 81747a8..69e647a 100644 --- a/docs/functions/customization.md +++ b/docs/functions/customization.md @@ -50,7 +50,7 @@ To change the allocated memory of your function, return the number in megabytes. ```php class ExampleFunction extends LambdaFunction { - public function memory() // [tl! focus:4] + public function memory() // [tl! focus:4] { // 2GB of memory return 2048; @@ -227,6 +227,25 @@ You likely won't need to change this, but if you do, *you must include the envir } ``` +## Tags + +A key-value array of strings of tags to be applied to your function, this can be used to organize your functions by owner, project or departement. + +You should note that there are some [restrictions](https://docs.aws.amazon.com/lambda/latest/dg/configuration-tags.html#configuration-tags-restrictions) that apply when adding tags. + +```php +class ExampleFunction extends LambdaFunction +{ + public function tags() // [tl! focus:start] + { + return [ + 'Project' => 'Super Secret Project', + 'Department' => 'Logistics', + ]; + } // [tl! focus:end] +} +``` + ## Description The description is totally up to you, you'll likely not need to change it at all. We have provided a descriptive default. Sidecar doesn't do anything with it. diff --git a/src/LambdaFunction.php b/src/LambdaFunction.php index b718712..129722b 100644 --- a/src/LambdaFunction.php +++ b/src/LambdaFunction.php @@ -325,6 +325,16 @@ public function memory() return config('sidecar.memory'); } + /** + * A list of tags to apply to the function. + * + * @return array + */ + public function tags() + { + return []; + } + /** * The function execution time, in seconds, at which Lambda should terminate the function. * Because the execution time has cost implications, we recommend you set this @@ -433,7 +443,8 @@ public function toDeploymentArray() 'Layers' => $this->layers(), 'Publish' => true, 'PackageType' => $this->packageType(), - 'Architectures' => [$this->architecture()] + 'Architectures' => [$this->architecture()], + 'Tags' => $this->tags(), ]; // For container image packages, we need to remove the Runtime From 797b94f605eed60ed4a77dee2665f91a5776c7be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilsen=20Hern=C3=A1ndez?= Date: Sun, 11 Sep 2022 14:01:51 -0400 Subject: [PATCH 2/6] Add function tags tests --- tests/Unit/DeploymentTest.php | 40 ++++++++++++++++++- tests/Unit/Support/DeploymentTestFunction.php | 1 + .../DeploymentTestFunctionWithTags.php | 16 ++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/Support/DeploymentTestFunctionWithTags.php diff --git a/tests/Unit/DeploymentTest.php b/tests/Unit/DeploymentTest.php index ca9b087..61a0727 100644 --- a/tests/Unit/DeploymentTest.php +++ b/tests/Unit/DeploymentTest.php @@ -14,6 +14,7 @@ use Hammerstone\Sidecar\Events\BeforeFunctionsDeployed; use Hammerstone\Sidecar\Exceptions\NoFunctionsRegisteredException; use Hammerstone\Sidecar\Tests\Unit\Support\DeploymentTestFunction; +use Hammerstone\Sidecar\Tests\Unit\Support\DeploymentTestFunctionWithTags; use Hammerstone\Sidecar\Tests\Unit\Support\DeploymentTestFunctionWithVariables; use Illuminate\Support\Facades\Event; use Mockery; @@ -63,7 +64,8 @@ public function mockCreatingFunction() 'MemorySize' => 'test-MemorySize', 'Layers' => 'test-Layers', 'Publish' => 'test-Publish', - 'Architectures' => ['x86_64'] + 'Architectures' => ['x86_64'], + 'Tags' => [], ]); $this->lambda->shouldNotReceive('updateFunctionConfiguration'); @@ -269,6 +271,42 @@ public function it_doesnt_change_variables_that_havent_changed() $this->assertEvents($deployed = true, $activated = true); } + /** @test */ + public function it_sets_function_tags() + { + $this->lambda->shouldReceive('functionExists')->andReturn(true); + $this->lambda->shouldReceive('getVersions')->andReturn([]); + $this->lambda->shouldReceive('updateExistingFunction')->once()->withArgs(function ($function) { + return $function instanceof DeploymentTestFunctionWithTags; + }); + + $this->lambda->shouldReceive('getFunctionConfiguration')->andReturn([ + 'Tags' => [ + 'Project' => 'Super Secret Project' + ], + ]); + + $this->lambda->shouldReceive('updateFunctionConfiguration') + ->with([ + 'FunctionName' => 'test-FunctionName', + 'Tags' => [ + 'Project' => 'Super Secret Project' + ], + ]); + + $this->lambda->shouldReceive('publishVersion') + ->once() + ->with([ + 'FunctionName' => 'test-FunctionName', + ]); + + $this->mockActivating(); + + DeploymentTestFunctionWithTags::deploy($activate = true); + + $this->assertEvents($deployed = true, $activated = true); + } + /** @test */ public function it_throws_an_exception_if_there_are_no_functions() { diff --git a/tests/Unit/Support/DeploymentTestFunction.php b/tests/Unit/Support/DeploymentTestFunction.php index 114856c..e069602 100644 --- a/tests/Unit/Support/DeploymentTestFunction.php +++ b/tests/Unit/Support/DeploymentTestFunction.php @@ -52,6 +52,7 @@ public function toDeploymentArray() 'Layers' => 'test-Layers', 'Publish' => 'test-Publish', 'Architectures' => ['x86_64'], + 'Tags' => $this->tags(), ]; } } diff --git a/tests/Unit/Support/DeploymentTestFunctionWithTags.php b/tests/Unit/Support/DeploymentTestFunctionWithTags.php new file mode 100644 index 0000000..fee7adf --- /dev/null +++ b/tests/Unit/Support/DeploymentTestFunctionWithTags.php @@ -0,0 +1,16 @@ + + */ + +namespace Hammerstone\Sidecar\Tests\Unit\Support; + +class DeploymentTestFunctionWithTags extends DeploymentTestFunction +{ + public function tags() + { + return [ + 'Project' => 'Super Secret Project' + ]; + } +} From f8edd4471c6a05fefcf6920b3cf622f891f9cc3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilsen=20Hern=C3=A1ndez?= Date: Sun, 11 Sep 2022 14:15:07 -0400 Subject: [PATCH 3/6] Fix deployment tests --- tests/Unit/DeploymentTest.php | 43 ++++++++++++++++------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/tests/Unit/DeploymentTest.php b/tests/Unit/DeploymentTest.php index 61a0727..3ad1282 100644 --- a/tests/Unit/DeploymentTest.php +++ b/tests/Unit/DeploymentTest.php @@ -274,34 +274,31 @@ public function it_doesnt_change_variables_that_havent_changed() /** @test */ public function it_sets_function_tags() { - $this->lambda->shouldReceive('functionExists')->andReturn(true); + $this->lambda->shouldReceive('functionExists')->andReturn(false); $this->lambda->shouldReceive('getVersions')->andReturn([]); - $this->lambda->shouldReceive('updateExistingFunction')->once()->withArgs(function ($function) { - return $function instanceof DeploymentTestFunctionWithTags; - }); - - $this->lambda->shouldReceive('getFunctionConfiguration')->andReturn([ + $this->lambda->shouldReceive('createFunction')->once()->with([ + 'FunctionName' => 'test-FunctionName', + 'Runtime' => 'test-Runtime', + 'Role' => 'test-Role', + 'Handler' => 'test-Handler', + 'Code' => [ + 'S3Bucket' => 'test-bucket', + 'S3Key' => 'test-key', + ], + 'Description' => 'test-Description', + 'Timeout' => 'test-Timeout', + 'MemorySize' => 'test-MemorySize', + 'EphemeralStorage' => [ + 'Size' => 'test-EphemeralStorage' + ], + 'Layers' => 'test-Layers', + 'Publish' => 'test-Publish', + 'Architectures' => ['x86_64'], 'Tags' => [ - 'Project' => 'Super Secret Project' + 'Project' => 'Super Secret Project', ], ]); - $this->lambda->shouldReceive('updateFunctionConfiguration') - ->with([ - 'FunctionName' => 'test-FunctionName', - 'Tags' => [ - 'Project' => 'Super Secret Project' - ], - ]); - - $this->lambda->shouldReceive('publishVersion') - ->once() - ->with([ - 'FunctionName' => 'test-FunctionName', - ]); - - $this->mockActivating(); - DeploymentTestFunctionWithTags::deploy($activate = true); $this->assertEvents($deployed = true, $activated = true); From afd365c2c8e690f5c50ba018fe1fd82bf81d6e1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilsen=20Hern=C3=A1ndez?= Date: Sun, 11 Sep 2022 14:34:24 -0400 Subject: [PATCH 4/6] Add additional checks to tests Update lambda client test Update checksum --- tests/Unit/DeploymentTest.php | 20 ++++++++++++++++++++ tests/Unit/LambdaClientTest.php | 12 +++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/tests/Unit/DeploymentTest.php b/tests/Unit/DeploymentTest.php index 3ad1282..21ff7f7 100644 --- a/tests/Unit/DeploymentTest.php +++ b/tests/Unit/DeploymentTest.php @@ -276,6 +276,7 @@ public function it_sets_function_tags() { $this->lambda->shouldReceive('functionExists')->andReturn(false); $this->lambda->shouldReceive('getVersions')->andReturn([]); + $this->lambda->shouldReceive('createFunction')->once()->with([ 'FunctionName' => 'test-FunctionName', 'Runtime' => 'test-Runtime', @@ -299,6 +300,25 @@ public function it_sets_function_tags() ], ]); + $this->lambda->shouldNotReceive('updateFunctionConfiguration'); + $this->lambda->shouldNotReceive('updateFunctionCode'); + + $this->lambda->shouldReceive('getLatestVersion') + ->once() + ->withArgs(function ($function) { + return $function instanceof DeploymentTestFunctionWithTags; + }) + ->andReturn('10'); + + $this->lambda->shouldReceive('aliasVersion') + ->once() + ->withArgs(function ($function, $alias, $version) { + return $function instanceof DeploymentTestFunctionWithTags + && $alias === 'active' + && $version === '10'; + }) + ->andReturn(LambdaClient::CREATED); + DeploymentTestFunctionWithTags::deploy($activate = true); $this->assertEvents($deployed = true, $activated = true); diff --git a/tests/Unit/LambdaClientTest.php b/tests/Unit/LambdaClientTest.php index 1bb1c9b..d09e86f 100644 --- a/tests/Unit/LambdaClientTest.php +++ b/tests/Unit/LambdaClientTest.php @@ -172,7 +172,7 @@ public function update_existing_function() $this->lambda->shouldReceive('functionExists') ->once() ->withArgs(function ($f, $checksum) use ($function) { - return $f === $function && $checksum === 'e827998e'; + return $f === $function && $checksum === '4ec93448'; }) ->andReturn(false); @@ -183,7 +183,7 @@ public function update_existing_function() 'Runtime' => 'test-Runtime', 'Role' => 'test-Role', 'Handler' => 'test-Handler', - 'Description' => 'test-Description [e827998e]', + 'Description' => 'test-Description [4ec93448]', 'Timeout' => 'test-Timeout', 'EphemeralStorage' => [ 'Size' => 'test-EphemeralStorage' @@ -192,7 +192,8 @@ public function update_existing_function() 'Layers' => 'test-Layers', 'Architectures' => [ Architecture::X86_64 - ] + ], + 'Tags' => [], ]); $this->lambda->shouldReceive('updateFunctionCode') @@ -234,7 +235,8 @@ public function update_existing_image_function() 'PackageType' => 'Image', 'Architectures' => [ Architecture::X86_64 - ] + ], + 'Tags' => [], ]); $this->lambda->shouldReceive('updateFunctionCode') @@ -259,7 +261,7 @@ public function existing_function_unchanged() $this->lambda->shouldReceive('functionExists') ->once() ->withArgs(function ($f, $checksum) use ($function) { - return $f === $function && $checksum === 'e827998e'; + return $f === $function && $checksum === '4ec93448'; }) ->andReturn(true); From 2328937233c5b723fb02f1e73d11e04d5aea585e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilsen=20Hern=C3=A1ndez?= Date: Sun, 11 Sep 2022 15:20:46 -0400 Subject: [PATCH 5/6] Fix last test --- tests/Unit/LambdaClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/LambdaClientTest.php b/tests/Unit/LambdaClientTest.php index d09e86f..d294f93 100644 --- a/tests/Unit/LambdaClientTest.php +++ b/tests/Unit/LambdaClientTest.php @@ -225,7 +225,7 @@ public function update_existing_image_function() ->with([ 'FunctionName' => 'test-FunctionName', 'Role' => null, - 'Description' => 'test-Description [ac420e45]', + 'Description' => 'test-Description [e280b565]', 'Timeout' => 300, 'MemorySize' => 512, 'EphemeralStorage' => [ From 8c42c3db01f9ecf624b780c8f54139133bce7160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilsen=20Hern=C3=A1ndez?= Date: Sun, 11 Sep 2022 15:21:15 -0400 Subject: [PATCH 6/6] Upgrade phpunit.xml.dist config using --migrate-configuration --- phpunit.xml.dist | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ab5011a..2a1e8c7 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,19 +9,19 @@ convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false"> - - - tests/Unit - - - tests/Integration - - - - - src/ - - - - - \ No newline at end of file + + + src/ + + + + + tests/Unit + + + tests/Integration + + + + +