| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- /* * ********************************************************************
- * proxmoxCloudVps product developed. (2017-01-20)
- * *
- *
- * CREATED BY MODULESGARDEN -> http://modulesgarden.com
- * CONTACT -> contact@modulesgarden.com
- *
- *
- * This software is furnished under a license and may be used and copied
- * only in accordance with the terms of such license and with the
- * inclusion of the above copyright notice. This software or any other
- * copies thereof may not be provided or otherwise made available to any
- * other person. No title to and ownership of the software is hereby
- * transferred.
- *
- *
- * ******************************************************************** */
- namespace MGProvision\Proxmox\v2\repository;
- use MGProvision\Proxmox\v2 as proxmox;
- use MGProvision\Proxmox\v2\interfaces\VmInterface;
- use MGProvision\Proxmox\v2\models\Snapshot;
- use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
- /**
- * Description of SnapshotRepository
- *
- * @author Pawel Kopec <pawelk@modulesgarden.com>
- * @version 1.0.0
- */
- class SnapshotRepository extends AbstractRepository
- {
- private $paths = array();
- private $ignoreCurrent = false;
- private $current;
- public function findByVm(VmInterface $vm)
- {
- $this->paths[] = "/nodes/{$vm->getNode()}/{$vm->getVirtualization()}/{$vm->getVmid()}/snapshot";
- return $this;
- }
- /**
- *
- * @param VmModel[] $vservers
- */
- public function findByVmModel($vservers)
- {
- $nodes = array();
- foreach ($vservers as $vserver)
- {
- if (!$vserver instanceof VmModel)
- {
- throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Ukown object: " . get_class($vserver));
- }
- $this->paths[] = "/nodes/{$vserver->node}/{$vserver->virtualization}/{$vserver->vmid}/snapshot";
- }
- return $this;
- }
- public function reset()
- {
- parent::reset();
- $this->ignoreCurrent = false;
- $this->paths = array();
- return $this;
- }
- public function ignoreCurrent($ignoreCurrent)
- {
- $this->ignoreCurrent = (boolean) $ignoreCurrent;
- return $this;
- }
- /**
- * @return proxmox\models\SnapshotKvm
- */
- public function getCurrent()
- {
- return $this->current;
- }
- /**
- * @return proxmox\models\SnapshotKvm[]
- * @throws proxmox\ProxmoxApiException
- */
- public function fetch()
- {
- if (!empty($this->fetch))
- {
- return $this->fetch;
- }
- foreach ($this->paths as $path)
- {
- $temp = $this->api()->get($path);
- foreach ($temp as $snap)
- {
- $entityObj = new Snapshot();
- $entityObj->setAttributes($snap);
- $entityObj->setPath(sprintf("{$path}/%s", $snap['name']));
- if ($this->ignoreCurrent && $snap['name'] == "current")
- {
- $this->current = $entityObj;
- continue;
- }
- $this->fetch[] = $entityObj;
- }
- }
- return $this->fetch;
- }
- public function sortBySnaptime(){
- $keys = [];
- $orginal = $this->fetch();
- foreach ($orginal as $key => $entity){
- $keys[$key] = $entity->getSnaptime();
- }
- uasort($keys, function($a, $b){
- if ($a == $b) {
- return 0;
- }
- return ($a < $b) ? -1 : 1;
- });
- $this->fetch=[];
- foreach ($keys as $id => $snaptime){
- $this->fetch[$id] = $orginal[$id];
- }
- return $this;
- }
- }
|