Skip to content

Commit

Permalink
add methods & command to set webserver type for app
Browse files Browse the repository at this point in the history
  • Loading branch information
gforsythe-godaddy committed Jul 1, 2022
1 parent 08a1143 commit bf318cd
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/API/Apps/AppsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Pagely\AtomicClient\API\Apps;

use Pagely\AtomicClient\API\BaseApiClient;
use Psr\Http\Message\ResponseInterface;

class AppsClient extends BaseApiClient
{
Expand Down Expand Up @@ -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(
Expand All @@ -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");
Expand Down
75 changes: 75 additions & 0 deletions src/Command/Apps/AppWebserverCommand.php
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit bf318cd

Please sign in to comment.