FirewallRulesRepository.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2016-12-13)
  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\ProxmoxApiException;
  21. use MGProvision\Proxmox\v2\models\FirewallRule;
  22. use MGProvision\Proxmox\v2\interfaces\VmInterface;
  23. use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
  24. /**
  25. * Description of FirewallRulesRepository
  26. *
  27. * @author Pawel Kopec <pawelk@modulesgarden.com>
  28. * @version 1.0.0
  29. */
  30. class FirewallRulesRepository extends AbstractRepository
  31. {
  32. protected $path;
  33. public function findByPath($path)
  34. {
  35. if (empty($path))
  36. throw new ProxmoxApiException('[path] - property is missing and it is not optional');
  37. $this->path = [$path];
  38. return $this;
  39. }
  40. /**
  41. *
  42. * @param VmModel[] $vservers
  43. */
  44. public function findByVmModel($vservers)
  45. {
  46. $nodes = array();
  47. $this->path = [];
  48. foreach ($vservers as $vserver)
  49. {
  50. if (!$vserver instanceof VmModel)
  51. {
  52. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Ukown object: " . get_class($vserver));
  53. }
  54. $this->path[] = "/nodes/{$vserver->node}/{$vserver->virtualization}/{$vserver->vmid}/firewall/rules";
  55. }
  56. return $this;
  57. }
  58. public function findByVm(VmInterface $vm)
  59. {
  60. $this->findByPath($vm->getPath() . '/firewall/rules');
  61. return $this;
  62. }
  63. /**
  64. *
  65. * @return FirewallRule[]
  66. * @throws ProxmoxApiException
  67. */
  68. public function fetch()
  69. {
  70. $errors = array();
  71. if (empty($this->path))
  72. $errors[] = '[path] - property is missing and it is not optional';
  73. if (!empty($errors))
  74. throw new ProxmoxApiException(implode(", ", $errors));
  75. $results = array();
  76. foreach ($this->path as $path){
  77. $res = $this->api()->get($path);
  78. foreach ($res as $r)
  79. {
  80. $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']);
  81. $rule->setPath(sprintf("{$path}/%s", $r['pos']));
  82. $results[] = $rule;
  83. }
  84. }
  85. return $results;
  86. }
  87. public function delete()
  88. {
  89. for ($i = 0; $i <= 1000; $i++)
  90. {
  91. $rules = $this->fetch();
  92. if (empty($rules))
  93. break;
  94. foreach ($rules as $rule)
  95. {
  96. $rule->delete();
  97. break;
  98. }
  99. unset($rules);
  100. }
  101. }
  102. public function fetchAsArray()
  103. {
  104. $data = [];
  105. foreach ($this->fetch() as $rule){
  106. $row = $rule->toArray();
  107. unset($row['digest'], $row['path'], $row['api']);
  108. $data[] = $row;
  109. }
  110. return $data;
  111. }
  112. }