Skip to content

Commit

Permalink
Merge pull request #1 from gowrizrh/cloud-sinks
Browse files Browse the repository at this point in the history
Setup package deployment workflow
  • Loading branch information
Vinai authored May 9, 2024
2 parents f389454 + 0da1e3f commit 6d00c52
Show file tree
Hide file tree
Showing 24 changed files with 1,574 additions and 0 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/split.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: 'Packages Split'

on:
push:
branches:
- main
tags:
- '*'

env:
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}

jobs:
packages_split:
runs-on: ubuntu-latest
name: ${{ matrix.package.local_path }}
strategy:
fail-fast: false
matrix:
package:
- local_path: 'AsyncEventsAWS'
split_repository: 'mageos-async-events-aws'

# - local_path: 'AsyncEventsAzure'
# split_repository: 'mageos-async-events-azure'

# - local_path: 'AsyncEventsGCP'
# split_repository: 'mageos-async-events-gcp'

steps:
- uses: actions/checkout@v4

- if: "!startsWith(github.ref, 'refs/tags/')"
uses: "symplify/[email protected]"
with:
package_directory: 'MageOS/${{ matrix.package.local_path }}'
repository_organization: 'mage-os'
repository_name: '${{ matrix.package.split_repository }}'
user_name: "mage-os-ci"
user_email: "[email protected]"

- if: "startsWith(github.ref, 'refs/tags/')"
uses: "symplify/[email protected]"
with:
tag: ${GITHUB_REF#refs/tags/}
package_directory: 'MageOS/${{ matrix.package.local_path }}'
repository_organization: 'mage-os'
repository_name: '${{ matrix.package.split_repository }}'
user_name: "mage-os-ci"
user_email: "[email protected]"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
1 change: 1 addition & 0 deletions MageOS/AsyncEventsAWS/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
57 changes: 57 additions & 0 deletions MageOS/AsyncEventsAWS/Model/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace MageOS\AsyncEventsAWS\Model;

use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;

class Config
{
const XML_PATH_AWS_ACCESS_KEY = 'async_events_aws/eventbridge/access_key';
const XML_PATH_AWS_SECRET_ACCESS_KEY = 'async_events_aws/eventbridge/secret_access_key';
const XML_PATH_AWS_REGION = 'async_events_aws/eventbridge/region';
const XML_PATH_EVENT_SOURCE = 'async_events_aws/eventbridge/source';

/**
* Config constructor.
*
* @param StoreManagerInterface $storeManager
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
private readonly StoreManagerInterface $storeManager,
private readonly ScopeConfigInterface $scopeConfig
) {
}

public function getAccessKey()
{
return $this->scopeConfig->getValue(self::XML_PATH_AWS_ACCESS_KEY, ScopeInterface::SCOPE_STORES);
}

public function getSecretAccessKey()
{
return $this->scopeConfig->getValue(self::XML_PATH_AWS_SECRET_ACCESS_KEY, ScopeInterface::SCOPE_STORES);
}

public function getRegion()
{
return $this->scopeConfig->getValue(self::XML_PATH_AWS_REGION, ScopeInterface::SCOPE_STORES);
}

public function getSource()
{
$source = $this->scopeConfig->getValue(self::XML_PATH_EVENT_SOURCE, ScopeInterface::SCOPE_STORES);
if ($source == null) {
$url = $this->storeManager->getStore()->getBaseUrl();
if ($url !== null) {
return parse_url($url, PHP_URL_HOST);
}
}

return $source;
}
}
1 change: 1 addition & 0 deletions MageOS/AsyncEventsAWS/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# MageOS Events integration for AWS
130 changes: 130 additions & 0 deletions MageOS/AsyncEventsAWS/Service/EventBridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

declare(strict_types=1);

namespace MageOS\AsyncEventsAWS\Service;

use Aws\EventBridge\EventBridgeClient;
use CloudEvents\Serializers\JsonSerializer;
use CloudEvents\Serializers\Normalizers\V1\Normalizer;
use CloudEvents\V1\CloudEventImmutable;
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\Service\AsyncEvent\NotifierInterface;
use MageOS\AsyncEventsAWS\Model\Config as EventBridgeConfig;

class EventBridge implements NotifierInterface
{
private ?EventBridgeClient $eventBridgeClient = null;

/**
* @param EventBridgeConfig $config
* @param Normalizer $normalizer
* @param EncryptorInterface $encryptor
* @param SerializerInterface $serializer
*/
public function __construct(
private readonly EventBridgeConfig $config,
private readonly Normalizer $normalizer,
private readonly EncryptorInterface $encryptor,
private readonly SerializerInterface $serializer,

) {
}

/**
* @inheritDoc
*/
public function notify(AsyncEventInterface $asyncEvent, CloudEventImmutable $event): ResultInterface
{
$result = new NotifierResult();
$result->setSubscriptionId($asyncEvent->getSubscriptionId());
$result->setAsyncEventData($this->normalizer->normalize($event, false));
$result->setIsRetryable(false);
$result->setIsSuccessful(false);

try {
$client = $this->getClient();

if (!$client) {
$result->setResponseData(
$this->serializer->serialize(__('EventBridge connection is not configured.'))
);

return $result;
}

$response = $client->putEvents([
'Entries' => [
[
'Source' => $this->config->getSource(),
'Detail' => JsonSerializer::create()->serializeStructured($event),
'DetailType' => $asyncEvent->getEventName(),
'EventBusName' => $asyncEvent->getRecipientUrl()
]
]
]);

if (isset($response['FailedEntryCount']) && $response['FailedEntryCount'] > 0) {
// As we are only ever submitting one event at a time, assume that only one result
// can be returned.
$entry = $result['Entries'][0];
$result->setResponseData(
$this->serializer->serialize($entry)
);

// Retryable error codes taken from
// https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutEventsResultEntry.html#API_PutEventsResultEntry_Contents
if ($entry['ErrorCode'] === 'InternalFailure' || $entry['ErrorCode'] === 'ThrottlingException') {
$result->setIsRetryable(true);
}
} else {
$result->setIsSuccessful(true);

$result->setResponseData(
$this->serializer->serialize($response)
);
}
} catch (\Exception $exception) {
$result->setResponseData(
$exception->getMessage()
);
}

return $result;
}

/**
* Instantiate and return EventBridge client
*
* @return EventBridgeClient|null
*/
private function getClient(): ?EventBridgeClient
{
if ($this->eventBridgeClient === null) {
$region = $this->config->getRegion();
$key = $this->config->getAccessKey();
$secret = $this->config->getSecretAccessKey();

if ($region === null || $key === null || $secret === null) {
$this->eventBridgeClient = null;
} else {
$this->eventBridgeClient = new EventBridgeClient(
[
'version' => '2015-10-07',
'region' => $region,
'credentials' => [
'key' => $key,
'secret' => $this->encryptor->decrypt($secret)
]
]
);
}
}

return $this->eventBridgeClient;
}
}
19 changes: 19 additions & 0 deletions MageOS/AsyncEventsAWS/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "mage-os/mageos-async-events-aws",
"require": {
"php": "^8.1",
"aws/aws-sdk-php": "^3.305",
"mage-os/mageos-async-events": "^4.0@beta"
},
"autoload": {
"psr-4": {
"MageOS\\AsyncEventsAWS\\": ""
}
},
"extra": {
"branch-alias": {
"dev-main": "4.x-dev"
}
},
"minimum-stability": "beta"
}
33 changes: 33 additions & 0 deletions MageOS/AsyncEventsAWS/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="async_events_aws" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Async Events AWS</label>
<tab>service</tab>
<resource>Magento_Webapi::config_webapi</resource>

