Skip to content

[sqs] Use a queue created in another AWS account. #662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions pkg/sqs/SqsConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ class SqsConnectionFactory implements ConnectionFactory

/**
* $config = [
* 'key' => null - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
* 'secret' => null, - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
* 'token' => null, - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
* '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.
* 'retries' => 3, - (int, default=int(3)) Configures the maximum number of allowed retries for a client (pass 0 to disable retries).
* 'version' => '2012-11-05', - (string, required) The version of the webservice to utilize
* 'lazy' => true, - Enable lazy connection (boolean)
* 'endpoint' => null - (string, default=null) The full URI of the webservice. This is only required when connecting to a custom endpoint e.g. localstack
* 'key' => null AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
* 'secret' => null, AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
* 'token' => null, AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
* '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.
* 'retries' => 3, (int, default=int(3)) Configures the maximum number of allowed retries for a client (pass 0 to disable retries).
* 'version' => '2012-11-05', (string, required) The version of the webservice to utilize
* 'lazy' => true, Enable lazy connection (boolean)
* 'endpoint' => null (string, default=null) The full URI of the webservice. This is only required when connecting to a custom endpoint e.g. localstack
* 'queue_owner_aws_account_id' The AWS account ID of the account that created the queue.
* ].
*
* or
Expand Down Expand Up @@ -74,10 +75,10 @@ public function createContext(): Context
if ($this->config['lazy']) {
return new SqsContext(function () {
return $this->establishConnection();
});
}, $this->config);
}

return new SqsContext($this->establishConnection());
return new SqsContext($this->establishConnection(), $this->config);
}

private function establishConnection(): SqsClient
Expand Down Expand Up @@ -132,6 +133,7 @@ private function parseDsn(string $dsn): array
'version' => $dsn->getString('version'),
'lazy' => $dsn->getBool('lazy'),
'endpoint' => $dsn->getString('endpoint'),
'queue_owner_aws_account_id' => $dsn->getString('queue_owner_aws_account_id'),
]), function ($value) { return null !== $value; });
}

Expand All @@ -146,6 +148,7 @@ private function defaultConfig(): array
'version' => '2012-11-05',
'lazy' => true,
'endpoint' => null,
'queue_owner_aws_account_id' => null,
];
}
}
17 changes: 12 additions & 5 deletions pkg/sqs/SqsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ class SqsContext implements Context
*/
private $queueUrls;

private $config;

/**
* Callable must return instance of SqsClient once called.
*
* @param SqsClient|callable $client
*/
public function __construct($client)
public function __construct($client, array $config)
{
if ($client instanceof SqsClient) {
$this->client = $client;
Expand All @@ -52,6 +54,8 @@ public function __construct($client)
SqsClient::class
));
}

$this->config = $config;
}

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

$result = $this->getClient()->getQueueUrl([
'QueueName' => $destination->getQueueName(),
]);
$arguments = ['QueueName' => $destination->getQueueName()];
if (false == empty($this->config['queue_owner_aws_account_id'])) {
$arguments['QueueOwnerAWSAccountId'] = $this->config['queue_owner_aws_account_id'];
}

$result = $this->getClient()->getQueueUrl($arguments);

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

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

public function declareQueue(SqsDestination $dest): void
Expand Down
21 changes: 21 additions & 0 deletions pkg/sqs/Tests/Spec/CreateSqsQueueTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Enqueue\Sqs\Tests\Spec;

use Enqueue\Sqs\SqsContext;
use Enqueue\Sqs\SqsDestination;

