http://modulesgarden.com * CONTACT -> contact@modulesgarden.com * * * This software is furnished under a license and may be used and copied * only in accordance with the terms of such license and with the * inclusion of the above copyright notice. This software or any other * copies thereof may not be provided or otherwise made available to any * other person. No title to and ownership of the software is hereby * transferred. * * * ******************************************************************** */ namespace MGProvision\Proxmox\v2\repository; use \MGProvision\Proxmox\v2\ProxmoxApiException; use \MGProvision\Proxmox\v2\models\MountPoint; /** * Description of MounPointRepostiory * * @author Pawel Kopec */ class MountPointRepostiory extends AbstractRepository { protected $path; private $excludeIds=[]; public function findByPath($path) { if (!preg_match('/config/', $path)) { throw new ProxmoxApiException(sprintf("MounPoint path ('%s') is not valid", $path)); } $this->path = $path; return $this; } /** * * @return MountPoint[] * @throws ProxmoxApiException */ public function fetch() { if ($this->fetch) { return $this->fetch; } if (empty($this->path)) { throw new ProxmoxApiException("MounPoint path is empty"); } $this->fetch = []; foreach ($this->api()->get($this->path) as $id => $config) { if (!preg_match('/mp/', $id) && $id != 'rootfs' && !preg_match('/disk/', $config)) { continue; } $this->fetch[$id] = (new MountPoint($id, $config))->setPath($this->path); } krsort($this->fetch); return $this->fetch; } public function size() { $size = 0; foreach ($this->fetch() as $mp) { $size += (int) $mp->getSize(); } return $size; } /** * Return sieze in GB * @return int * @throws ProxmoxApiException */ public function additionalSize() { $size = 0; foreach ($this->fetch() as $mp) { if ($mp->getId() == "rootfs") { continue; } else if($this->excludeIds && in_array($mp->getId(), $this->excludeIds)){ continue; } $size += (int) $mp->getSize(); } return $size; } public function additionalSizeToBytes(){ //GB => Bytes return $this->additionalSize() * pow(1024, 3); } public function nextId() { $used = []; foreach ($this->fetch() as $mp) { $used[] = preg_replace("/[a-z]+/", "", $mp->getId()); } for ($n = 0; $n <= 7; $n++) { if (!in_array($n, $used)) { return "mp" . $n; } } throw new ProxmoxApiException("Mount Point limit exceeded"); } public function findMountPointById($id){ foreach($this->fetch() as $entity){ if($entity->getId()==$id){ return $entity; } } throw new ProxmoxApiException("Mount Point {$id} not found"); } public function findById($id){ return $this->findMountPointById($id); } public function whereNotIn(array $excludeIds){ $this->excludeIds = $excludeIds; return $this; } public function deleteUnused(){ $this->reset(); foreach ($this->fetch() as $disk){ if(preg_match('/unused/', $disk->getId())){ $disk->delete(); } } } }