8000 feature #59822 [Messenger] Add options to specify SQS queue attribute… · symfony/symfony@0fc079f · GitHub
[go: up one dir, main page]

Skip to content

Commit 0fc079f

Browse files
feature #59822 [Messenger] Add options to specify SQS queue attributes and tags (TrePe0)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [Messenger] Add options to specify SQS queue attributes and tags | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | | License | MIT When automatically creating SQS queue, the new options `queue_attributes` and `queue_tags` can be used to specify queue attributes and cost allocation tags as per [SQS CreateQueue API](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_CreateQueue.html) Usage example: ``` $queueName = 'queueName.fifo'; $queueAttributes = [ QueueAttributeName::MESSAGE_RETENTION_PERIOD => '900', QueueAttributeName::MAXIMUM_MESSAGE_SIZE => '1024', ]; $queueTags = ['tag1' => 'value1', 'tag2' => 'value2']; $connection = new Connection(['queue_name' => $queueName, 'queue_attributes' => $queueAttributes, 'queue_tags' => $queueTags], $client); ``` Commits ------- 99e3f4d [Messenger] Add options to specify SQS queue attributes and tags
2 parents 68944f5 + 99e3f4d commit 0fc079f

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

src/Symfony/Component/Messenger/Bridge/AmazonSqs/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Add new `queue_attributes` and `queue_tags` options for SQS queue creation
8+
49
7.2
510
---
611

src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/ConnectionTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
use AsyncAws\Core\Exception\Http\HttpException;
1515
use AsyncAws\Core\Test\ResultMockFactory;
16+
use AsyncAws\Sqs\Enum\QueueAttributeName;
1617
use AsyncAws\Sqs\Result\GetQueueUrlResult;
18+
use AsyncAws\Sqs\Result\QueueExistsWaiter;
1719
use AsyncAws\Sqs\Result\ReceiveMessageResult;
1820
use AsyncAws\Sqs\SqsClient;
1921
use AsyncAws\Sqs\ValueObject\Message;
@@ -385,6 +387,43 @@ public function testKeepaliveWithTooSmallTtl()
385387
$connection->keepalive('123', 2);
386388
}
387389

390+
public function testQueueAttributesAndTags()
391+
{
392+
$queueName = 'queueName.fifo';
393+
$queueAttributes = [
394+
QueueAttributeName::MESSAGE_RETENTION_PERIOD => '900',
395+
QueueAttributeName::MAXIMUM_MESSAGE_SIZE => '1024',
396+
];
397+
$queueTags = ['tag1' => 'value1', 'tag2' => 'value2'];
398+
399+
$queueExistsWaiter = ResultMockFactory::waiter(QueueExistsWaiter::class, QueueExistsWaiter::STATE_FAILURE);
400+
$client = $this->createMock(SqsClient::class);
401+
$client->method('queueExists')->willReturn($queueExistsWaiter);
402+
$client->expects($this->once())->method('createQueue')->with(['QueueName' => $queueName, 'Attributes' => array_merge($queueAttributes, [QueueAttributeName::FIFO_QUEUE => 'true']), 'tags' => $queueTags]);
403+
404+
$connection = new Connection(['queue_name' => $queueName, 'queue_attributes' => $queueAttributes, 'queue_tags' => $queueTags], $client);
405+
406+
$this->expectException(TransportException::class);
407+
$connection->setup();
408+
}
409+
410+
public function testQueueAttributesAndTagsFromDsn()
411+
{
412+
$httpClient = $this->createMock(HttpClientInterface::class);
413+
414+
$queueName = 'queueName';
415+
$queueAttributes = [
416+
QueueAttributeName::MESSAGE_RETENTION_PERIOD => '900',
417+
QueueAttributeName::MAXIMUM_MESSAGE_SIZE => '1024',
418+
];
419+
$queueTags = ['tag1' => 'value1', 'tag2' => 'value2'];
420+
421+
$this->assertEquals(
422+
new Connection(['queue_name' => $queueName, 'queue_attributes' => $queueAttributes, 'queue_tags' => $queueTags], new SqsClient(['region' => 'eu-west-1', 'accessKeyId' => null, 'accessKeySecret' => null], null, $httpClient)),
423+
Connection::fromDsn('sqs://default/'.$queueName, ['queue_attributes' => $queueAttributes, 'queue_tags' => $queueTags], $httpClient)
424+
);
425+
}
426+
388427
private function getMockedQueueUrlResponse(): MockResponse
389428
{
390429
if ($this->isAsyncAwsSqsVersion2Installed()) {

src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/Connection.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class Connection
4646
'endpoint' => 'https://sqs.eu-west-1.amazonaws.com',
4747
'region' => 'eu-west-1',
4848
'queue_name' => 'messages',
49+
'queue_attributes' => null,
50+
'queue_tags' => null,
4951
'account' => null,
5052
'sslmode' => null,
5153
'debug' => null,
@@ -89,6 +91,8 @@ public function __destruct()
8991
* * endpoint: absolute URL to the SQS service (Default: https://sqs.eu-west-1.amazonaws.com)
9092
* * region: name of the AWS region (Default: eu-west-1)
9193
* * queue_name: name of the queue (Default: messages)
94+
* * queue_attributes: attributes of the queue, array<QueueAttributeName::*, string>
95+
* * queue_tags: tags of the queue, array<string, string>
9296
* * account: identifier of the AWS account
9397
* * access_key: AWS access key
9498
* * secret_key: AWS secret key
@@ -132,6 +136,8 @@ public static function fromDsn(#[\SensitiveParameter] string $dsn, array $option
132136
'visibility_timeout' => null !== $options['visibility_timeout'] ? (int) $options['visibility_timeout'] : null,
133137
'auto_setup' => filter_var($options['auto_setup'], \FILTER_VALIDATE_BOOL),
134138
'queue_name' => (string) $options['queue_name'],
139+
'queue_attributes' => $options['queue_attributes'],
140+
'queue_tags' => $options['queue_tags'],
135141
];
136142

137143
$clientConfiguration = [
@@ -278,12 +284,14 @@ public function setup(): void
278284
throw new InvalidArgumentException(\sprintf('The Amazon SQS queue "%s" does not exist (or you don\'t have permissions on it), and can\'t be created when an account is provided.', $this->configuration['queue_name']));
279285
}
280286

281-
$parameters = ['QueueName' => $this->configuration['queue_name']];
287+
$parameters = [
288+
'QueueName' => $this->configuration['queue_name'],
289+
'Attributes' => $this->configuration['queue_attributes'],
290+
'tags' => $this->configuration['queue_tags'],
291+
];
282292

283293
if (self::isFifoQueue($this->configuration['queue_name'])) {
284-
$parameters['Attributes'] = [
285-
'FifoQueue' => 'true',
286-
];
294+
$parameters['Attributes'][QueueAttributeName::FIFO_QUEUE] = 'true';
287295
}
288296

289297
$this->client->createQueue($parameters);

0 commit comments

Comments
 (0)
0