Skip to content

Commit

Permalink
CLI-1325: [ide:delete] Accept IDE uuid as option
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Apr 17, 2024
1 parent 0dc9add commit aea7f0b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Ide/IdeCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
26 changes: 16 additions & 10 deletions src/Command/Ide/IdeDeleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 <options=bold>{$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 <options=bold>$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) {
Expand Down
4 changes: 2 additions & 2 deletions src/Command/Ide/Wizard/IdeWizardCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Ide/Wizard/IdeWizardDeleteSshKeyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down
4 changes: 2 additions & 2 deletions src/Command/Ssh/SshKeyCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions tests/phpunit/src/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit aea7f0b

Please sign in to comment.