SnapshotRepository.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /* * ********************************************************************
  3. * proxmoxCloudVps product developed. (2017-01-20)
  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. use MGProvision\Proxmox\v2\models\Snapshot;
  23. use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
  24. /**
  25. * Description of SnapshotRepository
  26. *
  27. * @author Pawel Kopec <pawelk@modulesgarden.com>
  28. * @version 1.0.0
  29. */
  30. class SnapshotRepository extends AbstractRepository
  31. {
  32. private $paths = array();
  33. private $ignoreCurrent = false;
  34. private $current;
  35. public function findByVm(VmInterface $vm)
  36. {
  37. $this->paths[] = "/nodes/{$vm->getNode()}/{$vm->getVirtualization()}/{$vm->getVmid()}/snapshot";
  38. return $this;
  39. }
  40. /**
  41. *
  42. * @param VmModel[] $vservers
  43. */
  44. public function findByVmModel($vservers)
  45. {
  46. $nodes = array();
  47. foreach ($vservers as $vserver)
  48. {
  49. if (!$vserver instanceof VmModel)
  50. {
  51. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Ukown object: " . get_class($vserver));
  52. }
  53. $this->paths[] = "/nodes/{$vserver->node}/{$vserver->virtualization}/{$vserver->vmid}/snapshot";
  54. }
  55. return $this;
  56. }
  57. public function reset()
  58. {
  59. parent::reset();
  60. $this->ignoreCurrent = false;
  61. $this->paths = array();
  62. return $this;
  63. }
  64. public function ignoreCurrent($ignoreCurrent)
  65. {
  66. $this->ignoreCurrent = (boolean) $ignoreCurrent;
  67. return $this;
  68. }
  69. /**
  70. * @return proxmox\models\SnapshotKvm
  71. */
  72. public function getCurrent()
  73. {
  74. return $this->current;
  75. }
  76. /**
  77. * @return proxmox\models\SnapshotKvm[]
  78. * @throws proxmox\ProxmoxApiException
  79. */
  80. public function fetch()
  81. {
  82. if (!empty($this->fetch))
  83. {
  84. return $this->fetch;
  85. }
  86. foreach ($this->paths as $path)
  87. {
  88. $temp = $this->api()->get($path);
  89. foreach ($temp as $snap)
  90. {
  91. $entityObj = new Snapshot();
  92. $entityObj->setAttributes($snap);
  93. $entityObj->setPath(sprintf("{$path}/%s", $snap['name']));
  94. if ($this->ignoreCurrent && $snap['name'] == "current")
  95. {
  96. $this->current = $entityObj;
  97. continue;
  98. }
  99. $this->fetch[] = $entityObj;
  100. }
  101. }
  102. return $this->fetch;
  103. }
  104. public function sortBySnaptime(){
  105. $keys = [];
  106. $orginal = $this->fetch();
  107. foreach ($orginal as $key => $entity){
  108. $keys[$key] = $entity->getSnaptime();
  109. }
  110. uasort($keys, function($a, $b){
  111. if ($a == $b) {
  112. return 0;
  113. }
  114. return ($a < $b) ? -1 : 1;
  115. });
  116. $this->fetch=[];
  117. foreach ($keys as $id => $snaptime){
  118. $this->fetch[$id] = $orginal[$id];
  119. }
  120. return $this;
  121. }
  122. }