Skip to content

Commit

Permalink
Merge pull request #86 from wilsenhc/feature/allow-tagging-feature
Browse files Browse the repository at this point in the history
Allow tagging functions
  • Loading branch information
aarondfrancis authored Feb 28, 2023
2 parents 0924e02 + 3e4db6f commit 19bd73b
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 9 deletions.
21 changes: 20 additions & 1 deletion docs/functions/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
</testsuite>
</testsuites>
<php>

</php>
</phpunit>
13 changes: 12 additions & 1 deletion src/LambdaFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,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
Expand Down Expand Up @@ -427,7 +437,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
Expand Down
57 changes: 56 additions & 1 deletion tests/Unit/DeploymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -269,6 +271,59 @@ 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(false);
$this->lambda->shouldReceive('getVersions')->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',
],
]);

$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);
}

/** @test */
public function it_throws_an_exception_if_there_are_no_functions()
{
Expand Down
14 changes: 8 additions & 6 deletions tests/Unit/LambdaClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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'
Expand All @@ -192,7 +192,8 @@ public function update_existing_function()
'Layers' => 'test-Layers',
'Architectures' => [
Architecture::X86_64
]
],
'Tags' => [],
]);

$this->lambda->shouldReceive('updateFunctionCode')
Expand Down Expand Up @@ -224,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' => [
Expand All @@ -234,7 +235,8 @@ public function update_existing_image_function()
'PackageType' => 'Image',
'Architectures' => [
Architecture::X86_64
]
],
'Tags' => [],
]);

$this->lambda->shouldReceive('updateFunctionCode')
Expand All @@ -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);

Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Support/DeploymentTestFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function toDeploymentArray()
'Layers' => 'test-Layers',
'Publish' => 'test-Publish',
'Architectures' => ['x86_64'],
'Tags' => $this->tags(),
];
}
}
16 changes: 16 additions & 0 deletions tests/Unit/Support/DeploymentTestFunctionWithTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* @author Aaron Francis <[email protected]|https://twitter.com/aarondfrancis>
*/

namespace Hammerstone\Sidecar\Tests\Unit\Support;

class DeploymentTestFunctionWithTags extends DeploymentTestFunction
{
public function tags()
{
return [
'Project' => 'Super Secret Project'
];
}
}

0 comments on commit 19bd73b

Please sign in to comment.