Skip to content

Commit 1c276fd

Browse files
authored
Merge pull request #25 from renoki-co/feature/upgrade-sns-events
[5.x] Upgrade SNS to 7.0
2 parents de1c30d + 8f11be5 commit 1c276fd

File tree

9 files changed

+135
-186
lines changed

9 files changed

+135
-186
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"require": {
1515
"illuminate/http": "^7.30|^8.23",
1616
"illuminate/support": "^7.30|^8.23",
17-
"rennokki/laravel-sns-events": "^6.4"
17+
"rennokki/laravel-sns-events": "^7.0"
1818
},
1919
"autoload": {
2020
"psr-4": {
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
namespace RenokiCo\AwsWebhooks\Concerns;
4+
5+
use Rennokki\LaravelSnsEvents\Concerns\GeneratesSnsMessages;
6+
7+
trait GeneratesSnsWebhookMessages
8+
{
9+
use GeneratesSnsMessages;
10+
11+
/**
12+
* Get the SES message for mails.
13+
*
14+
* @param string $type
15+
* @return array
16+
*/
17+
public function getSesMessage(string $type): array
18+
{
19+
return $this->getNotificationPayload([
20+
'eventType' => $type,
21+
]);
22+
}
23+
24+
/**
25+
* Get an example cloudwatch notification payload.
26+
*
27+
* @param string $currentState
28+
* @param string $previousState
29+
* @return array
30+
*/
31+
public function getCloudwatchMessage(string $currentState = 'ALARM', string $previousState = 'OK'): array
32+
{
33+
return $this->getNotificationPayload([
34+
'AlarmName' => 'Some Alarm',
35+
'AlarmDescription' => 'This is the alarm description. Pass it some nice info.',
36+
'AWSAccountId' => '305476205504',
37+
'NewStateValue' => $currentState,
38+
'NewStateReason' => 'Threshold Crossed: 1 out of the last 1 datapoints [1.83333333333091 (24/06/20 20:25:00)] was greater than the threshold (1.6) (minimum 1 datapoint for OK -> ALARM transition).',
39+
'StateChangeTime' => '2020-06-24T20:27:43.474+0000',
40+
'Region' => 'US East (N. Virginia)',
41+
'AlarmArn' => 'arn:aws:cloudwatch:us-east-1:305476205504:alarm:Some Alarm',
42+
'OldStateValue' => $previousState,
43+
'Trigger' => [
44+
'MetricName' => 'CPUUtilization',
45+
'Namespace' => 'AWS/RDS',
46+
'StatisticType' => 'Statistic',
47+
'Statistic' => 'AVERAGE',
48+
'Unit' => null,
49+
'Dimensions' => [
50+
[
51+
'value' => 'some-db-identifier',
52+
'name' => 'DBInstanceIdentifier',
53+
],
54+
],
55+
'Period' => 60,
56+
'EvaluationPeriods' => 1,
57+
'ComparisonOperator' => 'GreaterThanThreshold',
58+
'Threshold' => 1.6,
59+
'EvaluateLowSampleCountPercentile' => '',
60+
],
61+
]);
62+
}
63+
64+
/**
65+
* Get an example EC2 notification payload.
66+
*
67+
* @return array
68+
*/
69+
public function getEc2Message(): array
70+
{
71+
return $this->getNotificationPayload([
72+
'version' => 0,
73+
'id' => '72d6566c-e6bd-117d-5bbc-2779c679abd5',
74+
'detail-type' => 'EC2 Spot Instance Interruption Warning',
75+
'source' => 'aws.ec2',
76+
'account' => '12345678910',
77+
'time' => '2020-03-06T08:25:40Z',
78+
'region' => 'eu-west-1',
79+
'resources' => [
80+
'arn:aws:ec2:eu-west-1a:instance/i-0cefe48f36d7c281a',
81+
],
82+
'detail' => [
83+
'instance-id' => 'i-0cefe48f36d7c281a',
84+
'instance-action' => 'terminate',
85+
],
86+
]);
87+
}
88+
89+
/**
90+
* Get an example Gamelift notification payload.
91+
*
92+
* @return array
93+
*/
94+
public function getGameliftMessage(): array
95+
{
96+
return $this->getNotificationPayload([
97+
'version' => 0,
98+
'id' => '72d6566c-e6bd-117d-5bbc-2779c679abd5',
99+
'detail-type' => 'GameLift Matchmaking Event',
100+
'source' => 'aws.gamelift',
101+
'account' => '12345678910',
102+
'time' => '2020-03-06T08:25:40Z',
103+
'region' => 'eu-west-1',
104+
'resources' => [
105+
'arn:aws:gamelift:us-west-2:123456789012:matchmakingconfiguration/SampleConfiguration',
106+
],
107+
'detail' => [
108+
'tickets' => [
109+
[
110+
'ticketId' => 'ticket-1',
111+
'startTime' => '2017-08-08T21:15:35.676Z',
112+
'players' => [
113+
['playerId' => 'player-1'],
114+
],
115+
],
116+
],
117+
'estimatedWaitMillis' => 'NOT_AVAILABLE',
118+
'type' => 'MatchmakingSearching',
119+
'gameSessionInfo' => [
120+
'players' => [
121+
['playerId' => 'player-1'],
122+
],
123+
],
124+
],
125+
]);
126+
}
127+
}

tests/CloudwatchTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ public function test_callable_methods()
1313
];
1414

1515
foreach ($payloads as $payload) {
16-
$this->withHeaders($this->getHeadersForMessage($payload))
17-
->json('GET', route('cloudwatch', ['certificate' => static::$certificate]), $payload)
16+
$this->sendSnsMessage(route('cloudwatch'), $payload)
1817
->assertSee('OK');
1918
}
2019
}

