From aea7f0b73137abb36ebdad709b1ddd6372dd9110 Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Wed, 17 Apr 2024 13:15:15 -0700 Subject: [PATCH] CLI-1325: [ide:delete] Accept IDE uuid as option --- src/Command/CommandBase.php | 4 +-- src/Command/Ide/IdeCommandBase.php | 2 +- src/Command/Ide/IdeDeleteCommand.php | 26 ++++++++++++------- .../Ide/Wizard/IdeWizardCommandBase.php | 4 +-- .../Wizard/IdeWizardDeleteSshKeyCommand.php | 2 +- src/Command/Ssh/SshKeyCommandBase.php | 4 +-- tests/phpunit/src/TestBase.php | 3 +-- 7 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/Command/CommandBase.php b/src/Command/CommandBase.php index 289740424..42d5ab9aa 100644 --- a/src/Command/CommandBase.php +++ b/src/Command/CommandBase.php @@ -1085,10 +1085,10 @@ protected function requireCloudIdeEnvironment(): void { /** * @return \stdClass|null */ - protected function findIdeSshKeyOnCloud(string $ideUuid): ?stdClass { + protected function findIdeSshKeyOnCloud(string $ideLabel, string $ideUuid): ?stdClass { $acquiaCloudClient = $this->cloudApiClientService->getClient(); $cloudKeys = $acquiaCloudClient->request('get', '/account/ssh-keys'); - $sshKeyLabel = SshKeyCommandBase::getIdeSshKeyLabel(); + $sshKeyLabel = SshKeyCommandBase::getIdeSshKeyLabel($ideLabel, $ideUuid); foreach ($cloudKeys as $cloudKey) { if ($cloudKey->label === $sshKeyLabel) { return $cloudKey; diff --git a/src/Command/Ide/IdeCommandBase.php b/src/Command/Ide/IdeCommandBase.php index a7f09bc71..3c07ab03d 100644 --- a/src/Command/Ide/IdeCommandBase.php +++ b/src/Command/Ide/IdeCommandBase.php @@ -20,7 +20,7 @@ protected function promptIdeChoice( string $questionText, Ides $idesResource, string $cloudApplicationUuid - ): ?IdeResponse { + ): IdeResponse { $ides = iterator_to_array($idesResource->getAll($cloudApplicationUuid)); if (empty($ides)) { throw new AcquiaCliException('No IDEs exist for this application.'); diff --git a/src/Command/Ide/IdeDeleteCommand.php b/src/Command/Ide/IdeDeleteCommand.php index aaae9d213..e90ea2af4 100644 --- a/src/Command/Ide/IdeDeleteCommand.php +++ b/src/Command/Ide/IdeDeleteCommand.php @@ -10,6 +10,7 @@ use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; #[RequireAuth] @@ -20,27 +21,32 @@ final class IdeDeleteCommand extends IdeCommandBase { protected function configure(): void { $this->acceptApplicationUuid(); - // @todo Add option to accept an ide UUID. + // @todo make this an argument + $this->addOption('uuid', NULL, InputOption::VALUE_OPTIONAL, 'UUID of the IDE to delete'); } protected function execute(InputInterface $input, OutputInterface $output): int { $acquiaCloudClient = $this->cloudApiClientService->getClient(); $idesResource = new Ides($acquiaCloudClient); - $cloudApplicationUuid = $this->determineCloudApplication(); - $ide = $this->promptIdeChoice("Select the IDE you'd like to delete:", $idesResource, $cloudApplicationUuid); - $answer = $this->io->confirm("Are you sure you want to delete {$ide->label}"); - if (!$answer) { - $this->io->writeln('Ok, nevermind.'); - return 1; + $ideUuid = $input->getOption('uuid'); + if ($ideUuid) { + $ide = $idesResource->get($ideUuid); + } + else { + $cloudApplicationUuid = $this->determineCloudApplication(); + $ide = $this->promptIdeChoice("Select the IDE you'd like to delete:", $idesResource, $cloudApplicationUuid); + $answer = $this->io->confirm("Are you sure you want to delete $ide->label"); + if (!$answer) { + $this->io->writeln('Ok, never mind.'); + return Command::FAILURE; + } } $response = $idesResource->delete($ide->uuid); $this->io->writeln($response->message); - // @todo Remove after CXAPI-8261 is closed. - $this->io->writeln("This process usually takes a few minutes."); // Check to see if an SSH key for this IDE exists on Cloud. - $cloudKey = $this->findIdeSshKeyOnCloud($ide->uuid); + $cloudKey = $this->findIdeSshKeyOnCloud($ide->label, $ide->uuid); if ($cloudKey) { $answer = $this->io->confirm('Would you like to delete the SSH key associated with this IDE from your Cloud Platform account?'); if ($answer) { diff --git a/src/Command/Ide/Wizard/IdeWizardCommandBase.php b/src/Command/Ide/Wizard/IdeWizardCommandBase.php index 2f122ca5c..43cccbb19 100644 --- a/src/Command/Ide/Wizard/IdeWizardCommandBase.php +++ b/src/Command/Ide/Wizard/IdeWizardCommandBase.php @@ -37,11 +37,11 @@ protected function validateEnvironment(): void { } protected function getSshKeyLabel(): string { - return $this::getIdeSshKeyLabel(); + return $this::getIdeSshKeyLabel(self::getThisCloudIdeLabel(), self::getThisCloudIdeUuid()); } protected function deleteThisSshKeyFromCloud(mixed $output): void { - if ($cloudKey = $this->findIdeSshKeyOnCloud($this::getThisCloudIdeUuid())) { + if ($cloudKey = $this->findIdeSshKeyOnCloud($this::getThisCloudIdeLabel(), $this::getThisCloudIdeUuid())) { $this->deleteSshKeyFromCloud($output, $cloudKey); } } diff --git a/src/Command/Ide/Wizard/IdeWizardDeleteSshKeyCommand.php b/src/Command/Ide/Wizard/IdeWizardDeleteSshKeyCommand.php index 97a4c87bf..d1a07faa9 100644 --- a/src/Command/Ide/Wizard/IdeWizardDeleteSshKeyCommand.php +++ b/src/Command/Ide/Wizard/IdeWizardDeleteSshKeyCommand.php @@ -27,7 +27,7 @@ protected function configure(): void { protected function execute(InputInterface $input, OutputInterface $output): int { $this->requireCloudIdeEnvironment(); - $cloudKey = $this->findIdeSshKeyOnCloud($this::getThisCloudIdeUuid()); + $cloudKey = $this->findIdeSshKeyOnCloud($this::getThisCloudIdeLabel(), $this::getThisCloudIdeUuid()); if (!$cloudKey) { throw new AcquiaCliException('Could not find an SSH key on the Cloud Platform matching any local key in this IDE.'); } diff --git a/src/Command/Ssh/SshKeyCommandBase.php b/src/Command/Ssh/SshKeyCommandBase.php index 9dbb17328..6a22b00b7 100644 --- a/src/Command/Ssh/SshKeyCommandBase.php +++ b/src/Command/Ssh/SshKeyCommandBase.php @@ -39,8 +39,8 @@ protected function setSshKeyFilepath(string $privateSshKeyFilename): void { $this->publicSshKeyFilepath = $this->privateSshKeyFilepath . '.pub'; } - public static function getIdeSshKeyLabel(): string { - return self::normalizeSshKeyLabel('IDE_' . self::getThisCloudIdeLabel() . '_' . self::getThisCloudIdeUuid()); + public static function getIdeSshKeyLabel(string $ideLabel, string $ideUuid): string { + return self::normalizeSshKeyLabel('IDE_' . $ideLabel . '_' . $ideUuid); } public static function normalizeSshKeyLabel(?string $label): string|null { diff --git a/tests/phpunit/src/TestBase.php b/tests/phpunit/src/TestBase.php index 9d32dd2b4..09c97cf39 100644 --- a/tests/phpunit/src/TestBase.php +++ b/tests/phpunit/src/TestBase.php @@ -8,7 +8,6 @@ use Acquia\Cli\Application; use Acquia\Cli\CloudApi\ClientService; use Acquia\Cli\CloudApi\CloudCredentials; -use Acquia\Cli\Command\Ssh\SshKeyCommandBase; use Acquia\Cli\Config\AcquiaCliConfig; use Acquia\Cli\Config\CloudDataConfig; use Acquia\Cli\DataStore\AcquiaCliDatastore; @@ -577,7 +576,7 @@ protected function mockListSshKeysRequest(): array { protected function mockListSshKeysRequestWithIdeKey(): object { $mockBody = $this->getMockResponseFromSpec('/account/ssh-keys', 'get', '200'); - $mockBody->{'_embedded'}->items[0]->label = SshKeyCommandBase::getIdeSshKeyLabel(); + $mockBody->{'_embedded'}->items[0]->label = 'IDE_ExampleIDE_215824ff272a4a8c9027df32ed1d68a9'; $this->clientProphecy->request('get', '/account/ssh-keys') ->willReturn($mockBody->{'_embedded'}->items) ->shouldBeCalled();