-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from gowrizrh/cloud-sinks
Add AWS SQS and GCP Pub/Sub sinks
- Loading branch information
Showing
21 changed files
with
1,619 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MageOS\AsyncEventsAWS\Model; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
|
||
class SQSConfig | ||
{ | ||
private const XML_PATH_AWS_ACCESS_KEY = 'async_events_aws/sqs/access_key'; | ||
private const XML_PATH_AWS_SECRET_ACCESS_KEY = 'async_events_aws/sqs/secret_access_key'; | ||
private const XML_PATH_AWS_REGION = 'async_events_aws/sqs/region'; | ||
|
||
/** | ||
* @param ScopeConfigInterface $scopeConfig | ||
*/ | ||
public function __construct( | ||
private readonly ScopeConfigInterface $scopeConfig | ||
) { | ||
} | ||
|
||
/** | ||
* Get Access Key | ||
* | ||
* @return string|null | ||
*/ | ||
public function getAccessKey(): ?string | ||
{ | ||
return $this->scopeConfig->getValue(self::XML_PATH_AWS_ACCESS_KEY); | ||
} | ||
|
||
/** | ||
* Get Secret Access Key | ||
* | ||
* @return string|null | ||
*/ | ||
public function getSecretAccessKey(): ?string | ||
{ | ||
return $this->scopeConfig->getValue(self::XML_PATH_AWS_SECRET_ACCESS_KEY); | ||
} | ||
|
||
/** | ||
* Get AWS region | ||
* | ||
* @return string|null | ||
*/ | ||
public function getRegion(): ?string | ||
{ | ||
return $this->scopeConfig->getValue(self::XML_PATH_AWS_REGION); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MageOS\AsyncEventsAWS\Service; | ||
|
||
use Aws\Exception\AwsException; | ||
use Aws\Sqs\SqsClient; | ||
use CloudEvents\Serializers\JsonSerializer; | ||
use CloudEvents\Serializers\Normalizers\V1\Normalizer; | ||
use CloudEvents\V1\CloudEventImmutable; | ||
use Exception; | ||
use Magento\Framework\Encryption\EncryptorInterface; | ||
use Magento\Framework\Serialize\SerializerInterface; | ||
use MageOS\AsyncEvents\Api\Data\AsyncEventInterface; | ||
use MageOS\AsyncEvents\Api\Data\ResultInterface; | ||
use MageOS\AsyncEvents\Helper\NotifierResult; | ||
use MageOS\AsyncEvents\Helper\NotifierResultFactory; | ||
use MageOS\AsyncEvents\Service\AsyncEvent\NotifierInterface; | ||
use MageOS\AsyncEventsAWS\Model\SQSConfig; | ||
|
||
class SQS implements NotifierInterface | ||
{ | ||
/** @var SqsClient|null */ | ||
private ?SqsClient $sqsClient = null; | ||
|
||
/** | ||
* @param NotifierResultFactory $notifierResultFactory | ||
* @param EncryptorInterface $encryptor | ||
* @param SerializerInterface $serializer | ||
* @param Normalizer $normalizer | ||
* @param SQSConfig $config | ||
*/ | ||
public function __construct( | ||
private readonly NotifierResultFactory $notifierResultFactory, | ||
private readonly EncryptorInterface $encryptor, | ||
private readonly SerializerInterface $serializer, | ||
private readonly Normalizer $normalizer, | ||
private readonly SQSConfig $config | ||
) { | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function notify(AsyncEventInterface $asyncEvent, CloudEventImmutable $event): ResultInterface | ||
{ | ||
/** @var NotifierResult $result */ | ||
$result = $this->notifierResultFactory->create(); | ||
$result->setSubscriptionId($asyncEvent->getSubscriptionId()); | ||
$result->setAsyncEventData($this->normalizer->normalize($event, false)); | ||
$result->setIsSuccessful(false); | ||
$result->setIsRetryable(false); | ||
|
||
$params = [ | ||
'MessageBody' => JsonSerializer::create()->serializeStructured($event), | ||
'QueueUrl' => $asyncEvent->getRecipientUrl() | ||
]; | ||
|
||
try { | ||
$client = $this->getClient(); | ||
|
||
if (!$client) { | ||
$result->setResponseData('SQS connection is not configured.'); | ||
|
||
return $result; | ||
} | ||
|
||
$sqsResult = $client->sendMessage($params); | ||
$result->setResponseData( | ||
$this->serializer->serialize([ | ||
'MessageId' => $sqsResult->get('MessageId'), | ||
]) | ||
); | ||
$result->setIsSuccessful(true); | ||
|
||
} catch (AwsException $awsException) { | ||
$code = $awsException->getAwsErrorCode(); | ||
|
||
// The PHP SDK doesn't throw named exceptions based on error codes, however the official go sdk maps the | ||
// service error codes to the internal SQS error codes. | ||
// https://github.com/aws/aws-sdk-go/blob/main/service/sqs/errors.go | ||
$retryable = match ($code) { | ||
'RequestThrottled', 'KmsDisabled', 'KmsThrottled' => true, | ||
default => false, | ||
}; | ||
$result->setIsRetryable($retryable); | ||
|
||
$result->setResponseData($this->serializer->serialize([ | ||
'code' => $awsException->getAwsErrorCode(), | ||
'message' => $awsException->getMessage() | ||
])); | ||
|
||
} catch (Exception $exception) { | ||
$result->setResponseData( | ||
$exception->getMessage() | ||
); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Instantiate and return SqsClient | ||
* | ||
* @return SqsClient|null | ||
*/ | ||
private function getClient(): ?SqsClient | ||
{ | ||
if ($this->sqsClient === null) { | ||
$region = $this->config->getRegion(); | ||
$key = $this->config->getAccessKey(); | ||
$secret = $this->config->getSecretAccessKey(); | ||
|
||
if ($key !== null && $secret !== null) { | ||
|
||
$this->sqsClient = new SqsClient([ | ||
'region' => $region, | ||
'credentials' => [ | ||
'key' => $key, | ||
'secret' => $this->encryptor->decrypt($secret) | ||
] | ||
]); | ||
} | ||
} | ||
|
||
return $this->sqsClient; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.