Skip to content

Commit 19bd73b

Browse files
Merge pull request #86 from wilsenhc/feature/allow-tagging-feature
Allow tagging functions
2 parents 0924e02 + 3e4db6f commit 19bd73b

File tree

7 files changed

+114
-9
lines changed

7 files changed

+114
-9
lines changed

docs/functions/customization.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ To change the allocated memory of your function, return the number in megabytes.
5050
```php
5151
class ExampleFunction extends LambdaFunction
5252
{
53-
public function memory() // [tl! focus:4]
53+
public function memory() // [tl! focus:4]
5454
{
5555
// 2GB of memory
5656
return 2048;
@@ -227,6 +227,25 @@ You likely won't need to change this, but if you do, *you must include the envir
227227
}
228228
```
229229

230+
## Tags
231+
232+
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.
233+
234+
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.
235+
236+
```php
237+
class ExampleFunction extends LambdaFunction
238+
{
239+
public function tags() // [tl! focus:start]
240+
{
241+
return [
242+
'Project' => 'Super Secret Project',
243+
'Department' => 'Logistics',
244+
];
245+
} // [tl! focus:end]
246+
}
247+
```
248+
230249
## Description
231250

232251
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.

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
</testsuite>
1515
</testsuites>
1616
<php>
17+
1718
</php>
1819
</phpunit>

src/LambdaFunction.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,16 @@ public function memory()
319319
return config('sidecar.memory');
320320
}
321321

322+
/**
323+
* A list of tags to apply to the function.
324+
*
325+
* @return array
326+
*/
327+
public function tags()
328+
{
329+
return [];
330+
}
331+
322332
/**
323333
* The function execution time, in seconds, at which Lambda should terminate the function.
324334
* Because the execution time has cost implications, we recommend you set this
@@ -427,7 +437,8 @@ public function toDeploymentArray()
427437
'Layers' => $this->layers(),
428438
'Publish' => true,
429439
'PackageType' => $this->packageType(),
430-
'Architectures' => [$this->architecture()]
440+
'Architectures' => [$this->architecture()],
441+
'Tags' => $this->tags(),
431442
];
432443

433444
// For container image packages, we need to remove the Runtime

tests/Unit/DeploymentTest.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Hammerstone\Sidecar\Events\BeforeFunctionsDeployed;
1515
use Hammerstone\Sidecar\Exceptions\NoFunctionsRegisteredException;
1616
use Hammerstone\Sidecar\Tests\Unit\Support\DeploymentTestFunction;
17+
use Hammerstone\Sidecar\Tests\Unit\Support\DeploymentTestFunctionWithTags;
1718
use Hammerstone\Sidecar\Tests\Unit\Support\DeploymentTestFunctionWithVariables;
1819
use Illuminate\Support\Facades\Event;
1920
use Mockery;
@@ -63,7 +64,8 @@ public function mockCreatingFunction()
6364
'MemorySize' => 'test-MemorySize',
6465
'Layers' => 'test-Layers',
6566
'Publish' => 'test-Publish',
66-
'Architectures' => ['x86_64']
67+
'Architectures' => ['x86_64'],
68+
'Tags' => [],
6769
]);
6870

6971
$this->lambda->shouldNotReceive('updateFunctionConfiguration');
@@ -269,6 +271,59 @@ public function it_doesnt_change_variables_that_havent_changed()
269271
$this->assertEvents($deployed = true, $activated = true);
270272
}
271273

274+
/** @test */
275+
public function it_sets_function_tags()
276+
{
277+
$this->lambda->shouldReceive('functionExists')->andReturn(false);
278+
$this->lambda->shouldReceive('getVersions')->andReturn([]);
279+
280+
$this->lambda->shouldReceive('createFunction')->once()->with([
281+
'FunctionName' => 'test-FunctionName',
282+
'Runtime' => 'test-Runtime',
283+
'Role' => 'test-Role',
284+
'Handler' => 'test-Handler',
285+
'Code' => [
286+
'S3Bucket' => 'test-bucket',
287+
'S3Key' => 'test-key',
288+
],
289+
'Description' => 'test-Description',
290+
'Timeout' => 'test-Timeout',
291+
'MemorySize' => 'test-MemorySize',
292+
'EphemeralStorage' => [
293+
'Size' => 'test-EphemeralStorage'
294+
],
295+
'Layers' => 'test-Layers',
296+
'Publish' => 'test-Publish',
297+
'Architectures' => ['x86_64'],
298+
'Tags' => [
299+
'Project' => 'Super Secret Project',
300+
],
301+
]);
302+
303+
$this->lambda->shouldNotReceive('updateFunctionConfiguration');
304+
$this->lambda->shouldNotReceive('updateFunctionCode');
305+
306+
$this->lambda->shouldReceive('getLatestVersion')
307+
->once()
308+
->withArgs(function ($function) {
309+
return $function instanceof DeploymentTestFunctionWithTags;
310+
})
311+
->andReturn('10');
312+
313+
$this->lambda->shouldReceive('aliasVersion')
314+
->once()
315+
->withArgs(function ($function, $alias, $version) {
316+
return $function instanceof DeploymentTestFunctionWithTags
317+
&& $alias === 'active'
318+
&& $version === '10';
319+
})
320+
->andReturn(LambdaClient::CREATED);
321+
322+
DeploymentTestFunctionWithTags::deploy($activate = true);
323+
324+
$this->assertEvents($deployed = true, $activated = true);
325+
}
326+
272327
/** @test */
273328
public function it_throws_an_exception_if_there_are_no_functions()
274329
{

tests/Unit/LambdaClientTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public function update_existing_function()
172172
$this->lambda->shouldReceive('functionExists')
173173
->once()
174174
->withArgs(function ($f, $checksum) use ($function) {
175-
return $f === $function && $checksum === 'e827998e';
175+
return $f === $function && $checksum === '4ec93448';
176176
})
177177
->andReturn(false);
178178

@@ -183,7 +183,7 @@ public function update_existing_function()
183183
'Runtime' => 'test-Runtime',
184184
'Role' => 'test-Role',
185185
'Handler' => 'test-Handler',
186-
'Description' => 'test-Description [e827998e]',
186+
'Description' => 'test-Description [4ec93448]',
187187
'Timeout' => 'test-Timeout',
188188
'EphemeralStorage' => [
189189
'Size' => 'test-EphemeralStorage'
@@ -192,7 +192,8 @@ public function update_existing_function()
192192
'Layers' => 'test-Layers',
193193
'Architectures' => [
194194
Architecture::X86_64
195-
]
195+
],
196+
'Tags' => [],
196197
]);
197198

