TaskRepository.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /* * ********************************************************************
  3. * proxmoxCloud product developed. (2017-01-18)
  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 as proxmox;
  21. use MGProvision\Proxmox\v2\interfaces\VmInterface;
  22. class TaskRepository extends AbstractRepository
  23. {
  24. private $nodes;
  25. private $vmids;
  26. private $limit;
  27. private $fromStartime;
  28. public function findByVm(VmInterface $vm)
  29. {
  30. $this->nodes = array($vm->getNode());
  31. $this->vmids = array($vm->getVmid());
  32. return $this;
  33. }
  34. public function findByNodes($nodes)
  35. {
  36. $this->nodes = $nodes;
  37. return $this;
  38. }
  39. public function findByVmids($vmids)
  40. {
  41. $this->vmids = $vmids;
  42. return $this;
  43. }
  44. public function limit($limit)
  45. {
  46. $this->limit = $limit;
  47. return $this;
  48. }
  49. public function fromStartTime($fromStartime)
  50. {
  51. $this->fromStartime = $fromStartime;
  52. return $this;
  53. }
  54. /**
  55. *
  56. * @return proxmox\models\Task[]
  57. */
  58. public function fetch()
  59. {
  60. if (!empty($this->fetch))
  61. {
  62. return $this->fetch;
  63. }
  64. $data = array();
  65. $i = 0;
  66. foreach ($this->nodes as $node)
  67. {
  68. $parameters = array();
  69. if (isset($this->vmids[$i]))
  70. {
  71. $parameters["vmid"] = $this->vmids[$i];
  72. }
  73. if (isset($this->limit))
  74. {
  75. $parameters["limit"] = $this->limit;
  76. }
  77. $collection = $this->api()->get("/nodes/{$node}/tasks", $parameters);
  78. foreach ($collection as $task)
  79. {
  80. if($this->fromStartime && $task['starttime'] < $this->fromStartime ){
  81. continue;
  82. }
  83. $entityObj = new proxmox\models\Task();
  84. $entityObj->setAttributes($task);
  85. $entityObj->setPath("/nodes/{$node}/tasks/" . $task['upid']);
  86. $data[] = $entityObj;
  87. }
  88. $i++;
  89. }
  90. return $data;
  91. }
  92. public function delete()
  93. {
  94. foreach ($this->fetch() as $task)
  95. {
  96. $task->delete();
  97. }
  98. }
  99. }