trait CreateSqsQueueTrait
{
private $queue;

protected function createSqsQueue(SqsContext $context, string $queueName): SqsDestination
{
$queueName = $queueName.time();

$this->queue = $context->createQueue($queueName);
$context->declareQueue($this->queue);

return $this->queue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ class SqsSendAndReceiveDelayedMessageFromQueueTest extends SendAndReceiveDelayed
{
use RetryTrait;
use SqsExtension;
use CreateSqsQueueTrait;

/**
* @var SqsContext
*/
private $context;

/**
* @var SqsDestination
*/
private $queue;

protected function tearDown()
{
parent::tearDown();
Expand All @@ -37,26 +33,13 @@ protected function tearDown()
}
}

/**
* {@inheritdoc}
*/
protected function createContext()
protected function createContext(): SqsContext
{
return $this->context = $this->buildSqsContext();
}

/**
* {@inheritdoc}
*
* @param SqsContext $context
*/
protected function createQueue(Context $context, $queueName)
protected function createQueue(Context $context, $queueName): SqsDestination
{
$queueName = $queueName.time();

$this->queue = $context->createQueue($queueName);
$context->declareQueue($this->queue);

return $this->queue;
return $this->createSqsQueue($context, $queueName);
}
}
25 changes: 4 additions & 21 deletions pkg/sqs/Tests/Spec/SqsSendToAndReceiveFromQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@
class SqsSendToAndReceiveFromQueueTest extends SendToAndReceiveFromQueueSpec
{
use SqsExtension;
use CreateSqsQueueTrait;

/**
* @var SqsContext
*/
private $context;

/**
* @var SqsDestination
*/
private $queue;

protected function tearDown()
{
parent::tearDown();
Expand All @@ -34,26 +30,13 @@ protected function tearDown()
}
}

/**
* {@inheritdoc}
*/
protected function createContext()
protected function createContext(): SqsContext
{
return $this->context = $this->buildSqsContext();
}

/**
* {@inheritdoc}
*
* @param SqsContext $context
*/
protected function createQueue(Context $context, $queueName)
protected function createQueue(Context $context, $queueName): SqsDestination
{
$queueName = $queueName.time();

$this->queue = $context->createQueue($queueName);
$context->declareQueue($this->queue);

return $this->queue;
return $this->createSqsQueue($context, $queueName);
}
}
25 changes: 4 additions & 21 deletions pkg/sqs/Tests/Spec/SqsSendToAndReceiveFromTopicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ class SqsSendToAndReceiveFromTopicTest extends SendToAndReceiveFromTopicSpec
{
use SqsExtension;
use RetryTrait;
use CreateSqsQueueTrait;

/**
* @var SqsContext
*/
private $context;

/**
* @var SqsDestination
*/
private $queue;

protected function tearDown()
{
parent::tearDown();
Expand All @@ -37,26 +33,13 @@ protected function tearDown()
}
}

/**
* {@inheritdoc}
*/
protected function createContext()
protected function createContext(): SqsContext
{
return $this->context = $this->buildSqsContext();
}

/**
* {@inheritdoc}
*
* @param SqsContext $context
*/
protected function createTopic(Context $context, $topicName)
protected function createTopic(Context $context, $queueName): SqsDestination
{
$topicName = $topicName.time();

$this->queue = $context->createTopic($topicName);
$context->declareQueue($this->queue);

return $this->queue;
return $this->createSqsQueue($context, $queueName);
}
}
32 changes: 25 additions & 7 deletions pkg/sqs/Tests/Spec/SqsSendToAndReceiveNoWaitFromQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,41 @@

namespace Enqueue\Sqs\Tests\Spec;

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

/**
* @group functional
*/
class SqsSendToAndReceiveNoWaitFromQueueTest extends SendToAndReceiveNoWaitFromQueueSpec
{
public function test()
{
$this->markTestSkipped('The test is fragile. This is how SQS.');
}
use SqsExtension;
use CreateSqsQueueTrait;

/**
* {@inheritdoc}
* @var SqsContext
*/
protected function createContext()
private $context;

protected function tearDown()
{
parent::tearDown();

if ($this->context && $this->queue) {
$this->context->deleteQueue($this->queue);
}
}

protected function createContext(): SqsContext
{
return $this->context = $this->buildSqsContext();
}

protected function createQueue(Context $context, $queueName): SqsDestination
{
throw new \LogicException('Should not be ever called');
return $this->createSqsQueue($context, $queueName);
}
}
32 changes: 25 additions & 7 deletions pkg/sqs/Tests/Spec/SqsSendToAndReceiveNoWaitFromTopicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,41 @@

namespace Enqueue\Sqs\Tests\Spec;

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

/**
* @group functional
*/
class SqsSendToAndReceiveNoWaitFromTopicTest extends SendToAndReceiveNoWaitFromTopicSpec
{
public function test()
{
$this->markTestSkipped('The test is fragile. This is how SQS.');
}
use SqsExtension;
use CreateSqsQueueTrait;

/**
* {@inheritdoc}
* @var SqsContext
*/
protected function createContext()
private $context;

protected function tearDown()
{
parent::tearDown();

if ($this->context && $this->queue) {
$this->context->deleteQueue($this->queue);
}
}

protected function createContext(): SqsContext
{
return $this->context = $this->buildSqsContext();
}

protected function createTopic(Context $context, $queueName): SqsDestination
{
throw new \LogicException('Should not be ever called');
return $this->createSqsQueue($context, $queueName);
}
}
Loading