Skip to content

Commit da05a6a

Browse files
authored
Merge pull request #662 from php-enqueue/sqs-use-queue-from-another-account
[sqs] Use a queue created in another AWS account.
2 parents 047a7a8 + 3aace61 commit da05a6a

13 files changed

+231
-123
lines changed

Diff for: pkg/sqs/SqsConnectionFactory.php

+13-10
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ class SqsConnectionFactory implements ConnectionFactory
2323

2424
/**
2525
* $config = [
26-
* 'key' => null - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
27-
* 'secret' => null, - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
28-
* 'token' => null, - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
29-
* 'region' => null, - (string, required) Region to connect to. See http://docs.aws.amazon.com/general/latest/gr/rande.html for a list of available regions.
30-
* 'retries' => 3, - (int, default=int(3)) Configures the maximum number of allowed retries for a client (pass 0 to disable retries).
31-
* 'version' => '2012-11-05', - (string, required) The version of the webservice to utilize
32-
* 'lazy' => true, - Enable lazy connection (boolean)
33-
* 'endpoint' => null - (string, default=null) The full URI of the webservice. This is only required when connecting to a custom endpoint e.g. localstack
26+
* 'key' => null AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
27+
* 'secret' => null, AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
28+
* 'token' => null, AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
29+
* 'region' => null, (string, required) Region to connect to. See http://docs.aws.amazon.com/general/latest/gr/rande.html for a list of available regions.
30+
* 'retries' => 3, (int, default=int(3)) Configures the maximum number of allowed retries for a client (pass 0 to disable retries).
31+
* 'version' => '2012-11-05', (string, required) The version of the webservice to utilize
32+
* 'lazy' => true, Enable lazy connection (boolean)
33+
* 'endpoint' => null (string, default=null) The full URI of the webservice. This is only required when connecting to a custom endpoint e.g. localstack
34+
* 'queue_owner_aws_account_id' The AWS account ID of the account that created the queue.
3435
* ].
3536
*
3637
* or
@@ -74,10 +75,10 @@ public function createContext(): Context
7475
if ($this->config['lazy']) {
7576
return new SqsContext(function () {
7677
return $this->establishConnection();
77-
});
78+
}, $this->config);
7879
}
7980

80-
return new SqsContext($this->establishConnection());
81+
return new SqsContext($this->establishConnection(), $this->config);
8182
}
8283

8384
private function establishConnection(): SqsClient
@@ -132,6 +133,7 @@ private function parseDsn(string $dsn): array
132133
'version' => $dsn->getString('version'),
133134
'lazy' => $dsn->getBool('lazy'),
134135
'endpoint' => $dsn->getString('endpoint'),
136+
'queue_owner_aws_account_id' => $dsn->getString('queue_owner_aws_account_id'),
135137
]), function ($value) { return null !== $value; });
136138
}
137139

@@ -146,6 +148,7 @@ private function defaultConfig(): array
146148
'version' => '2012-11-05',
147149
'lazy' => true,
148150
'endpoint' => null,
151+
'queue_owner_aws_account_id' => null,
149152
];
150153
}
151154
}

Diff for: pkg/sqs/SqsContext.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ class SqsContext implements Context
3434
*/
3535
private $queueUrls;
3636

37+
private $config;
38+
3739
/**
3840
* Callable must return instance of SqsClient once called.
3941
*
4042
* @param SqsClient|callable $client
4143
*/
42-
public function __construct($client)
44+
public function __construct($client, array $config)
4345
{
4446
if ($client instanceof SqsClient) {
4547
$this->client = $client;
@@ -52,6 +54,8 @@ public function __construct($client)
5254
SqsClient::class
5355
));
5456
}
57+
58+
$this->config = $config;
5559
}
5660

