From bf318cd80aef22b4858e127941feaadfafa48b0f Mon Sep 17 00:00:00 2001 From: Gordon Forsythe <gordon.forsythe@pagely.com> Date: Fri, 1 Jul 2022 11:30:08 -0700 Subject: [PATCH] add methods & command to set webserver type for app --- src/API/Apps/AppsClient.php | 25 +++++++- src/Command/Apps/AppWebserverCommand.php | 75 ++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 src/Command/Apps/AppWebserverCommand.php diff --git a/src/API/Apps/AppsClient.php b/src/API/Apps/AppsClient.php index bb938c4..a020b7e 100644 --- a/src/API/Apps/AppsClient.php +++ b/src/API/Apps/AppsClient.php @@ -2,6 +2,7 @@ namespace Pagely\AtomicClient\API\Apps; use Pagely\AtomicClient\API\BaseApiClient; +use Psr\Http\Message\ResponseInterface; class AppsClient extends BaseApiClient { @@ -190,10 +191,28 @@ public function remove($accessToken, $appId) ->delete("apps/{$appId}"); } + protected function setWebserverType(string $accessToken, $appId, string $type): ResponseInterface + { + return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) + ->post("apps/{$appId}/server-type", [ + 'json' => ['serverType' => $type], + ]); + } + + public function setWebserverTypeNginx(string $accessToken, int $appId) + { + return $this->setWebserverType($accessToken, $appId, 'nginx'); + } + + public function setWebserverTypeApache(string $accessToken, int $appId) + { + return $this->setWebserverType($accessToken, $appId, 'nginx-apache'); + } + // // Git integration methods // - public function createGitIntegration(string $accessToken, int $appId, string $remoteProvider, string $branch): \Psr\Http\Message\ResponseInterface + public function createGitIntegration(string $accessToken, int $appId, string $remoteProvider, string $branch): ResponseInterface { return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) ->post( @@ -207,13 +226,13 @@ public function createGitIntegration(string $accessToken, int $appId, string $re ); } - public function deleteGitIntegration(string $accessToken, int $appId): \Psr\Http\Message\ResponseInterface + public function deleteGitIntegration(string $accessToken, int $appId): ResponseInterface { return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) ->delete("/apps/{$appId}/git-integration"); } - public function getGitIntegration(string $accessToken, int $appId): \Psr\Http\Message\ResponseInterface + public function getGitIntegration(string $accessToken, int $appId): ResponseInterface { return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) ->get("/apps/{$appId}/git-integration"); diff --git a/src/Command/Apps/AppWebserverCommand.php b/src/Command/Apps/AppWebserverCommand.php new file mode 100644 index 0000000..96835f0 --- /dev/null +++ b/src/Command/Apps/AppWebserverCommand.php @@ -0,0 +1,75 @@ +<?php + +namespace Pagely\AtomicClient\Command\Apps; + +use GuzzleHttp\Exception\ClientException; +use Pagely\AtomicClient\API\AuthApi; +use Pagely\AtomicClient\Command\Command; +use Pagely\AtomicClient\Command\OauthCommandTrait; +use Pagely\AtomicClient\API\Apps\AppsClient; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class AppWebserverCommand extends Command +{ + use OauthCommandTrait; + + /** + * @var AppsClient + */ + protected $api; + + public function __construct(AuthApi $authApi, AppsClient $apps, $name = 'apps:set-server-type') + { + $this->authClient = $authApi; + $this->api = $apps; + parent::__construct($name); + } + + public function configure() + { + parent::configure(); + $this + ->setDescription('Set webserver type for app') + ->addArgument('appId', InputArgument::REQUIRED, 'App ID') + ->addArgument('type', InputArgument::REQUIRED, 'nginx or apache') + ; + $this->addOauthOptions(); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $appId = $input->getArgument('appId'); + $serverType = $input->getArgument('type'); + $token = $this->token->token; + + try { + switch ($serverType) { + case 'apache': + $result = $this->api->setWebserverTypeApache($token, $appId); + break; + case 'nginx': + $result = $this->api->setWebserverTypeNginx($token, $appId); + break; + default: + $output->writeln("<error>Unknown server type: {$serverType}</error> - must be apache or nginx"); + return 1; + } + } catch (ClientException $e) { + $output->writeln("<error>{$e->getMessage()}</error>"); + $output->writeln($e->getResponse()->getBody()->getContents()); + return 1; + } + + $output->writeln( + json_encode( + json_decode( + $result->getBody()->getContents() + ), + JSON_PRETTY_PRINT + ) + ); + return 0; + } +}