IpSet.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\models;
  20. use \MGProvision\Proxmox\v2\ProxmoxApiException;
  21. use \MGProvision\Proxmox\v2\repository\IpCidrRepository;
  22. /**
  23. * Description of IpSet
  24. *
  25. * @author Pawel Kopec <pawelk@modulesgardne.com>
  26. */
  27. class IpSet extends AbstractObject
  28. {
  29. const DEFAULT_PATH = 'firewall/ipset';
  30. private $path;
  31. protected $name;
  32. protected $comment;
  33. private $ipCidr;
  34. function __construct($path = null)
  35. {
  36. if (!is_null($path))
  37. {
  38. $this->setPath($path);
  39. }
  40. }
  41. public function getName()
  42. {
  43. return $this->name;
  44. }
  45. public function getComment()
  46. {
  47. return $this->comment;
  48. }
  49. public function setName($name)
  50. {
  51. $this->name = $name;
  52. return $this;
  53. }
  54. public function setComment($comment)
  55. {
  56. $this->comment = $comment;
  57. return $this;
  58. }
  59. /**
  60. *
  61. * @param string $path /api2/json/nodes/{node}/qemu/{vmid}/firewall/ipset
  62. * @throws proxmox\ProxmoxApiException
  63. */
  64. public function setPath($path)
  65. {
  66. if (!preg_match('/ipset/', $path))
  67. {
  68. throw new ProxmoxApiException(sprintf("IpSet path ('%s') is not valid", $path));
  69. }
  70. $this->path = $path;
  71. return $this;
  72. }
  73. public function create()
  74. {
  75. if (empty($this->path))
  76. {
  77. throw new ProxmoxApiException("IpSet path is empty");
  78. }
  79. $this->api()->post($this->path, ['name' => $this->name, "comment" => $this->comment]);
  80. $this->setPath($this->path . "/{$this->name}");
  81. return $this;
  82. }
  83. public function delete()
  84. {
  85. if (empty($this->path))
  86. {
  87. throw new ProxmoxApiException("IpSet path is empty");
  88. }
  89. $this->api()->delete($this->path);
  90. return $this;
  91. }
  92. public function getPath()
  93. {
  94. return $this->path;
  95. }
  96. /**
  97. *
  98. * @return IpCidr[]
  99. */
  100. public function getIpCidr()
  101. {
  102. if ($this->ipCidr)
  103. {
  104. return $this->ipCidr;
  105. }
  106. $this->ipCidr = (new IpCidrRepository())->findByPath($this->getPath())->fetch();
  107. return $this->ipCidr;
  108. }
  109. }