Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| BlockStorageService | |
100.00% |
11 / 11 |
|
100.00% |
7 / 7 |
9 | |
100.00% |
1 / 1 |
| getBlockDevices | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getBlockDevice | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createBlockDevice | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| updateBlockDevice | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| deleteBlockDevice | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| attachBlockDevice | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| detachBlockDevice | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Vultr\VultrPhp\Services\BlockStorage; |
| 6 | |
| 7 | use Vultr\VultrPhp\Services\VultrService; |
| 8 | use Vultr\VultrPhp\Util\ListOptions; |
| 9 | use Vultr\VultrPhp\VultrClientException; |
| 10 | |
| 11 | /** |
| 12 | * Blockstorage service handler, for blocks endpoints. |
| 13 | * |
| 14 | * @see https://www.vultr.com/api/#tag/block |
| 15 | */ |
| 16 | class BlockStorageService extends VultrService |
| 17 | { |
| 18 | /** |
| 19 | * Retrieve block storage devices on the account. |
| 20 | * |
| 21 | * @see https://www.vultr.com/api/#tag/block/operation/list-blocks |
| 22 | * @param $options - ListOptions|null - Interact via reference. |
| 23 | * @throws BlockStorageException |
| 24 | * @return array |
| 25 | */ |
| 26 | public function getBlockDevices(?ListOptions &$options = null) : array |
| 27 | { |
| 28 | return $this->getListObjects('blocks', new BlockStorage(), $options); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Get a specific block storage device on the account. |
| 33 | * |
| 34 | * @see https://www.vultr.com/api/#tag/block/operation/get-block |
| 35 | * @param $block_id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
| 36 | * @throws BlockStorageException |
| 37 | * @return BlockStorage |
| 38 | */ |
| 39 | public function getBlockDevice(string $block_id) : BlockStorage |
| 40 | { |
| 41 | return $this->getObject('blocks/'.$block_id, new BlockStorage()); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Create a new block storage device in a region. The size range differs based on the block_type |
| 46 | * |
| 47 | * @see https://www.vultr.com/api/#tag/block/operation/create-block |
| 48 | * @param $block - BlockStorage |
| 49 | * @throws BlockStorageException |
| 50 | * @return array |
| 51 | */ |
| 52 | public function createBlockDevice(BlockStorage $block) : BlockStorage |
| 53 | { |
| 54 | return $this->createObject('blocks', new BlockStorage(), $block->getInitializedProps()); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Update information on the block storage device. |
| 59 | * |
| 60 | * @see https://www.vultr.com/api/#tag/block/operation/update-block |
| 61 | * @param $block - BlockStorage |
| 62 | * @throws BlockStorageException |
| 63 | * @return void |
| 64 | */ |
| 65 | public function updateBlockDevice(BlockStorage $block) : void |
| 66 | { |
| 67 | $this->patchObject('blocks/'.$block->getId(), $block); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Delete the block storage device. |
| 72 | * |
| 73 | * @see https://www.vultr.com/api/#tag/block/operation/delete-block |
| 74 | * @param $block_id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
| 75 | * @throws BlockStorageException |
| 76 | * @return void |
| 77 | */ |
| 78 | public function deleteBlockDevice(string $block_id) : void |
| 79 | { |
| 80 | $this->deleteObject('blocks/'.$block_id, new BlockStorage()); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Attach the block storage device to a virtual machine. |
| 85 | * |
| 86 | * @see https://www.vultr.com/api/#tag/block/operation/attach-block |
| 87 | * @param $block_id - string - Example cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
| 88 | * @param $live - bool |
| 89 | * @throws BlockStorageException |
| 90 | * @return void |
| 91 | */ |
| 92 | public function attachBlockDevice(string $block_id, string $instance_id, bool $live = true) : void |
| 93 | { |
| 94 | try |
| 95 | { |
| 96 | $this->getClientHandler()->post('blocks/'.$block_id.'/attach', [ |
| 97 | 'instance_id' => $instance_id, |
| 98 | 'live' => $live |
| 99 | ]); |
| 100 | } |
| 101 | catch (VultrClientException $e) |
| 102 | { |
| 103 | throw new BlockStorageException('Failed to attach block device: '.$e->getMessage(), $e->getHTTPCode(), $e); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Detach the block storage device from the virtual machine. |
| 109 | * |
| 110 | * @see https://www.vultr.com/api/#tag/block/operation/detach-block |
| 111 | * @param $block_id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
| 112 | * @param $live - bool |
| 113 | * @throws BlockStorageException |
| 114 | * @return void |
| 115 | */ |
| 116 | public function detachBlockDevice(string $block_id, bool $live = true) : void |
| 117 | { |
| 118 | try |
| 119 | { |
| 120 | $this->getClientHandler()->post('blocks/'.$block_id.'/detach', ['live' => $live]); |
| 121 | } |
| 122 | catch (VultrClientException $e) |
| 123 | { |
| 124 | throw new BlockStorageException('Failed to detach block device: '.$e->getMessage(), $e->getHTTPCode(), $e); |
| 125 | } |
| 126 | } |
| 127 | } |