ClusterResourcesRepository.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2017-06-07)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace MGProvision\Proxmox\v2\repository;
  20. use MGProvision\Proxmox\v2\models\ClusterResource;
  21. /**
  22. * Description of ClusterResourcesRepository
  23. *
  24. * @author Pawel Kopec <pawelk@modulesgarden.com>
  25. * @version 1.0.0
  26. */
  27. class ClusterResourcesRepository extends AbstractRepository
  28. {
  29. private $type;
  30. private $filters = [];
  31. /**
  32. *
  33. * @param string $type enum vm | storage | node
  34. * @return $this
  35. */
  36. public function findType($type)
  37. {
  38. $this->type = $type;
  39. return $this;
  40. }
  41. public function findByNodes($nodes)
  42. {
  43. $this->addfilter(['node' => $nodes]);
  44. return $this;
  45. }
  46. public function addfilter(array $filter)
  47. {
  48. $this->filters = array_merge($this->filters, $filter);
  49. return $this;
  50. }
  51. public function findVm()
  52. {
  53. $this->findType('vm');
  54. return $this;
  55. }
  56. public function findKvmTemplate()
  57. {
  58. $this->findType('vm')
  59. ->addfilter(['template' => '1']);
  60. return $this;
  61. }
  62. public function findQemu(){
  63. $this ->addfilter(['type' => 'qemu']);
  64. return $this;
  65. }
  66. public function findLxc(){
  67. $this ->addfilter(['type' => 'lxc']);
  68. return $this;
  69. }
  70. public function findVmid($vmid){
  71. $this ->addfilter(['vmid' => $vmid]);
  72. return $this;
  73. }
  74. /**
  75. *
  76. * @return ClusterResource[]
  77. */
  78. public function fetch()
  79. {
  80. if($this->fetch){
  81. return $this->fetch;
  82. }
  83. $parameters = [];
  84. if ($this->type)
  85. {
  86. $parameters['type'] = $this->type;
  87. }
  88. $data = $this->api()->get('/cluster/resources', $parameters);
  89. foreach ($data as $k => $resurces)
  90. {
  91. foreach ($resurces as $keyRes => $resurce)
  92. {
  93. if (!isset($this->filters[$keyRes]))
  94. {
  95. continue;
  96. }
  97. if (is_array($this->filters[$keyRes]) && !in_array($resurce, $this->filters[$keyRes]))
  98. {
  99. unset($data[$k], $resurces);
  100. break;
  101. }
  102. else if (is_string($this->filters[$keyRes]) && $resurce != $this->filters[$keyRes])
  103. {
  104. unset($data[$k], $resurces);
  105. break;
  106. }
  107. }
  108. foreach ($this->filters as $fk => $f)
  109. {
  110. if (!isset($resurces[$fk]))
  111. {
  112. unset($data[$k], $resurces);
  113. break;
  114. }
  115. }
  116. if (empty($resurces))
  117. {
  118. continue;
  119. }
  120. $obj = new ClusterResource();
  121. $obj->setAttributes($resurces);
  122. $this->fetch[] = $obj;
  123. }
  124. return $this->fetch;
  125. }
  126. public function fetchWithUniqueNames($defaultNode){
  127. if(is_null($defaultNode)){
  128. throw new \InvalidArgumentException("The default node parameter must be defined");
  129. }
  130. if($this->filters['node']){
  131. return $this->fetch();
  132. }
  133. $currents =[];
  134. foreach ($this->fetch() as $entity){
  135. if($entity->getNode() != $defaultNode){
  136. continue;
  137. }
  138. $currents [$entity->getVmid()] = $entity->getName();
  139. }
  140. $currents = array_unique( $currents);
  141. $fetch =[];
  142. foreach ($this->fetch() as $entity){
  143. if( in_array($entity->getName(), $currents) && $entity->getNode() != $defaultNode ){
  144. continue;
  145. }
  146. $fetch[] = $entity;
  147. }
  148. return $fetch;
  149. }
  150. public function firstOrFail(){
  151. foreach ($this->fetch() as $clusterResource){
  152. return $clusterResource;
  153. }
  154. throw new \InvalidArgumentException("Cannot find: ".ClusterResource::class);
  155. }
  156. }