From dcb40ede774cf305a99f92d6ed8e9e5870436f28 Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Tue, 23 Jan 2024 10:45:32 -0800 Subject: [PATCH] CLI-1245: Use REMOTEIDE_LABEL to bypass API call (#1665) * CLI-1245: Use REMOTEIDE_LABEL to bypass API call * kill mutants --- src/Command/CommandBase.php | 11 ++++++----- src/Command/Ide/Wizard/IdeWizardCommandBase.php | 9 ++------- .../Ide/Wizard/IdeWizardCreateSshKeyCommand.php | 1 - src/Command/Ssh/SshKeyCommandBase.php | 5 ++--- .../phpunit/src/Commands/Ide/IdeDeleteCommandTest.php | 5 +---- tests/phpunit/src/Commands/Ide/IdeHelper.php | 1 + .../Ide/Wizard/IdeWizardCreateSshKeyCommandTest.php | 10 ---------- .../Ide/Wizard/IdeWizardDeleteSshKeyCommandTest.php | 9 ++------- tests/phpunit/src/TestBase.php | 5 ++--- 9 files changed, 16 insertions(+), 40 deletions(-) diff --git a/src/Command/CommandBase.php b/src/Command/CommandBase.php index 316e702ab..09e695079 100644 --- a/src/Command/CommandBase.php +++ b/src/Command/CommandBase.php @@ -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; @@ -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); @@ -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; diff --git a/src/Command/Ide/Wizard/IdeWizardCommandBase.php b/src/Command/Ide/Wizard/IdeWizardCommandBase.php index 0cc138325..2f122ca5c 100644 --- a/src/Command/Ide/Wizard/IdeWizardCommandBase.php +++ b/src/Command/Ide/Wizard/IdeWizardCommandBase.php @@ -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; @@ -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 { @@ -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 { diff --git a/src/Command/Ide/Wizard/IdeWizardCreateSshKeyCommand.php b/src/Command/Ide/Wizard/IdeWizardCreateSshKeyCommand.php index a77785a93..5a2aa470f 100644 --- a/src/Command/Ide/Wizard/IdeWizardCreateSshKeyCommand.php +++ b/src/Command/Ide/Wizard/IdeWizardCreateSshKeyCommand.php @@ -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(); diff --git a/src/Command/Ssh/SshKeyCommandBase.php b/src/Command/Ssh/SshKeyCommandBase.php index 60075750a..9dbb17328 100644 --- a/src/Command/Ssh/SshKeyCommandBase.php +++ b/src/Command/Ssh/SshKeyCommandBase.php @@ -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; @@ -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 { diff --git a/tests/phpunit/src/Commands/Ide/IdeDeleteCommandTest.php b/tests/phpunit/src/Commands/Ide/IdeDeleteCommandTest.php index 68ccc584e..eb40b65e6 100644 --- a/tests/phpunit/src/Commands/Ide/IdeDeleteCommandTest.php +++ b/tests/phpunit/src/Commands/Ide/IdeDeleteCommandTest.php @@ -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; /** @@ -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); diff --git a/tests/phpunit/src/Commands/Ide/IdeHelper.php b/tests/phpunit/src/Commands/Ide/IdeHelper.php index 245e19fae..15b09fa02 100644 --- a/tests/phpunit/src/Commands/Ide/IdeHelper.php +++ b/tests/phpunit/src/Commands/Ide/IdeHelper.php @@ -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, ]; } diff --git a/tests/phpunit/src/Commands/Ide/Wizard/IdeWizardCreateSshKeyCommandTest.php b/tests/phpunit/src/Commands/Ide/Wizard/IdeWizardCreateSshKeyCommandTest.php index 14312f52a..45d747f47 100644 --- a/tests/phpunit/src/Commands/Ide/Wizard/IdeWizardCreateSshKeyCommandTest.php +++ b/tests/phpunit/src/Commands/Ide/Wizard/IdeWizardCreateSshKeyCommandTest.php @@ -7,7 +7,6 @@ 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 @@ -15,15 +14,12 @@ */ 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); } @@ -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(); } diff --git a/tests/phpunit/src/Commands/Ide/Wizard/IdeWizardDeleteSshKeyCommandTest.php b/tests/phpunit/src/Commands/Ide/Wizard/IdeWizardDeleteSshKeyCommandTest.php index be5c4e5fd..09999118d 100644 --- a/tests/phpunit/src/Commands/Ide/Wizard/IdeWizardDeleteSshKeyCommandTest.php +++ b/tests/phpunit/src/Commands/Ide/Wizard/IdeWizardDeleteSshKeyCommandTest.php @@ -7,7 +7,6 @@ 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 @@ -15,11 +14,7 @@ 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); @@ -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); } diff --git a/tests/phpunit/src/TestBase.php b/tests/phpunit/src/TestBase.php index 2b418350d..c328b8c20 100644 --- a/tests/phpunit/src/TestBase.php +++ b/tests/phpunit/src/TestBase.php @@ -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; @@ -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();