StorageRepository.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2016-10-11)
  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. /**
  21. * Description of StorageRepository
  22. *
  23. * @author Pawel Kopec <pawelk@modulesgarden.com>
  24. * @version 1.0.0
  25. */
  26. class StorageRepository extends AbstractRepository
  27. {
  28. protected $nodes = array();
  29. private $enabled;
  30. protected $filterPattern;
  31. public function findEnabed()
  32. {
  33. $this->enabled = 1;
  34. return $this;
  35. }
  36. public function findContent($content)
  37. {
  38. $this->content = $content;
  39. return $this;
  40. }
  41. public function findIso()
  42. {
  43. $this->content = 'iso';
  44. return $this;
  45. }
  46. public function findSnippets(){
  47. $this->filterPattern = "/snippets/";
  48. }
  49. /**
  50. * @return \MGProvision\Proxmox\v2\models\Storage[]
  51. */
  52. public function fetch()
  53. {
  54. $request = $this->content ? array("content" => $this->content) : array();
  55. if (!is_null($this->enabled))
  56. {
  57. $request['enabled'] = $this->enabled;
  58. }
  59. $data = array();
  60. foreach ($this->nodes as $node)
  61. {
  62. $storages = $this->api()->get("/nodes/{$node}/storage", $request);
  63. foreach ($storages as $storage)
  64. {
  65. if ($this->enabled && (!$storage['enabled'] && version_compare($this->api()->getVersion(), "5.0", '>') ))
  66. {
  67. continue;
  68. }
  69. $storageObj = new \MGProvision\Proxmox\v2\models\Storage();
  70. $storageObj->setAttributes($storage);
  71. $data[] = $storageObj;
  72. }
  73. }
  74. if(empty($this->nodes)){
  75. $storages = $this->api()->get("/storage", $request);
  76. foreach ($storages as $storage)
  77. {
  78. if($this->filterPattern && !preg_match($this->filterPattern, $storage['content'])){
  79. continue;
  80. }
  81. $storageObj = new \MGProvision\Proxmox\v2\models\Storage();
  82. $storageObj->setAttributes($storage);
  83. $data[] = $storageObj;
  84. }
  85. }
  86. return $data;
  87. }
  88. public function fetchAsArray()
  89. {
  90. $data = array();
  91. foreach ($this->fetch() as $storage)
  92. {
  93. $data[] = $storage->getStorage();
  94. }
  95. return $data;
  96. }
  97. public function findByNodes(array $nodes)
  98. {
  99. $this->nodes = $nodes;
  100. return $this;
  101. }
  102. }