| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- /* * ********************************************************************
- * ProxmoxVPS product developed. (2017-06-07)
- * *
- *
- * CREATED BY MODULESGARDEN -> 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 <pawelk@modulesgarden.com>
- * @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);
- }
- }
|