Skip to content

Commit

Permalink
chore: handle the failed state before the complete state for a VM build
Browse files Browse the repository at this point in the history
  • Loading branch information
bencromwell committed Apr 30, 2024
1 parent 5150222 commit e90366b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/Exceptions/VirtualMachines/VirtualMachineBuildFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace WHMCS\Module\Server\Katapult\Exceptions\VirtualMachines;

use WHMCS\Module\Server\Katapult\Exceptions\Exception;

class VirtualMachineBuildFailed extends Exception
{
}
9 changes: 9 additions & 0 deletions lib/WHMCS/Service/VirtualMachine.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Krystal\Katapult\KatapultAPI\Model\VirtualMachineLookup;
use Krystal\Katapult\KatapultAPI\Model\GetVirtualMachine200ResponseVirtualMachine as KatapultVirtualMachine;
use Psr\Http\Message\ResponseInterface;
use WHMCS\Module\Server\Katapult\Exceptions\VirtualMachines\VirtualMachineBuildFailed;
use WHMCS\Module\Server\Katapult\Exceptions\VirtualMachines\VirtualMachineBuilding;
use WHMCS\Module\Server\Katapult\Exceptions\VirtualMachines\VirtualMachineBuildNotFound;
use WHMCS\Module\Server\Katapult\Exceptions\VirtualMachines\VirtualMachineBuildTimeout;
Expand Down Expand Up @@ -109,6 +110,7 @@ public function silentlyCheckForExistingBuildAttempt(): void
* @throws VirtualMachineBuildNotFound
* @throws VirtualMachineBuilding
* @throws VirtualMachineExists
* @throws VirtualMachineBuildFailed
* @throws VirtualMachineBuildTimeout
*/
public function checkForExistingBuildAttempt(): void
Expand Down Expand Up @@ -142,11 +144,18 @@ public function checkForExistingBuildAttempt(): void
}

// Is the build state complete?
// Possible states:
// "draft"
// "failed"
// "pending"
// "complete"
// "building"
// We're after "complete", but it could have permanently failed as well
// So check for that first
if ($apiResult->getVirtualMachineBuild()->getState() === 'failed') {
throw new VirtualMachineBuildFailed('The VM build has failed');
}
// ...and if it didn't fail, see if it's complete yet
if ($apiResult->getVirtualMachineBuild()->getState() !== 'complete') {
throw new VirtualMachineBuilding('The VM build is still in progress');
}
Expand Down

0 comments on commit e90366b

Please sign in to comment.