5761
/**
@@ -148,15 +152,18 @@ public function getQueueUrl(SqsDestination $destination): string
148152
return $this->queueUrls[$destination->getQueueName()];
149153
}
150154

151-
$result = $this->getClient()->getQueueUrl([
152-
'QueueName' => $destination->getQueueName(),
153-
]);
155+
$arguments = ['QueueName' => $destination->getQueueName()];
156+
if (false == empty($this->config['queue_owner_aws_account_id'])) {
157+
$arguments['QueueOwnerAWSAccountId'] = $this->config['queue_owner_aws_account_id'];
158+
}
159+
160+
$result = $this->getClient()->getQueueUrl($arguments);
154161

155162
if (false == $result->hasKey('QueueUrl')) {
156163
throw new \RuntimeException(sprintf('QueueUrl cannot be resolved. queueName: "%s"', $destination->getQueueName()));
157164
}
158165

159-
return $this->queueUrls[$destination->getQueueName()] = $result->get('QueueUrl');
166+
return $this->queueUrls[$destination->getQueueName()] = (string) $result->get('QueueUrl');
160167
}
161168

162169
public function declareQueue(SqsDestination $dest): void

Diff for: pkg/sqs/Tests/Spec/CreateSqsQueueTrait.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Enqueue\Sqs\Tests\Spec;
4+
5+
use Enqueue\Sqs\SqsContext;
6+
use Enqueue\Sqs\SqsDestination;
7+
8+
trait CreateSqsQueueTrait
9+
{
10+
private $queue;
11+
12+
protected function createSqsQueue(SqsContext $context, string $queueName): SqsDestination
13+
{
14+
$queueName = $queueName.time();
15+
16+
$this->queue = $context->createQueue($queueName);
17+
$context->declareQueue($this->queue);
18+
19+
return $this->queue;
20+
}
21+
}

Diff for: pkg/sqs/Tests/Spec/SqsSendAndReceiveDelayedMessageFromQueueTest.php

+4-21
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,13 @@ class SqsSendAndReceiveDelayedMessageFromQueueTest extends SendAndReceiveDelayed
1717
{
1818
use RetryTrait;
1919
use SqsExtension;
20+
use CreateSqsQueueTrait;
2021

2122
/**
2223
* @var SqsContext
2324
*/
2425
private $context;
2526

26-
/**
27-
* @var SqsDestination
28-
*/
29-
private $queue;
30-
3127
protected function tearDown()
3228
{
3329
parent::tearDown();
@@ -37,26 +33,13 @@ protected function tearDown()
3733
}
3834
}
3935

40-
/**
41-
* {@inheritdoc}
42-
*/
43-
protected function createContext()
36+
protected function createContext(): SqsContext
4437
{
4538
return $this->context = $this->buildSqsContext();
4639
}
4740

48-
/**
49-
* {@inheritdoc}
50-
*
51-
* @param SqsContext $context
52-
*/
53-
protected function createQueue(Context $context, $queueName)
41+
protected function createQueue(Context $context, $queueName): SqsDestination
5442
{
55-
$queueName = $queueName.time();
56-
57-
$this->queue = $context->createQueue($queueName);
58-
$context->declareQueue($this->queue);
59-
60-
return $this->queue;
43+
return $this->createSqsQueue($context, $queueName);
6144
}
6245
}

Diff for: pkg/sqs/Tests/Spec/SqsSendToAndReceiveFromQueueTest.php

+4-21
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,13 @@
1414
class SqsSendToAndReceiveFromQueueTest extends SendToAndReceiveFromQueueSpec
1515
{
1616
use SqsExtension;
17+
use CreateSqsQueueTrait;
1718

1819
/**
1920
* @var SqsContext
2021
*/
2122
private $context;
2223

23-
/**
24-
* @var SqsDestination
25-
*/
26-
private $queue;
27-
2824
protected function tearDown()
2925
{
3026
parent::tearDown();
@@ -34,26 +30,13 @@ protected function tearDown()
3430
}
3531
}
3632

37-
/**
38-
* {@inheritdoc}
39-
*/
40-
protected function createContext()
33+
protected function createContext(): SqsContext
4134
{
4235
return $this->context = $this->buildSqsContext();
4336
}
4437

45-
/**
46-
* {@inheritdoc}
47-
*
48-
* @param SqsContext $context
49-
*/
50-
protected function createQueue(Context $context, $queueName)
38+
protected function createQueue(Context $context, $queueName): SqsDestination
5139
{
52-
$queueName = $queueName.time();
53-
54-
$this->queue = $context->createQueue($queueName);
55-
$context->declareQueue($this->queue);
56-
57-
return $this->queue;
40+
return $this->createSqsQueue($context, $queueName);
5841
}
5942
}

Diff for: pkg/sqs/Tests/Spec/SqsSendToAndReceiveFromTopicTest.php