tests/Controllers/CloudwatchController.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,11 @@
22

33
namespace RenokiCo\AwsWebhooks\Test\Controllers;
44

5-
use Aws\Sns\MessageValidator;
65
use Illuminate\Http\Request;
76
use RenokiCo\AwsWebhooks\Http\Controllers\CloudwatchWebhook;
87

98
class CloudwatchController extends CloudwatchWebhook
109
{
11-
/**
12-
* Get the message validator instance.
13-
*
14-
* @param \Illuminate\Http\Request $request
15-
* @return \Aws\Sns\MessageValidator
16-
*/
17-
protected function getMessageValidator(Request $request)
18-
{
19-
return new MessageValidator(function ($url) use ($request) {
20-
return $request->certificate ?: $url;
21-
});
22-
}
23-
2410
/**
2511
* Handle the event when an alarm transitioned to OK.
2612
*

tests/Controllers/EventbridgeController.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,11 @@
22

33
namespace RenokiCo\AwsWebhooks\Test\Controllers;
44

5-
use Aws\Sns\MessageValidator;
65
use Illuminate\Http\Request;
76
use RenokiCo\AwsWebhooks\Http\Controllers\EventbridgeWebhook;
87

98
class EventbridgeController extends EventbridgeWebhook
109
{
11-
/**
12-
* Get the message validator instance.
13-
*
14-
* @param \Illuminate\Http\Request $request
15-
* @return \Aws\Sns\MessageValidator
16-
*/
17-
protected function getMessageValidator(Request $request)
18-
{
19-
return new MessageValidator(function ($url) use ($request) {
20-
return $request->certificate ?: $url;
21-
});
22-
}
23-
2410
/**
2511
* Handle the event coming from Autoscaling.
2612
*

tests/Controllers/SesController.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,11 @@
22

33
namespace RenokiCo\AwsWebhooks\Test\Controllers;
44

5-
use Aws\Sns\MessageValidator;
65
use Illuminate\Http\Request;
76
use RenokiCo\AwsWebhooks\Http\Controllers\SesWebhook;
87

98
class SesController extends SesWebhook
109
{
11-
/**
12-
* Get the message validator instance.
13-
*
14-
* @param \Illuminate\Http\Request $request
15-
* @return \Aws\Sns\MessageValidator
16-
*/
17-
protected function getMessageValidator(Request $request)
18-
{
19-
return new MessageValidator(function ($url) use ($request) {
20-
return $request->certificate ?: $url;
21-
});
22-
}
23-
2410
/**
2511
* Handle the Rendering Failure event.
2612
*

tests/EventbridgeTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@ public function test_call_with_existing_event()
88
{
99
$payload = $this->getGameliftMessage();
1010

11-
$this->withHeaders($this->getHeadersForMessage($payload))
12-
->json('GET', route('eventbridge', ['certificate' => static::$certificate]), $payload)
11+
$this->sendSnsMessage(route('eventbridge'), $payload)
1312
->assertSee('OK');
1413
}
1514

1615
public function test_call_with_inexisting_event()
1716
{
1817
$payload = $this->getEc2Message();
1918

20-
$this->withHeaders($this->getHeadersForMessage($payload))
21-
->json('GET', route('eventbridge', ['certificate' => static::$certificate]), $payload)
19+
$this->sendSnsMessage(route('eventbridge'), $payload)
2220
->assertSee('OK');
2321
}
2422
}

tests/SesTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@ public function test_calling_existing_method()
88
{
99
$payload = $this->getSesMessage('Rendering Failure');
1010

11-
$this->withHeaders($this->getHeadersForMessage($payload))
12-
->json('GET', route('ses', ['certificate' => static::$certificate]), $payload)
11+
$this->sendSnsMessage(route('ses'), $payload)
1312
->assertSee('OK');
1413
}
1514

1615
public function test_calling_inexisting_method()
1716
{
1817
$payload = $this->getSesMessage('Bounce');
1918

20-
$this->withHeaders($this->getHeadersForMessage($payload))
21-
->json('GET', route('ses', ['certificate' => static::$certificate]), $payload)
19+
$this->sendSnsMessage(route('ses'), $payload)
2220
->assertSee('OK');
2321
}
2422
}

0 commit comments

Comments
 (0)