Skip to content

Commit

Permalink
ci: commit oat-sa/environment-management#
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Apr 22, 2022
1 parent e5284b1 commit 7a3892a
Show file tree
Hide file tree
Showing 40 changed files with 1,135 additions and 127 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
],
"require": {
"php": ">=7.4.0",
"ext-grpc": "*",
"lcobucci/jwt": "^4.1.5",
"oat-sa/lib-em-php-proto": "*",
"psr/http-message": "^1.0",
"oat-sa/lib-lti1p3-core": "*",
"psr/log": "^1.0 || ^2.0 || ^3.0"
},
"require-dev": {
Expand Down
4 changes: 2 additions & 2 deletions docs/tenant-id.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<?php

use OAT\Library\EnvironmentManagementClient\Http\TenantIdExtractorInterface;
use OAT\Library\EnvironmentManagementClient\Http\TenantIdHeaderExtractor;
use OAT\Library\EnvironmentManagementClient\Http\TenantIdExtractor;
use OAT\Library\EnvironmentManagementClient\Exception\TenantIdNotFoundException;
use OAT\Library\EnvironmentManagementClient\Model\TenantId;
use Psr\Http\Message\MessageInterface;
Expand Down Expand Up @@ -42,6 +42,6 @@ class MyService {
}
}