198199
$this->lambda->shouldReceive('updateFunctionCode')
@@ -224,7 +225,7 @@ public function update_existing_image_function()
224225
->with([
225226
'FunctionName' => 'test-FunctionName',
226227
'Role' => null,
227-
'Description' => 'test-Description [ac420e45]',
228+
'Description' => 'test-Description [e280b565]',
228229
'Timeout' => 300,
229230
'MemorySize' => 512,
230231
'EphemeralStorage' => [
@@ -234,7 +235,8 @@ public function update_existing_image_function()
234235
'PackageType' => 'Image',
235236
'Architectures' => [
236237
Architecture::X86_64
237-
]
238+
],
239+
'Tags' => [],
238240
]);
239241

240242
$this->lambda->shouldReceive('updateFunctionCode')
@@ -259,7 +261,7 @@ public function existing_function_unchanged()
259261
$this->lambda->shouldReceive('functionExists')
260262
->once()
261263
->withArgs(function ($f, $checksum) use ($function) {
262-
return $f === $function && $checksum === 'e827998e';
264+
return $f === $function && $checksum === '4ec93448';
263265
})
264266
->andReturn(true);
265267

tests/Unit/Support/DeploymentTestFunction.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function toDeploymentArray()
5252
'Layers' => 'test-Layers',
5353
'Publish' => 'test-Publish',
5454
'Architectures' => ['x86_64'],
55+
'Tags' => $this->tags(),
5556
];
5657
}
5758
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* @author Aaron Francis <[email protected]|https://twitter.com/aarondfrancis>
4+
*/
5+
6+
namespace Hammerstone\Sidecar\Tests\Unit\Support;
7+
8+
class DeploymentTestFunctionWithTags extends DeploymentTestFunction
9+
{
10+
public function tags()
11+
{
12+
return [
13+
'Project' => 'Super Secret Project'
14+
];
15+
}
16+
}

0 commit comments

Comments
 (0)