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\models\ClusterResource; /** * Description of ClusterResourcesRepository * * @author Pawel Kopec * @version 1.0.0 */ class ClusterResourcesRepository extends AbstractRepository { private $type; private $filters = []; /** * * @param string $type enum vm | storage | node * @return $this */ public function findType($type) { $this->type = $type; return $this; } public function findByNodes($nodes) { $this->addfilter(['node' => $nodes]); return $this; } public function addfilter(array $filter) { $this->filters = array_merge($this->filters, $filter); return $this; } public function findVm() { $this->findType('vm'); return $this; } public function findKvmTemplate() { $this->findType('vm') ->addfilter(['template' => '1']); return $this; } public function findQemu(){ $this ->addfilter(['type' => 'qemu']); return $this; } public function findLxc(){ $this ->addfilter(['type' => 'lxc']); return $this; } public function findVmid($vmid){ $this ->addfilter(['vmid' => $vmid]); return $this; } /** * * @return ClusterResource[] */ public function fetch() { if($this->fetch){ return $this->fetch; } $parameters = []; if ($this->type) { $parameters['type'] = $this->type; } $data = $this->api()->get('/cluster/resources', $parameters); foreach ($data as $k => $resurces) { foreach ($resurces as $keyRes => $resurce) { if (!isset($this->filters[$keyRes])) { continue; } if (is_array($this->filters[$keyRes]) && !in_array($resurce, $this->filters[$keyRes])) { unset($data[$k], $resurces); break; } else if (is_string($this->filters[$keyRes]) && $resurce != $this->filters[$keyRes]) { unset($data[$k], $resurces); break; } } foreach ($this->filters as $fk => $f) { if (!isset($resurces[$fk])) { unset($data[$k], $resurces); break; } } if (empty($resurces)) { continue; } $obj = new ClusterResource(); $obj->setAttributes($resurces); $this->fetch[] = $obj; } return $this->fetch; } public function fetchWithUniqueNames($defaultNode){ if(is_null($defaultNode)){ throw new \InvalidArgumentException("The default node parameter must be defined"); } if($this->filters['node']){ return $this->fetch(); } $currents =[]; foreach ($this->fetch() as $entity){ if($entity->getNode() != $defaultNode){ continue; } $currents [$entity->getVmid()] = $entity->getName(); } $currents = array_unique( $currents); $fetch =[]; foreach ($this->fetch() as $entity){ if( in_array($entity->getName(), $currents) && $entity->getNode() != $defaultNode ){ continue; } $fetch[] = $entity; } return $fetch; } public function firstOrFail(){ foreach ($this->fetch() as $clusterResource){ return $clusterResource; } throw new \InvalidArgumentException("Cannot find: ".ClusterResource::class); } }