<group id="eventbridge" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>EventBridge</label>
<field id="access_key" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Access Key</label>
</field>

<field id="secret_access_key" type="obscure" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Secret Access Key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>

<field id="region" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Region</label>
<comment><![CDATA[Default: 'us-east-1']]></comment>
</field>

<field id="source" type="text" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Event source name</label>
<comment><![CDATA[Default: store's host name. Example: store.com]]></comment>
</field>
</group>
</section>
</system>
</config>
10 changes: 10 additions & 0 deletions MageOS/AsyncEventsAWS/etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MageOS\AsyncEvents\Service\AsyncEvent\NotifierFactory">
<arguments>
<argument name="notifierClasses" xsi:type="array">
<item name="eventbridge" xsi:type="object">MageOS\AsyncEventsAWS\Service\EventBridge</item>
</argument>
</arguments>
</type>
</config>
9 changes: 9 additions & 0 deletions MageOS/AsyncEventsAWS/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="MageOS_AsyncEventsAWS">
<sequence>
<module name="MageOS_AsyncEvents"/>
</sequence>
</module>
</config>
7 changes: 7 additions & 0 deletions MageOS/AsyncEventsAWS/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'MageOS_AsyncEventsAWS', __DIR__);
1 change: 1 addition & 0 deletions MageOS/AsyncEventsAzure/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
1 change: 1 addition & 0 deletions MageOS/AsyncEventsAzure/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# MageOS Events integration for Azure
18 changes: 18 additions & 0 deletions MageOS/AsyncEventsAzure/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "mage-os/mageos-async-events-azure",
"require": {
"php": "^8.1",
"mage-os/mageos-async-events": "^4.0@beta"
},
"autoload": {
"psr-4": {
"MageOS\\AsyncEventsAzure\\": ""
}
},
"extra": {
"branch-alias": {
"dev-main": "4.x-dev"
}
},
"minimum-stability": "beta"
}
9 changes: 9 additions & 0 deletions MageOS/AsyncEventsAzure/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="MageOS_AsyncEventsAzure">
<sequence>
<module name="MageOS_AsyncEvents"/>
</sequence>
</module>
</config>
5 changes: 5 additions & 0 deletions MageOS/AsyncEventsAzure/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'MageOS_AsyncEventsAzure', __DIR__);
1 change: 1 addition & 0 deletions MageOS/AsyncEventsGCP/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
1 change: 1 addition & 0 deletions MageOS/AsyncEventsGCP/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# MageOS Events integration for GCP
20 changes: 20 additions & 0 deletions MageOS/AsyncEventsGCP/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "mage-os/mageos-async-events-gcp",
"require": {
"php": "^8.1",
"mage-os/mageos-async-events": "^4.0@beta",
"google/cloud-pubsub": "^2.1",
"google/cloud-eventarc-publishing": "^0.6.3"
},
"autoload": {
"psr-4": {
"MageOS\\AsyncEventsGCP\\": ""
}
},
"extra": {
"branch-alias": {
"dev-main": "4.x-dev"
}
},
"minimum-stability": "beta"
}
Loading

0 comments on commit 6d00c52

Please sign in to comment.