| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- /* * ********************************************************************
- * ProxmoxVPS product developed. (2016-12-13)
- * *
- *
- * 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\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 <pawelk@modulesgarden.com>
- * @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);
- }
- }
- public function fetchAsArray()
- {
- $data = [];
- foreach ($this->fetch() as $rule){
- $row = $rule->toArray();
- unset($row['digest'], $row['path'], $row['api']);
- $data[] = $row;
- }
- return $data;
- }
- }
|