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\ProxmoxApiException; use MGProvision\Proxmox\v2\models\FirewallRule; use MGProvision\Proxmox\v2\interfaces\VmInterface; use ModulesGarden\ProxmoxAddon\App\Models\VmModel; /** * Description of FirewallRulesRepository * * @author Pawel Kopec * @version 1.0.0 */ class FirewallRulesRepository extends AbstractRepository { protected $path; public function findByPath($path) { if (empty($path)) throw new ProxmoxApiException('[path] - property is missing and it is not optional'); $this->path = [$path]; return $this; } /** * * @param VmModel[] $vservers */ public function findByVmModel($vservers) { $nodes = array(); $this->path = []; foreach ($vservers as $vserver) { if (!$vserver instanceof VmModel) { throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Ukown object: " . get_class($vserver)); } $this->path[] = "/nodes/{$vserver->node}/{$vserver->virtualization}/{$vserver->vmid}/firewall/rules"; } return $this; } public function findByVm(VmInterface $vm) { $this->findByPath($vm->getPath() . '/firewall/rules'); return $this; } /** * * @return FirewallRule[] * @throws ProxmoxApiException */ public function fetch() { $errors = array(); if (empty($this->path)) $errors[] = '[path] - property is missing and it is not optional'; if (!empty($errors)) throw new ProxmoxApiException(implode(", ", $errors)); $results = array(); foreach ($this->path as $path){ $res = $this->api()->get($path); foreach ($res as $r) { $rule = new FirewallRule($r['proto'], $r['enable'], $r['dport'], $r['comment'], $r['action'], $r['iface'], $r['type'], $r['digest'], $r['pos'], $r['sport'], $r['macro'], $r['source'], $r['dest']); $rule->setPath(sprintf("{$path}/%s", $r['pos'])); $results[] = $rule; } } return $results; } public function delete() { for ($i = 0; $i <= 1000; $i++) { $rules = $this->fetch(); if (empty($rules)) break; foreach ($rules as $rule) { $rule->delete(); break; } unset($rules); } } }