SnapshotRepository.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. $nodes = array();
  46. foreach ($vservers as $vserver)
  47. {
  48. if (!$vserver instanceof VmModel)
  49. {
  50. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Ukown object: " . get_class($vserver));
  51. }
  52. $this->paths[] = "/nodes/{$vserver->node}/{$vserver->virtualization}/{$vserver->vmid}/snapshot";
  53. }
  54. return $this;
  55. }
  56. public function reset() {
  57. parent::reset();
  58. $this->ignoreCurrent = false;
  59. $this->paths = array();
  60. return $this;
  61. }
  62. public function ignoreCurrent($ignoreCurrent) {
  63. $this->ignoreCurrent = (boolean) $ignoreCurrent;
  64. return $this;
  65. }
  66. /**
  67. * @return proxmox\models\SnapshotKvm
  68. */
  69. public function getCurrent() {
  70. return $this->current;
  71. }
  72. /**
  73. * @return proxmox\models\SnapshotKvm[]
  74. * @throws proxmox\ProxmoxApiException
  75. */
  76. public function fetch() {
  77. if (!empty($this->fetch)) {
  78. return $this->fetch;
  79. }
  80. foreach ($this->paths as $path) {
  81. $temp = $this->api()->get($path);
  82. foreach ($temp as $snap) {
  83. $entityObj = new Snapshot();
  84. $entityObj->setAttributes($snap);
  85. $entityObj->setPath(sprintf("{$path}/%s", $snap['name']));
  86. if ($this->ignoreCurrent && $snap['name'] == "current") {
  87. $this->current = $entityObj;
  88. continue;
  89. }
  90. $this->fetch[] = $entityObj;
  91. }
  92. }
  93. return $this->fetch;
  94. }
  95. public function sortBySnaptime(){
  96. $keys = [];
  97. $orginal = $this->fetch();
  98. foreach ($orginal as $key => $entity){
  99. $keys[$key] = $entity->getSnaptime();
  100. }
  101. uasort($keys, function($a, $b){
  102. if ($a == $b) {
  103. return 0;
  104. }
  105. return ($a < $b) ? -1 : 1;
  106. });
  107. $this->fetch=[];
  108. foreach ($keys as $id => $snaptime){
  109. $this->fetch[$id] = $orginal[$id];
  110. }
  111. return $this;
  112. }
  113. }