IpSetRepository.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Mar 9, 2018)
  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\IpSet;
  22. use \MGProvision\Proxmox\v2\interfaces\VmInterface;
  23. /**
  24. * Description of IpSetRepository
  25. *
  26. * @author Pawel Kopec <pawelk@modulesgardne.com>
  27. */
  28. class IpSetRepository extends AbstractRepository
  29. {
  30. protected $path;
  31. public function findByPath($path)
  32. {
  33. if (!preg_match('/ipset/', $path))
  34. {
  35. throw new ProxmoxApiException(sprintf("IpSet path ('%s') is not valid", $path));
  36. }
  37. $this->path = $path;
  38. return $this;
  39. }
  40. public function findByVm(VmInterface $vm)
  41. {
  42. $this->findByPath($vm->getPath() . '/firewall/ipset');
  43. return $this;
  44. }
  45. /**
  46. *
  47. * @return IpSet[]
  48. * @throws ProxmoxApiException
  49. */
  50. public function fetch()
  51. {
  52. if ($this->fetch)
  53. {
  54. return $this->fetch;
  55. }
  56. if (empty($this->path))
  57. {
  58. throw new ProxmoxApiException("IpSet path is empty");
  59. }
  60. $this->fetch = [];
  61. $ipSet = $this->api()->get($this->path);
  62. krsort($ipSet);
  63. foreach ($ipSet as $entity)
  64. {
  65. $this->fetch[] = (new IpSet("{$this->path}/{$entity['name']}"))->setAttributes($entity);
  66. }
  67. return $this->fetch;
  68. }
  69. public function find(IpSet $ipSet)
  70. {
  71. foreach ($this->fetch() as $cIpSet)
  72. {
  73. if ($cIpSet->getName() == $ipSet->getName())
  74. {
  75. return $cIpSet;
  76. }
  77. }
  78. return null;
  79. }
  80. }