Skip to content

Commit

Permalink
Add ability to set multiple regionIDs and mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
charrondev committed Sep 14, 2023
1 parent 4ab9614 commit 06e1dd4
Show file tree
Hide file tree
Showing 14 changed files with 357 additions and 42 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Contracts\Cache\CacheInterface;

$orchHttpClient = new OrchHttpClient("https://orch.vanilla.localhost", "access-token-here");
$siteProvider = new OrchSiteProvider($orchHttpClient, OrchCluster::REGION_AMS1_PROD1);
$siteProvider = new OrchSiteProvider($orchHttpClient, [OrchCluster::REGION_AMS1_PROD1]);

// It is highly recommended to set a user-agent for network requests.
$siteProvider->setUserAgent("my-service:1.0");
Expand All @@ -71,7 +71,7 @@ $cache = new MemcachedAdapter(/** Configuration here. */);
$siteProvider->setCache($cache);

# Region can be changed later
$siteProvider->setRegionID(OrchCluster::REGION_YUL1_PROD1);
$siteProvider->setRegionIDs([OrchCluster::REGION_YUL1_PROD1, OrchCluster::REGION_AMS1_PROD1]);
```

The orchestration provider needs to be configured with an authenticated `OrchHttpClient` and a region/network to load sites from.
Expand Down Expand Up @@ -117,8 +117,7 @@ function doSomethingWithCluster(Cluster $cluster)
{
// A few getters
$clusterID = $cluster->getClusterID();
$region = $cluster->getRegion();
$network = $cluster->getNetwork();
$regionID = $cluster->getRegionID();
}

function doSomethingWithSite(Site $site)
Expand Down
3 changes: 3 additions & 0 deletions src/Cluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ class Cluster
public const REGION_AMS1_PROD1 = "ams1-prod1";
public const REGION_SJC1_PROD1 = "sjc1-prod1";
public const REGION_LOCALHOST = "localhost";
public const REGION_MOCK = "mock";

public const VALID_REGIONS = [
self::REGION_YUL1_DEV1,
self::REGION_YUL1_PROD1,
self::REGION_AMS1_PROD1,
self::REGION_SJC1_PROD1,
self::REGION_LOCALHOST,
self::REGION_MOCK,
];

private string $clusterID;
Expand Down
11 changes: 3 additions & 8 deletions src/Local/LocalSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,13 @@ class LocalSite extends Site
* @param string $configPath
* @param SiteRecord $siteRecord
* @param LocalSiteProvider $siteProvider
* @param HttpHandlerInterface $httpHandler
*
* @psalm-suppress InvalidArgument
*/
public function __construct(
string $configPath,
SiteRecord $siteRecord,
LocalSiteProvider $siteProvider,
HttpHandlerInterface $httpHandler
) {
public function __construct(string $configPath, SiteRecord $siteRecord, LocalSiteProvider $siteProvider)
{
$this->configPath = $configPath;
parent::__construct($siteRecord, $siteProvider, $httpHandler);
parent::__construct($siteRecord, $siteProvider);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Local/LocalSiteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Garden\Sites\Local;

use Garden\Http\CurlHandler;
use Garden\Sites\Cluster;
use Garden\Sites\Exceptions\ConfigLoadingException;
use Garden\Sites\SiteProvider;
use Garden\Sites\SiteRecord;
Expand All @@ -31,7 +32,7 @@ class LocalSiteProvider extends SiteProvider
*/
public function __construct(string $siteConfigFsBasePath)
{
parent::__construct("localhost");
parent::__construct([Cluster::REGION_LOCALHOST]);
$this->siteConfigFsBasePath = $siteConfigFsBasePath;
}

Expand Down Expand Up @@ -95,7 +96,7 @@ public function getSite(int $siteID): LocalSite

$configPath = $siteRecord->getExtra("configPath") ?? "";

$localSite = new LocalSite($this->siteConfigFsBasePath . $configPath, $siteRecord, $this, new CurlHandler());
$localSite = new LocalSite($this->siteConfigFsBasePath . $configPath, $siteRecord, $this);
return $localSite;
}

Expand Down
23 changes: 23 additions & 0 deletions src/Mock/MockCluster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* @copyright 2009-2023 Vanilla Forums Inc.
* @license Proprietary
*/

namespace Garden\Sites\Mock;

use Garden\Sites\Cluster;

/**
* Cluster for usage in mocks.
*/
class MockCluster extends Cluster
{
/**
* @param string $clusterID
*/
public function __construct(string $clusterID)
{
parent::__construct($clusterID, Cluster::REGION_MOCK);
}
}
181 changes: 181 additions & 0 deletions src/Mock/MockSite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php
/**
* @copyright 2009-2023 Vanilla Forums Inc.
* @license Proprietary
*/

namespace Garden\Sites\Mock;

use Garden\Sites\Local\LocalCluster;
use Garden\Sites\Site;
use Garden\Sites\SiteRecord;
use Garden\Utils\ArrayUtils;

/**
* A mock local site for testing purposes.
*
* @extends Site<MockSite, LocalCluster>
*/
class MockSite extends Site
{
public const MOCK_CLUSTER_ID = "cl00000";

/** @var int */
public int $maxJobConcurrency = 25;

/** @var string */
protected string $baseUrl;

/** @var array<array-key, mixed>|\ArrayAccess */
protected $config;

/**
* Constructor.
*
* @param string $baseUrl
* @param int $siteID
* @param array $config
* @param string $clusterID
* @param MockSiteProvider|null $mockSiteProvider
*/
public function __construct(
string $baseUrl,
int $siteID = MockSiteProvider::MOCK_SITE_ID,
array $config = [],
string $clusterID = self::MOCK_CLUSTER_ID,
MockSiteProvider $mockSiteProvider = null
) {
$this->baseUrl = $baseUrl;
$this->config = $config;
$siteRecord = new SiteRecord($siteID, 0, $clusterID, $baseUrl);
parent::__construct($siteRecord, new MockSiteProvider());
$this->generateKey();
$this->setSystemToken("test123");
$this->setConfigs([
"Vanilla.AccountID" => 1,
"queue.disableFeedback" => true,
]);
}

/**
* @param MockSiteProvider $mockSiteProvider
* @return void
*/
public function setSiteProvider(MockSiteProvider $mockSiteProvider): void
{
$this->siteProvider = $mockSiteProvider;
}

/**
* @return SiteRecord
*/
public function getSiteRecord(): SiteRecord
{
return $this->siteRecord;
}

/**
* @inheritDoc
*/
protected function loadSiteConfig(): array
{
return $this->config;
}

/**
* Set config values for the site.
*
* @param array<string, mixed> $configs
*
* @return void
*/
public function setConfigs(array $configs): void
{
foreach ($configs as $key => $val) {
ArrayUtils::setByPath($key, $this->config, $val);
}
$this->clearConfigCache();
}

/**
* Generate a random private key.
*/
public function generateKey(): void
{
$privateKey = bin2hex(random_bytes(32));
$this->setPrivateKey($privateKey);
}

/**
* Set the config private key.
*
* @param string|null $key
*/
public function setPrivateKey(?string $key): void
{
$this->config["VanillaQueue"]["Keys"]["Private"] = $key;
}

/**
* Set a value for the Garden.Scheduler.Token config field.
*
* @param string|null $value
* @return void
*/
public function setSchedulerToken(?string $value): void
{
$this->config["Garden"]["Scheduler"]["Token"] = $value;
}

/**
* Set Api Key
*
* @param string|null $key
* @return void
*/
public function setSystemToken(?string $key): void
{
ArrayUtils::setByPath(self::CONF_SYSTEM_ACCESS_TOKEN, $this->config, $key);
}

/**
* Set Site Account id
*
* @param int $accountID
* @return void
*/
public function setAccountID(int $accountID): void
{
$this->config["Vanilla"]["AccountID"] = $accountID;
}

/**
* Set Elastic Secret
*
* @param ?string $secret
* @return void
*/
public function setElasticSecret(?string $secret): void
{
$this->config["ElasticDev"]["Secret"] = $secret;
}

/**
* Set the site Plugins.
*
* @param array $plugins
* @return void
*/
public function setPlugins(array $plugins): void
{
$this->config["Plugins"] = $plugins;
}

/**
* @return string
*/
public function getSearchServiceBaseUrl(): string
{
return "https://fake-search-service.test";
}
}
86 changes: 86 additions & 0 deletions src/Mock/MockSiteProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* @copyright 2009-2023 Vanilla Forums Inc.
* @license Proprietary
*/

namespace Garden\Sites\Mock;

use Garden\Sites\Cluster;
use Garden\Sites\Exceptions\SiteNotFoundException;
use Garden\Sites\Local\LocalCluster;
use Garden\Sites\Site;
use Garden\Sites\SiteProvider;
use Symfony\Component\Cache\Adapter\NullAdapter;

/**
* A Mock LocalSiteProvider for testing purposes.
*
* @extends SiteProvider<MockSite, LocalCluster>
*/
class MockSiteProvider extends SiteProvider
{
const MOCK_SITE_ID = 123;

/** @var array<int, MockSite> */
private array $mockSites = [];

/**
* Constructor.
*
* @param MockSite ...$mockSites One or more mock sites.
*/
public function __construct(MockSite ...$mockSites)
{
foreach ($mockSites as $mockLocalSite) {
$this->mockSites[$mockLocalSite->getSiteID()] = $mockLocalSite;
}
parent::__construct([Cluster::REGION_MOCK]);
$this->setCache(new NullAdapter());
}

/**
* Add a site.
*
* @param MockSite $mockSite
* @return void
*/
public function addSite(MockSite $mockSite): void
{
$mockSite->setSiteProvider($this);
$this->mockSites[$mockSite->getSiteID()] = $mockSite;
}

/**
* @inheritDoc
*/
protected function loadAllSiteRecords(): array
{
$siteRecords = [];
foreach ($this->mockSites as $siteID => $site) {
$siteRecords[$siteID] = $site->getSiteRecord();
}

return $siteRecords;
}

/**
* @inheritDoc
*/
public function getSite(int $siteID): Site
{
$site = $this->mockSites[$siteID] ?? null;
if ($site === null) {
throw new SiteNotFoundException($siteID);
}
return $site;
}

/**
* @inheritDoc
*/
protected function loadAllClusters(): array
{
return [MockSite::MOCK_CLUSTER_ID => new LocalCluster(MockSite::MOCK_CLUSTER_ID)];
}
}
Loading

0 comments on commit 06e1dd4

Please sign in to comment.