FirewallRulesRepository.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. logModuleCall(
  49. 'ProxmoxCloud',
  50. __FUNCTION__,
  51. $vservers,
  52. 'Debug',
  53. $this
  54. );
  55. foreach ($vservers as $vserver)
  56. {
  57. if (!$vserver instanceof VmModel)
  58. {
  59. throw new \MGProvision\Proxmox\v2\ProxmoxApiException("Ukown object: " . get_class($vserver));
  60. }
  61. $this->path[] = "/nodes/{$vserver->node}/{$vserver->virtualization}/{$vserver->vmid}/firewall/rules";
  62. }
  63. return $this;
  64. }
  65. public function findByVm(VmInterface $vm)
  66. {
  67. $this->findByPath($vm->getPath() . '/firewall/rules');
  68. return $this;
  69. }
  70. /**
  71. *
  72. * @return FirewallRule[]
  73. * @throws ProxmoxApiException
  74. */
  75. public function fetch()
  76. {
  77. $errors = array();
  78. if (empty($this->path))
  79. $errors[] = '[path] - property is missing and it is not optional';
  80. if (!empty($errors))
  81. throw new ProxmoxApiException(implode(", ", $errors));
  82. $results = array();
  83. foreach ($this->path as $path){
  84. $res = $this->api()->get($path);
  85. foreach ($res as $r)
  86. {
  87. $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']);
  88. $rule->setPath(sprintf("{$path}/%s", $r['pos']));
  89. $results[] = $rule;
  90. }
  91. }
  92. return $results;
  93. }
  94. public function delete()
  95. {
  96. for ($i = 0; $i <= 1000; $i++)
  97. {
  98. $rules = $this->fetch();
  99. if (empty($rules))
  100. break;
  101. foreach ($rules as $rule)
  102. {
  103. $rule->delete();
  104. break;
  105. }
  106. unset($rules);
  107. }
  108. }
  109. public function fetchAsArray()
  110. {
  111. $data = [];
  112. foreach ($this->fetch() as $rule){
  113. $row = $rule->toArray();
  114. unset($row['digest'], $row['path'], $row['api']);
  115. $data[] = $row;
  116. }
  117. return $data;
  118. }
  119. }