Skip to content

Commit

Permalink
CLI-1245: Use REMOTEIDE_LABEL to bypass API call (#1665)
Browse files Browse the repository at this point in the history
* CLI-1245: Use REMOTEIDE_LABEL to bypass API call

* kill mutants
  • Loading branch information
danepowell authored Jan 23, 2024
1 parent 4a13fec commit dcb40ed
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 40 deletions.
11 changes: 6 additions & 5 deletions src/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use AcquiaCloudApi\Endpoints\Account;
use AcquiaCloudApi\Endpoints\Applications;
use AcquiaCloudApi\Endpoints\Environments;
use AcquiaCloudApi\Endpoints\Ides;
use AcquiaCloudApi\Endpoints\Notifications;
use AcquiaCloudApi\Endpoints\Organizations;
use AcquiaCloudApi\Endpoints\Subscriptions;
Expand Down Expand Up @@ -786,10 +785,14 @@ protected static function getThisCloudIdeCloudAppUuid(): false|string {
*
* This command assumes it is being run inside a Cloud IDE.
*/
public static function getThisCloudIdeUuid(): false|string {
protected static function getThisCloudIdeUuid(): false|string {
return getenv('REMOTEIDE_UUID');
}

protected static function getThisCloudIdeLabel(): false|string {
return getenv('REMOTEIDE_LABEL');
}

protected function getCloudApplication(string $applicationUuid): ApplicationResponse {
$applicationsResource = new Applications($this->cloudApiClientService->getClient());
return $applicationsResource->get($applicationUuid);
Expand Down Expand Up @@ -908,9 +911,7 @@ protected function requireCloudIdeEnvironment(): void {
protected function findIdeSshKeyOnCloud(string $ideUuid): ?stdClass {
$acquiaCloudClient = $this->cloudApiClientService->getClient();
$cloudKeys = $acquiaCloudClient->request('get', '/account/ssh-keys');
$idesResource = new Ides($acquiaCloudClient);
$ide = $idesResource->get($ideUuid);
$sshKeyLabel = SshKeyCommandBase::getIdeSshKeyLabel($ide);
$sshKeyLabel = SshKeyCommandBase::getIdeSshKeyLabel();
foreach ($cloudKeys as $cloudKey) {
if ($cloudKey->label === $sshKeyLabel) {
return $cloudKey;
Expand Down
9 changes: 2 additions & 7 deletions src/Command/Ide/Wizard/IdeWizardCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Acquia\Cli\Command\WizardCommandBase;
use Acquia\Cli\Helpers\SshCommandTrait;
use AcquiaCloudApi\Endpoints\Ides;
use AcquiaCloudApi\Response\IdeResponse;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -25,12 +24,8 @@ abstract class IdeWizardCommandBase extends WizardCommandBase {
protected function initialize(InputInterface $input, OutputInterface $output): void {
parent::initialize($input, $output);

$this->ideUuid = $this::getThisCloudIdeUuid();
$this->setSshKeyFilepath(self::getSshKeyFilename($this->ideUuid));
$this->setSshKeyFilepath(self::getSshKeyFilename($this::getThisCloudIdeUuid()));
$this->passphraseFilepath = $this->localMachineHelper->getLocalFilepath('~/.passphrase');
$acquiaCloudClient = $this->cloudApiClientService->getClient();
$idesResource = new Ides($acquiaCloudClient);
$this->ide = $idesResource->get($this->ideUuid);
}

public static function getSshKeyFilename(mixed $ideUuid): string {
Expand All @@ -42,7 +37,7 @@ protected function validateEnvironment(): void {
}

protected function getSshKeyLabel(): string {
return $this::getIdeSshKeyLabel($this->ide);
return $this::getIdeSshKeyLabel();
}

protected function deleteThisSshKeyFromCloud(mixed $output): void {
Expand Down
1 change: 0 additions & 1 deletion src/Command/Ide/Wizard/IdeWizardCreateSshKeyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
// the local key, delete remote key!
$this->deleteThisSshKeyFromCloud($output);
$publicKey = $this->localMachineHelper->readFile($this->publicSshKeyFilepath);
$chosenLocalKey = basename($this->publicSshKeyFilepath);
$this->uploadSshKey($this->getSshKeyLabel(), $publicKey);

$checklist->completePreviousItem();
Expand Down
5 changes: 2 additions & 3 deletions src/Command/Ssh/SshKeyCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Acquia\Cli\Output\Spinner\Spinner;
use AcquiaCloudApi\Connector\Client;
use AcquiaCloudApi\Endpoints\SshKeys;
use AcquiaCloudApi\Response\IdeResponse;
use React\EventLoop\Loop;
use RuntimeException;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -40,8 +39,8 @@ protected function setSshKeyFilepath(string $privateSshKeyFilename): void {
$this->publicSshKeyFilepath = $this->privateSshKeyFilepath . '.pub';
}

public static function getIdeSshKeyLabel(IdeResponse $ide): string {
return self::normalizeSshKeyLabel('IDE_' . $ide->label . '_' . $ide->uuid);
public static function getIdeSshKeyLabel(): string {
return self::normalizeSshKeyLabel('IDE_' . self::getThisCloudIdeLabel() . '_' . self::getThisCloudIdeUuid());
}

public static function normalizeSshKeyLabel(?string $label): string|null {
Expand Down
5 changes: 1 addition & 4 deletions tests/phpunit/src/Commands/Ide/IdeDeleteCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Acquia\Cli\Command\Ide\IdeDeleteCommand;
use Acquia\Cli\Command\Ssh\SshKeyDeleteCommand;
use Acquia\Cli\Tests\CommandTestBase;
use AcquiaCloudApi\Response\IdeResponse;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand Down Expand Up @@ -39,9 +38,7 @@ public function testIdeDeleteCommand(): void {
$this->mockRequest('getApplicationByUuid', $applications[0]->uuid);
$ides = $this->mockRequest('getApplicationIdes', $applications[0]->uuid);
$this->mockRequest('deleteIde', $ides[0]->uuid, NULL, 'De-provisioning IDE');
$ideGetResponse = $this->mockRequest('getIde', $ides[0]->uuid);
$ide = new IdeResponse((object) $ideGetResponse);
$sshKeyGetResponse = $this->mockListSshKeysRequestWithIdeKey($ide);
$sshKeyGetResponse = $this->mockListSshKeysRequestWithIdeKey();

$this->mockDeleteSshKeyRequest($sshKeyGetResponse->{'_embedded'}->items[0]->uuid);

Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/src/Commands/Ide/IdeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static function getEnvVars(): array {
return [
'ACQUIA_USER_UUID' => '4acf8956-45df-3cf4-5106-065b62cf1ac8',
'AH_SITE_ENVIRONMENT' => 'IDE',
'REMOTEIDE_LABEL' => 'ExampleIDE',
'REMOTEIDE_UUID' => self::$remoteIdeUuid,
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@
use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Command\Ide\Wizard\IdeWizardCreateSshKeyCommand;
use Acquia\Cli\Tests\Commands\Ide\IdeHelper;
use AcquiaCloudApi\Response\IdeResponse;

/**
* @property \Acquia\Cli\Command\Ide\Wizard\IdeWizardCreateSshKeyCommand $command
* @requires OS linux|darwin
*/
class IdeWizardCreateSshKeyCommandTest extends IdeWizardTestBase {

protected IdeResponse $ide;

public function setUp(): void {
parent::setUp();
$applicationResponse = $this->mockApplicationRequest();
$this->mockListSshKeysRequest();
$this->mockRequest('getAccount');
$this->mockPermissionsRequest($applicationResponse);
$this->ide = $this->mockIdeRequest();
$this->sshKeyFileName = IdeWizardCreateSshKeyCommand::getSshKeyFilename(IdeHelper::$remoteIdeUuid);
}

Expand All @@ -34,12 +30,6 @@ protected function createCommand(): CommandBase {
return $this->injectCommand(IdeWizardCreateSshKeyCommand::class);
}

protected function mockIdeRequest(): IdeResponse {
$ideResponse = $this->getMockResponseFromSpec('/ides/{ideUuid}', 'get', '200');
$this->clientProphecy->request('get', '/ides/' . IdeHelper::$remoteIdeUuid)->willReturn($ideResponse)->shouldBeCalled();
return new IdeResponse($ideResponse);
}

public function testCreate(): void {
parent::runTestCreate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,14 @@
use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Command\Ide\Wizard\IdeWizardDeleteSshKeyCommand;
use Acquia\Cli\Tests\Commands\Ide\IdeHelper;
use AcquiaCloudApi\Response\IdeResponse;

/**
* @property \Acquia\Cli\Command\Ide\Wizard\IdeWizardCreateSshKeyCommand $command
*/
class IdeWizardDeleteSshKeyCommandTest extends IdeWizardTestBase {

public function testDelete(): void {
// Request for IDE data.
$ideResponse = $this->getMockResponseFromSpec('/ides/{ideUuid}', 'get', '200');
$this->clientProphecy->request('get', '/ides/' . IdeHelper::$remoteIdeUuid)->willReturn($ideResponse)->shouldBeCalled();
$ide = new IdeResponse((object) $ideResponse);
$mockBody = $this->mockListSshKeysRequestWithIdeKey($ide);
$mockBody = $this->mockListSshKeysRequestWithIdeKey();

$this->mockDeleteSshKeyRequest($mockBody->{'_embedded'}->items[0]->uuid);

Expand All @@ -29,7 +24,7 @@ public function testDelete(): void {
$this->fs->dumpFile($this->sshDir . '/' . $sshKeyFilename . '.pub', $mockBody->{'_embedded'}->items[0]->public_key);

// Run it!
$this->executeCommand([]);
$this->executeCommand();

$this->assertFileDoesNotExist($this->sshDir . '/' . $sshKeyFilename);
}
Expand Down
5 changes: 2 additions & 3 deletions tests/phpunit/src/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Acquia\Cli\Helpers\TelemetryHelper;
use AcquiaCloudApi\Connector\Client;
use AcquiaCloudApi\Exception\ApiErrorException;
use AcquiaCloudApi\Response\IdeResponse;
use AcquiaLogstream\LogstreamManager;
use Closure;
use GuzzleHttp\Psr7\Response;
Expand Down Expand Up @@ -591,9 +590,9 @@ protected function mockListSshKeysRequest(): array {
return $this->mockRequest('getAccountSshKeys');
}

protected function mockListSshKeysRequestWithIdeKey(IdeResponse $ide): object {
protected function mockListSshKeysRequestWithIdeKey(): object {
$mockBody = $this->getMockResponseFromSpec('/account/ssh-keys', 'get', '200');
$mockBody->{'_embedded'}->items[0]->label = SshKeyCommandBase::getIdeSshKeyLabel($ide);
$mockBody->{'_embedded'}->items[0]->label = SshKeyCommandBase::getIdeSshKeyLabel();
$this->clientProphecy->request('get', '/account/ssh-keys')
->willReturn($mockBody->{'_embedded'}->items)
->shouldBeCalled();
Expand Down

0 comments on commit dcb40ed

Please sign in to comment.