Skip to content
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

[sns] added possibility to define already existing topics (prevent create topic call) #1022 #1147

Merged
merged 7 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
30 changes: 28 additions & 2 deletions pkg/sns/SnsConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ class SnsConnectionFactory implements ConnectionFactory

/**
* $config = [
* 'key' => null AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
* '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.
* '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
* 'endpoint' => null, (string, default=null) The full URI of the webservice. This is only required when connecting to a custom endpoint e.g. localstack
* 'topic_arns' => [], (array<string,string>) The list of existing topic arns
* ].
*
* or
Expand Down Expand Up @@ -59,6 +60,9 @@ public function __construct($config = 'sns:')

unset($config['dsn']);
}
if (\array_key_exists('topic_arns', $config) && \is_string($config['topic_arns'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at this stage it has to be assoc array already. The string parsing should happen if dsn is passed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed, not needed when using dsn parsing

$config['topic_arns'] = $this->extractTopicArns($config['topic_arns']);
}
} else {
throw new \LogicException(\sprintf('The config must be either an array of options, a DSN string, null or instance of %s', AwsSnsClient::class));
}
Expand Down Expand Up @@ -132,9 +136,30 @@ private function parseDsn(string $dsn): array
'version' => $dsn->getString('version'),
'lazy' => $dsn->getBool('lazy'),
'endpoint' => $dsn->getString('endpoint'),
'topic_arns' => $this->extractTopicArns($dsn->getString('topic_arns')),
]), function ($value) { return null !== $value; });
}

private function extractTopicArns(?string $topicArns): array
{
if (!$topicArns) {
return [];
}

return array_column(
array_map(function ($topic) {
list($name, $arn) = explode('|', $topic);

return [
'name' => $name,
'arn' => $arn,
];
}, explode(';', $topicArns)),
'arn',
'name'
);
}

private function defaultConfig(): array
{
return [
Expand All @@ -145,6 +170,7 @@ private function defaultConfig(): array
'version' => '2010-03-31',
'lazy' => true,
'endpoint' => null,
'topic_arns' => [],
];
}
}
3 changes: 1 addition & 2 deletions pkg/sns/SnsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public function __construct(SnsClient $client, array $config)
{
$this->client = $client;
$this->config = $config;

$this->topicArns = [];
$this->topicArns = $config['topic_arns'];
makasim marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
24 changes: 24 additions & 0 deletions pkg/sns/Tests/SnsConnectionFactoryConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public static function provideConfigs()
'version' => '2010-03-31',
'lazy' => true,
'endpoint' => null,
'topic_arns' => [],
],
];

Expand All @@ -77,6 +78,7 @@ public static function provideConfigs()
'version' => '2010-03-31',
'lazy' => true,
'endpoint' => null,
'topic_arns' => [],
],
];

Expand All @@ -90,6 +92,7 @@ public static function provideConfigs()
'version' => '2010-03-31',
'lazy' => true,
'endpoint' => null,
'topic_arns' => [],
],
];

Expand All @@ -103,6 +106,7 @@ public static function provideConfigs()
'version' => '2010-03-31',
'lazy' => false,
'endpoint' => null,
'topic_arns' => [],
],
];

Expand All @@ -116,6 +120,7 @@ public static function provideConfigs()
'version' => '2010-03-31',
'lazy' => false,
'endpoint' => null,
'topic_arns' => [],
],
];

Expand All @@ -129,6 +134,7 @@ public static function provideConfigs()
'version' => '2010-03-31',
'lazy' => false,
'endpoint' => null,
'topic_arns' => [],
],
];

Expand All @@ -148,6 +154,24 @@ public static function provideConfigs()
'version' => '2010-03-31',
'lazy' => false,
'endpoint' => 'http://localstack:1111',
'topic_arns' => [],
],
];

yield [
['dsn' => 'sns:?topic_arns=topic1|arn:aws:sns:us-east-1:123456789012:topic1;topic2|arn:aws:sns:us-west-2:123456789012:topic2'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about passing arns as

topic_arns[topic1]=arn:aws:sns:us-east-1:123456789012&topic_arns[topic2]=arn:aws:sns:us-west-2:123456789012:topic2

that way the standard query parsing function does the job

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, replaced

[
'key' => null,
'secret' => null,
'token' => null,
'region' => null,
'version' => '2010-03-31',
'lazy' => true,
'endpoint' => null,
'topic_arns' => [
'topic1' => 'arn:aws:sns:us-east-1:123456789012:topic1',
'topic2' => 'arn:aws:sns:us-west-2:123456789012:topic2',
],
],
];
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/sns/Tests/SnsConnectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function testCouldBeConstructedWithEmptyConfiguration()
'region' => null,
'version' => '2010-03-31',
'endpoint' => null,
'topic_arns' => [],
], 'config', $factory);
}

Expand All @@ -48,6 +49,7 @@ public function testCouldBeConstructedWithCustomConfiguration()
'region' => null,
'version' => '2010-03-31',
'endpoint' => null,
'topic_arns' => [],
], 'config', $factory);
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sns/Tests/Spec/SnsContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ protected function createContext()
{
$client = $this->createMock(SnsClient::class);

return new SnsContext($client, []);
return new SnsContext($client, ['topic_arns' => []]);
}
}