$myService = new MyService(new TenantIdHeaderExtractor());
$myService = new MyService(new TenantIdExtractor());
$myService->myMethod();
```
5 changes: 5 additions & 0 deletions src/Exception/GrpcCallFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@

final class GrpcCallFailedException extends EnvironmentManagementClientException
{
public static function serverNotReady(): self
{
return new self('gRPC server is not in ready state');
}

public static function duringCall(string $requestName, Throwable $previous): self
{
return new self(
Expand Down
31 changes: 31 additions & 0 deletions src/Exception/LtiMessageExtractFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2021 (original work) Open Assessment Technologies SA;
*/

declare(strict_types=1);

namespace OAT\Library\EnvironmentManagementClient\Exception;

final class LtiMessageExtractFailedException extends EnvironmentManagementClientException
{
public static function unableToExtractLtiMessage(): self
{
return new self('Not able to parse Lti message from JWT token.');
}
}
31 changes: 31 additions & 0 deletions src/Exception/RegistrationIdNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2021 (original work) Open Assessment Technologies SA;
*/

declare(strict_types=1);

namespace OAT\Library\EnvironmentManagementClient\Exception;

final class RegistrationIdNotFoundException extends EnvironmentManagementClientException
{
public static function notInToken(): self
{
return new self('LTI Registration Id not found in JWT token.');
}
}
4 changes: 2 additions & 2 deletions src/Exception/TenantIdNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

final class TenantIdNotFoundException extends EnvironmentManagementClientException
{
public static function notInHeader(): self
public static function notInToken(): self
{
return new self('Tenant Id not found in request header.');
return new self('Tenant Id not found in JWT token.');
}
}
8 changes: 8 additions & 0 deletions src/Exception/TokenUnauthorizedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace OAT\Library\EnvironmentManagementClient\Exception;

class TokenUnauthorizedException extends EnvironmentManagementClientException
{

}
41 changes: 32 additions & 9 deletions src/Grpc/ConfigurationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@
use OAT\Library\EnvironmentManagementClient\Model\Configuration;
use OAT\Library\EnvironmentManagementClient\Model\ConfigurationCollection;
use OAT\Library\EnvironmentManagementClient\Repository\ConfigurationRepositoryInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

final class ConfigurationRepository implements ConfigurationRepositoryInterface
{
use GrpcCallTrait;

private ConfigServiceClient $grpcClient;
private ?LoggerInterface $logger;

public function __construct(ConfigServiceClient $grpcClient)
public function __construct(ConfigServiceClient $grpcClient, ?LoggerInterface $logger = null)
{
$this->grpcClient = $grpcClient;
$this->logger = $logger ?? new NullLogger();
}

public function find(string $tenantId, string $configId): Configuration
Expand All @@ -46,20 +50,39 @@ public function find(string $tenantId, string $configId): Configuration
$grpcRequest->setTenantId($tenantId);
$grpcRequest->setConfigurationId($configId);

return Configuration::fromProtobuf($this->doUnaryCall(
$this->grpcClient->GetConfig($grpcRequest),
GetConfigRequest::class
));
$this->checkClientAvailability($this->grpcClient);

$this->logger->debug('Fetching Configuration', [
'tenantId' => $tenantId,
'configId' => $configId,
'grpc_endpoint' => $this->grpcClient->getTarget(),
]);

return Configuration::fromProtobuf(
$this->doUnaryCall(
$this->grpcClient->GetConfig($grpcRequest, [], ['timeout' => 10 * 1000000]),
GetConfigRequest::class
)
);
}

public function findAll(string $tenantId): ConfigurationCollection
{
$grpcRequest = new ListConfigsRequest();
$grpcRequest->setTenantId($tenantId);

return ConfigurationCollection::fromProtobuf($this->doUnaryCall(
$this->grpcClient->ListConfigs($grpcRequest),
ListConfigsRequest::class
));
$this->checkClientAvailability($this->grpcClient);

$this->logger->debug('Fetching all Configurations', [
'tenantId' => $tenantId,
'grpc_endpoint' => $this->grpcClient->getTarget(),
]);

return ConfigurationCollection::fromProtobuf(
$this->doUnaryCall(
$this->grpcClient->ListConfigs($grpcRequest, [], ['timeout' => 10 * 1000000]),
ListConfigsRequest::class
)
);
}
}
22 changes: 21 additions & 1 deletion src/Grpc/Factory/GrpcClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@
use Oat\Envmgmt\Sidecar\FeatureFlagServiceClient;
use Oat\Envmgmt\Sidecar\LtiServiceClient;
use Oat\Envmgmt\Sidecar\Oauth2ClientServiceClient;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

final class GrpcClientFactory
{
private string $hostname;
private array $opts;
private ?LoggerInterface $logger;

public function __construct()
public function __construct(?LoggerInterface $logger = null)
{
$this->logger = $logger ?? new NullLogger();

$this->hostname = sprintf(
'%s:%s',
getenv('EM_SIDECAR_HOST') ?: 'localhost',
Expand All @@ -48,21 +53,36 @@ public function __construct()

public function createConfigServiceClient(): ConfigServiceClient
{
$this->log(ConfigServiceClient::class);

return new ConfigServiceClient($this->hostname, $this->opts);
}

public function createFeatureFlagServiceClient(): FeatureFlagServiceClient
{
$this->log(FeatureFlagServiceClient::class);

return new FeatureFlagServiceClient($this->hostname, $this->opts);
}

public function createLtiServiceClient(): LtiServiceClient
{
$this->log(LtiServiceClient::class);

return new LtiServiceClient($this->hostname, $this->opts);
}

public function createOauth2ClientServiceClient(): Oauth2ClientServiceClient
{
$this->log(Oauth2ClientServiceClient::class);

return new Oauth2ClientServiceClient($this->hostname, $this->opts);
}

private function log(string $clientName): void
{
$this->logger->debug(sprintf('Creating "%s"', $clientName), [
'hostname' => $this->hostname,
]);
}
}
41 changes: 32 additions & 9 deletions src/Grpc/FeatureFlagRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@
use OAT\Library\EnvironmentManagementClient\Model\FeatureFlag;
use OAT\Library\EnvironmentManagementClient\Model\FeatureFlagCollection;
use OAT\Library\EnvironmentManagementClient\Repository\FeatureFlagRepositoryInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

final class FeatureFlagRepository implements FeatureFlagRepositoryInterface
{
use GrpcCallTrait;

private FeatureFlagServiceClient $grpcClient;
private ?LoggerInterface $logger;

public function __construct(FeatureFlagServiceClient $grpcClient)
public function __construct(FeatureFlagServiceClient $grpcClient, ?LoggerInterface $logger = null)
{
$this->grpcClient = $grpcClient;
$this->logger = $logger ?? new NullLogger();
}

public function find(string $tenantId, string $featureFlagId): FeatureFlag
Expand All @@ -46,20 +50,39 @@ public function find(string $tenantId, string $featureFlagId): FeatureFlag
$grpcRequest->setTenantId($tenantId);
$grpcRequest->setFeatureFlagId($featureFlagId);

return FeatureFlag::fromProtobuf($this->doUnaryCall(
$this->grpcClient->GetFeatureFlag($grpcRequest),
GetFeatureFlagRequest::class
));
$this->checkClientAvailability($this->grpcClient);

$this->logger->debug('Fetching Feature Flag', [
'tenantId' => $tenantId,
'featureFlagId' => $featureFlagId,
'grpc_endpoint' => $this->grpcClient->getTarget(),
]);

return FeatureFlag::fromProtobuf(
$this->doUnaryCall(
$this->grpcClient->GetFeatureFlag($grpcRequest, [], ['timeout' => 10 * 1000000]),
GetFeatureFlagRequest::class
)
);
}

public function findAll(string $tenantId): FeatureFlagCollection
{
$grpcRequest = new ListFeatureFlagsRequest();
$grpcRequest->setTenantId($tenantId);

return FeatureFlagCollection::fromProtobuf($this->doUnaryCall(
$this->grpcClient->ListFeatureFlags($grpcRequest),
ListFeatureFlagsRequest::class
));
$this->checkClientAvailability($this->grpcClient);

$this->logger->debug('Fetching all Feature Flags', [
'tenantId' => $tenantId,
'grpc_endpoint' => $this->grpcClient->getTarget(),
]);

return FeatureFlagCollection::fromProtobuf(
$this->doUnaryCall(
$this->grpcClient->ListFeatureFlags($grpcRequest, [], ['timeout' => 10 * 1000000]),
ListFeatureFlagsRequest::class
)
);
}
}
12 changes: 12 additions & 0 deletions src/Grpc/GrpcCallTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

namespace OAT\Library\EnvironmentManagementClient\Grpc;

use Exception;
use Grpc\BaseStub;
use Grpc\UnaryCall;
use OAT\Library\EnvironmentManagementClient\Exception\GrpcCallFailedException;
use Throwable;
Expand All @@ -44,4 +46,14 @@ private function doUnaryCall(UnaryCall $call, string $requestName)

return $grpcResponse;
}

/**
* @throws Exception
*/
private function checkClientAvailability(BaseStub $client): void
{
if (!$client->waitForReady(10 * 1000000)) { // 10 seconds
throw GrpcCallFailedException::serverNotReady();
}
}
}
Loading

0 comments on commit 7a3892a

Please sign in to comment.