Skip to content

Commit 7f17f06

Browse files
committed
[sqs] Add ability to use another aws account per queue.
1 parent c285f64 commit 7f17f06

9 files changed

+78
-2
lines changed

docs/transport/sqs.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ It uses internally official [aws sdk library](https://packagist.org/packages/aws
1919
* [Send delay message](#send-delay-message)
2020
* [Consume message](#consume-message)
2121
* [Purge queue messages](#purge-queue-messages)
22+
* [Queue from another AWS account](#queue-from-another-aws-account)
2223

2324
## Installation
2425

@@ -122,4 +123,23 @@ $fooQueue = $context->createQueue('foo');
122123
$context->purgeQueue($fooQueue);
123124
```
124125

126+
## Queue from another AWS account
127+
128+
SQS allows to use queues from another account. You could set it globally for all queues via option `queue_owner_aws_account_id` or
129+
per queue using `SqsDestination::setQueueOwnerAWSAccountId` method.
130+
131+
```php
132+
<?php
133+
use Enqueue\Sqs\SqsConnectionFactory;
134+
135+
// globally for all queues
136+
$factory = new SqsConnectionFactory('sqs:?queue_owner_aws_account_id=awsAccountId');
137+
138+
$context = (new SqsConnectionFactory('sqs:'))->createContext();
139+
140+
// per queue.
141+
$queue = $context->createQueue('foo');
142+
$queue->setQueueOwnerAWSAccountId('awsAccountId');
143+
```
144+
125145
[back to index](../index.md)

pkg/sqs/SqsContext.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ public function getQueueUrl(SqsDestination $destination): string
153153
}
154154

155155
$arguments = ['QueueName' => $destination->getQueueName()];
156-
if (false == empty($this->config['queue_owner_aws_account_id'])) {
156+
157+
if ($destination->getQueueOwnerAWSAccountId()) {
158+
$arguments['QueueOwnerAWSAccountId'] = $destination->getQueueOwnerAWSAccountId();
159+
} elseif (false == empty($this->config['queue_owner_aws_account_id'])) {
157160
$arguments['QueueOwnerAWSAccountId'] = $this->config['queue_owner_aws_account_id'];
158161
}
159162

pkg/sqs/SqsDestination.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class SqsDestination implements Topic, Queue
1919
*/
2020
private $attributes;
2121

22+
/**
23+
* @var string|null
24+
*/
25+
private $queueOwnerAWSAccountId;
26+
2227
/**
2328
* The name of the new queue.
2429
* The following limits apply to this name:
@@ -187,4 +192,14 @@ public function setContentBasedDeduplication(bool $enable): void
187192
unset($this->attributes['ContentBasedDeduplication']);
188193
}
189194
}
195+
196+
public function getQueueOwnerAWSAccountId(): ?string
197+
{
198+
return $this->queueOwnerAWSAccountId;
199+
}
200+
201+
public function setQueueOwnerAWSAccountId(?string $queueOwnerAWSAccountId): void
202+
{
203+
$this->queueOwnerAWSAccountId = $queueOwnerAWSAccountId;
204+
}
190205
}

pkg/sqs/Tests/Spec/SqsSendToAndReceiveFromQueueTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44

55
use Enqueue\Sqs\SqsContext;
66
use Enqueue\Sqs\SqsDestination;
7+
use Enqueue\Test\RetryTrait;
78
use Enqueue\Test\SqsExtension;
89
use Interop\Queue\Context;
910
use Interop\Queue\Spec\SendToAndReceiveFromQueueSpec;
1011

1112
/**
1213
* @group functional
14+
* @retry 5
1315
*/
1416
class SqsSendToAndReceiveFromQueueTest extends SendToAndReceiveFromQueueSpec
1517
{
18+
use RetryTrait;
1619
use SqsExtension;
1720
use CreateSqsQueueTrait;
1821

pkg/sqs/Tests/Spec/SqsSendToAndReceiveNoWaitFromQueueTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44

55
use Enqueue\Sqs\SqsContext;
66
use Enqueue\Sqs\SqsDestination;
7+
use Enqueue\Test\RetryTrait;
78
use Enqueue\Test\SqsExtension;
89
use Interop\Queue\Context;
910
use Interop\Queue\Spec\SendToAndReceiveNoWaitFromQueueSpec;
1011

1112
/**
1213
* @group functional
14+
* @retry 5
1315
*/
1416
class SqsSendToAndReceiveNoWaitFromQueueTest extends SendToAndReceiveNoWaitFromQueueSpec
1517
{
18+
use RetryTrait;
1619
use SqsExtension;
1720
use CreateSqsQueueTrait;
1821

pkg/sqs/Tests/Spec/SqsSendToAndReceiveNoWaitFromTopicTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44

55
use Enqueue\Sqs\SqsContext;
66
use Enqueue\Sqs\SqsDestination;
7+
use Enqueue\Test\RetryTrait;
78
use Enqueue\Test\SqsExtension;
89
use Interop\Queue\Context;
910
use Interop\Queue\Spec\SendToAndReceiveNoWaitFromTopicSpec;
1011

1112
/**
1213
* @group functional
14+
* @retry 5
1315
*/
1416
class SqsSendToAndReceiveNoWaitFromTopicTest extends SendToAndReceiveNoWaitFromTopicSpec
1517
{
18+
use RetryTrait;
1619
use SqsExtension;
1720
use CreateSqsQueueTrait;
1821

pkg/sqs/Tests/Spec/SqsSendToTopicAndReceiveFromQueueTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44

55
use Enqueue\Sqs\SqsContext;
66
use Enqueue\Sqs\SqsDestination;
7+
use Enqueue\Test\RetryTrait;
78
use Enqueue\Test\SqsExtension;
89
use Interop\Queue\Context;
910
use Interop\Queue\Spec\SendToTopicAndReceiveFromQueueSpec;
1011

1112
/**
1213
* @group functional
14+
* @retry 5
1315
*/
1416
class SqsSendToTopicAndReceiveFromQueueTest extends SendToTopicAndReceiveFromQueueSpec
1517
{
18+
use RetryTrait;
1619
use SqsExtension;
1720
use CreateSqsQueueTrait;
1821

pkg/sqs/Tests/Spec/SqsSendToTopicAndReceiveNoWaitFromQueueTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44

55
use Enqueue\Sqs\SqsContext;
66
use Enqueue\Sqs\SqsDestination;
7+
use Enqueue\Test\RetryTrait;
78
use Enqueue\Test\SqsExtension;
89
use Interop\Queue\Context;
910
use Interop\Queue\Spec\SendToTopicAndReceiveNoWaitFromQueueSpec;
1011

1112
/**
1213
* @group functional
14+
* @retry 5
1315
*/
1416
class SqsSendToTopicAndReceiveNoWaitFromQueueTest extends SendToTopicAndReceiveNoWaitFromQueueSpec
1517
{
18+
use RetryTrait;
1619
use SqsExtension;
1720
use CreateSqsQueueTrait;
1821

pkg/sqs/Tests/SqsContextTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public function testShouldAllowGetQueueUrl()
220220
$context->getQueueUrl(new SqsDestination('aQueueName'));
221221
}
222222

223-
public function testShouldAllowGetQueueUrlFromAnotherAWSAccount()
223+
public function testShouldAllowGetQueueUrlFromAnotherAWSAccountSetGlobally()
224224
{
225225
$sqsClient = $this->createSqsClientMock();
226226
$sqsClient
@@ -240,6 +240,29 @@ public function testShouldAllowGetQueueUrlFromAnotherAWSAccount()
240240
$context->getQueueUrl(new SqsDestination('aQueueName'));
241241
}
242242

243+
public function testShouldAllowGetQueueUrlFromAnotherAWSAccountSetPerQueue()
244+
{
245+
$sqsClient = $this->createSqsClientMock();
246+
$sqsClient
247+
->expects($this->once())
248+
->method('getQueueUrl')
249+
->with($this->identicalTo([
250+
'QueueName' => 'aQueueName',
251+
'QueueOwnerAWSAccountId' => 'anotherAWSAccountID',
252+
]))
253+
->willReturn(new Result(['QueueUrl' => 'theQueueUrl']))
254+
;
255+
256+
$context = new SqsContext($sqsClient, [
257+
'queue_owner_aws_account_id' => null,
258+
]);
259+
260+
$queue = new SqsDestination('aQueueName');
261+
$queue->setQueueOwnerAWSAccountId('anotherAWSAccountID');
262+
263+
$context->getQueueUrl($queue);
264+
}
265+
243266
public function testShouldThrowExceptionIfGetQueueUrlResultHasNoQueueUrlProperty()
244267
{
245268
$sqsClient = $this->createSqsClientMock();

0 commit comments

Comments
 (0)