Skip to content

Commit

Permalink
Merge pull request #666 from php-enqueue/sqs-another-account
Browse files Browse the repository at this point in the history
[sqs] Add ability to use another aws account per queue.
  • Loading branch information
makasim authored Dec 1, 2018
2 parents c285f64 + 7f17f06 commit 4e23151
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 2 deletions.
20 changes: 20 additions & 0 deletions docs/transport/sqs.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ It uses internally official [aws sdk library](https://packagist.org/packages/aws
* [Send delay message](#send-delay-message)
* [Consume message](#consume-message)
* [Purge queue messages](#purge-queue-messages)
* [Queue from another AWS account](#queue-from-another-aws-account)

## Installation

Expand Down Expand Up @@ -122,4 +123,23 @@ $fooQueue = $context->createQueue('foo');
$context->purgeQueue($fooQueue);
```

## Queue from another AWS account

SQS allows to use queues from another account. You could set it globally for all queues via option `queue_owner_aws_account_id` or
per queue using `SqsDestination::setQueueOwnerAWSAccountId` method.

```php
<?php
use Enqueue\Sqs\SqsConnectionFactory;

// globally for all queues
$factory = new SqsConnectionFactory('sqs:?queue_owner_aws_account_id=awsAccountId');

$context = (new SqsConnectionFactory('sqs:'))->createContext();

// per queue.
$queue = $context->createQueue('foo');
$queue->setQueueOwnerAWSAccountId('awsAccountId');
```

[back to index](../index.md)
5 changes: 4 additions & 1 deletion pkg/sqs/SqsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ public function getQueueUrl(SqsDestination $destination): string
}

$arguments = ['QueueName' => $destination->getQueueName()];
if (false == empty($this->config['queue_owner_aws_account_id'])) {

if ($destination->getQueueOwnerAWSAccountId()) {
$arguments['QueueOwnerAWSAccountId'] = $destination->getQueueOwnerAWSAccountId();
} elseif (false == empty($this->config['queue_owner_aws_account_id'])) {
$arguments['QueueOwnerAWSAccountId'] = $this->config['queue_owner_aws_account_id'];
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/sqs/SqsDestination.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class SqsDestination implements Topic, Queue
*/
private $attributes;

/**
* @var string|null
*/
private $queueOwnerAWSAccountId;

/**
* The name of the new queue.
* The following limits apply to this name:
Expand Down Expand Up @@ -187,4 +192,14 @@ public function setContentBasedDeduplication(bool $enable): void
unset($this->attributes['ContentBasedDeduplication']);
}
}

public function getQueueOwnerAWSAccountId(): ?string
{
return $this->queueOwnerAWSAccountId;
}

public function setQueueOwnerAWSAccountId(?string $queueOwnerAWSAccountId): void
{
$this->queueOwnerAWSAccountId = $queueOwnerAWSAccountId;
}
}
3 changes: 3 additions & 0 deletions pkg/sqs/Tests/Spec/SqsSendToAndReceiveFromQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

use Enqueue\Sqs\SqsContext;
use Enqueue\Sqs\SqsDestination;
use Enqueue\Test\RetryTrait;
use Enqueue\Test\SqsExtension;
use Interop\Queue\Context;
use Interop\Queue\Spec\SendToAndReceiveFromQueueSpec;

/**
* @group functional
* @retry 5
*/
class SqsSendToAndReceiveFromQueueTest extends SendToAndReceiveFromQueueSpec
{
use RetryTrait;
use SqsExtension;
use CreateSqsQueueTrait;

Expand Down
3 changes: 3 additions & 0 deletions pkg/sqs/Tests/Spec/SqsSendToAndReceiveNoWaitFromQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

use Enqueue\Sqs\SqsContext;
use Enqueue\Sqs\SqsDestination;
use Enqueue\Test\RetryTrait;
use Enqueue\Test\SqsExtension;
use Interop\Queue\Context;
use Interop\Queue\Spec\SendToAndReceiveNoWaitFromQueueSpec;

/**
* @group functional
* @retry 5
*/
class SqsSendToAndReceiveNoWaitFromQueueTest extends SendToAndReceiveNoWaitFromQueueSpec
{
use RetryTrait;
use SqsExtension;
use CreateSqsQueueTrait;

Expand Down
3 changes: 3 additions & 0 deletions pkg/sqs/Tests/Spec/SqsSendToAndReceiveNoWaitFromTopicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

use Enqueue\Sqs\SqsContext;
use Enqueue\Sqs\SqsDestination;
use Enqueue\Test\RetryTrait;
use Enqueue\Test\SqsExtension;
use Interop\Queue\Context;
use Interop\Queue\Spec\SendToAndReceiveNoWaitFromTopicSpec;

/**
* @group functional
* @retry 5
*/
class SqsSendToAndReceiveNoWaitFromTopicTest extends SendToAndReceiveNoWaitFromTopicSpec
{
use RetryTrait;
use SqsExtension;
use CreateSqsQueueTrait;

Expand Down
3 changes: 3 additions & 0 deletions pkg/sqs/Tests/Spec/SqsSendToTopicAndReceiveFromQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

use Enqueue\Sqs\SqsContext;
use Enqueue\Sqs\SqsDestination;
use Enqueue\Test\RetryTrait;
use Enqueue\Test\SqsExtension;
use Interop\Queue\Context;
use Interop\Queue\Spec\SendToTopicAndReceiveFromQueueSpec;

/**
* @group functional
* @retry 5
*/
class SqsSendToTopicAndReceiveFromQueueTest extends SendToTopicAndReceiveFromQueueSpec
{
use RetryTrait;
use SqsExtension;
use CreateSqsQueueTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

use Enqueue\Sqs\SqsContext;
use Enqueue\Sqs\SqsDestination;
use Enqueue\Test\RetryTrait;
use Enqueue\Test\SqsExtension;
use Interop\Queue\Context;
use Interop\Queue\Spec\SendToTopicAndReceiveNoWaitFromQueueSpec;

/**
* @group functional
* @retry 5
*/
class SqsSendToTopicAndReceiveNoWaitFromQueueTest extends SendToTopicAndReceiveNoWaitFromQueueSpec
{
use RetryTrait;
use SqsExtension;
use CreateSqsQueueTrait;

Expand Down
25 changes: 24 additions & 1 deletion pkg/sqs/Tests/SqsContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public function testShouldAllowGetQueueUrl()
$context->getQueueUrl(new SqsDestination('aQueueName'));
}

public function testShouldAllowGetQueueUrlFromAnotherAWSAccount()
public function testShouldAllowGetQueueUrlFromAnotherAWSAccountSetGlobally()
{
$sqsClient = $this->createSqsClientMock();
$sqsClient
Expand All @@ -240,6 +240,29 @@ public function testShouldAllowGetQueueUrlFromAnotherAWSAccount()
$context->getQueueUrl(new SqsDestination('aQueueName'));
}

public function testShouldAllowGetQueueUrlFromAnotherAWSAccountSetPerQueue()
{
$sqsClient = $this->createSqsClientMock();
$sqsClient
->expects($this->once())
->method('getQueueUrl')
->with($this->identicalTo([
'QueueName' => 'aQueueName',
'QueueOwnerAWSAccountId' => 'anotherAWSAccountID',
]))
->willReturn(new Result(['QueueUrl' => 'theQueueUrl']))
;

$context = new SqsContext($sqsClient, [
'queue_owner_aws_account_id' => null,
]);

$queue = new SqsDestination('aQueueName');
$queue->setQueueOwnerAWSAccountId('anotherAWSAccountID');

$context->getQueueUrl($queue);
}

public function testShouldThrowExceptionIfGetQueueUrlResultHasNoQueueUrlProperty()
{
$sqsClient = $this->createSqsClientMock();
Expand Down

0 comments on commit 4e23151

Please sign in to comment.