+4-21
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,13 @@ class SqsSendToAndReceiveFromTopicTest extends SendToAndReceiveFromTopicSpec
1717
{
1818
use SqsExtension;
1919
use RetryTrait;
20+
use CreateSqsQueueTrait;
2021

2122
/**
2223
* @var SqsContext
2324
*/
2425
private $context;
2526

26-
/**
27-
* @var SqsDestination
28-
*/
29-
private $queue;
30-
3127
protected function tearDown()
3228
{
3329
parent::tearDown();
@@ -37,26 +33,13 @@ protected function tearDown()
3733
}
3834
}
3935

40-
/**
41-
* {@inheritdoc}
42-
*/
43-
protected function createContext()
36+
protected function createContext(): SqsContext
4437
{
4538
return $this->context = $this->buildSqsContext();
4639
}
4740

48-
/**
49-
* {@inheritdoc}
50-
*
51-
* @param SqsContext $context
52-
*/
53-
protected function createTopic(Context $context, $topicName)
41+
protected function createTopic(Context $context, $queueName): SqsDestination
5442
{
55-
$topicName = $topicName.time();
56-
57-
$this->queue = $context->createTopic($topicName);
58-
$context->declareQueue($this->queue);
59-
60-
return $this->queue;
43+
return $this->createSqsQueue($context, $queueName);
6144
}
6245
}

Diff for: pkg/sqs/Tests/Spec/SqsSendToAndReceiveNoWaitFromQueueTest.php

+25-7
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,41 @@
22

33
namespace Enqueue\Sqs\Tests\Spec;
44

5+
use Enqueue\Sqs\SqsContext;
6+
use Enqueue\Sqs\SqsDestination;
7+
use Enqueue\Test\SqsExtension;
8+
use Interop\Queue\Context;
59
use Interop\Queue\Spec\SendToAndReceiveNoWaitFromQueueSpec;
610

711
/**
812
* @group functional
913
*/
1014
class SqsSendToAndReceiveNoWaitFromQueueTest extends SendToAndReceiveNoWaitFromQueueSpec
1115
{
12-
public function test()
13-
{
14-
$this->markTestSkipped('The test is fragile. This is how SQS.');
15-
}
16+
use SqsExtension;
17+
use CreateSqsQueueTrait;
1618

1719
/**
18-
* {@inheritdoc}
20+
* @var SqsContext
1921
*/
20-
protected function createContext()
22+
private $context;
23+
24+
protected function tearDown()
25+
{
26+
parent::tearDown();
27+
28+
if ($this->context && $this->queue) {
29+
$this->context->deleteQueue($this->queue);
30+
}
31+
}
32+
33+
protected function createContext(): SqsContext
34+
{
35+
return $this->context = $this->buildSqsContext();
36+
}
37+
38+
protected function createQueue(Context $context, $queueName): SqsDestination
2139
{
22-
throw new \LogicException('Should not be ever called');
40+
return $this->createSqsQueue($context, $queueName);
2341
}
2442
}

Diff for: pkg/sqs/Tests/Spec/SqsSendToAndReceiveNoWaitFromTopicTest.php

+25-7
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,41 @@
22

33
namespace Enqueue\Sqs\Tests\Spec;
44

5+
use Enqueue\Sqs\SqsContext;
6+
use Enqueue\Sqs\SqsDestination;
7+
use Enqueue\Test\SqsExtension;
8+
use Interop\Queue\Context;
59
use Interop\Queue\Spec\SendToAndReceiveNoWaitFromTopicSpec;
610

711
/**
812
* @group functional
913
*/
1014
class SqsSendToAndReceiveNoWaitFromTopicTest extends SendToAndReceiveNoWaitFromTopicSpec
1115
{
12-
public function test()
13-
{
14-
$this->markTestSkipped('The test is fragile. This is how SQS.');
15-
}
16+
use SqsExtension;
17+
use CreateSqsQueueTrait;
1618

1719
/**
18-
* {@inheritdoc}
20+
* @var SqsContext
1921
*/
20-
protected function createContext()
22+
private $context;
23+
24+
protected function tearDown()
25+
{
26+
parent::tearDown();
27+
28+
if ($this->context && $this->queue) {
29+
$this->context->deleteQueue($this->queue);
30+
}
31+
}
32+
33+
protected function createContext(): SqsContext
34+
{
35+
return $this->context = $this->buildSqsContext();
36+
}
37+
38+
protected function createTopic(Context $context, $queueName): SqsDestination
2139
{
22-
throw new \LogicException('Should not be ever called');
40+
return $this->createSqsQueue($context, $queueName);
2341
}
2442
}

0 commit comments

Comments
 (0)