NetworkDeviceLxc.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2016-12-14)
  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. /**
  21. * Description of NetworkDeviceLxc
  22. *
  23. * @author Pawel Kopec <pawelk@modulesgarden.com>
  24. * @version 1.0.0
  25. */
  26. class NetworkDeviceLxc extends AbstractObject
  27. {
  28. protected $id;
  29. protected $name;
  30. protected $bridge;
  31. protected $firewall;
  32. protected $gw;
  33. protected $gw6;
  34. protected $hwaddr;
  35. protected $ip;
  36. protected $cidr;
  37. protected $ip6;
  38. protected $cidr6;
  39. protected $tag;
  40. protected $trunks;
  41. protected $type;
  42. protected $rate;
  43. public function __construct($id=null, $config = null)
  44. {
  45. $this->id = $id;
  46. if ($config !== null)
  47. {
  48. $config = self::asArray($config);
  49. if ($config['ip'] && preg_match("/\\//", $config['ip']))
  50. {
  51. list($ip, $cidr) = explode("/", $config['ip']);
  52. $config['ip'] = $ip;
  53. $config['cidr'] = $cidr;
  54. }
  55. if ($config['ip6'] && preg_match("/\//", $config['ip6']))
  56. {
  57. list($ip, $cidr) = explode("/", $config['ip6']);
  58. $config['ip6'] = $ip;
  59. $config['cidr6'] = $cidr;
  60. }
  61. $this->setAttributes($config);
  62. }
  63. }
  64. public function getId()
  65. {
  66. return $this->id;
  67. }
  68. public function getName()
  69. {
  70. return $this->name;
  71. }
  72. public function getBridge()
  73. {
  74. return $this->bridge;
  75. }
  76. public function getFirewall()
  77. {
  78. return $this->firewall;
  79. }
  80. public function getGw()
  81. {
  82. return $this->gw;
  83. }
  84. public function getHwaddr()
  85. {
  86. return $this->hwaddr;
  87. }
  88. public function getIp()
  89. {
  90. return $this->ip;
  91. }
  92. public function getTag()
  93. {
  94. return $this->tag;
  95. }
  96. public function getType()
  97. {
  98. return $this->type;
  99. }
  100. public function setId($id)
  101. {
  102. $this->id = $id;
  103. return $this;
  104. }
  105. public function setName($name)
  106. {
  107. $this->name = $name;
  108. return $this;
  109. }
  110. public function setBridge($bridge)
  111. {
  112. $this->bridge = $bridge;
  113. return $this;
  114. }
  115. public function setFirewall($firewall)
  116. {
  117. $this->firewall = $firewall;
  118. return $this;
  119. }
  120. public function setGw($gw)
  121. {
  122. $this->gw = $gw;
  123. return $this;
  124. }
  125. public function setHwaddr($hwaddr)
  126. {
  127. $this->hwaddr = $hwaddr;
  128. return $this;
  129. }
  130. public function setIp($ip)
  131. {
  132. $this->ip = $ip;
  133. return $this;
  134. }
  135. public function setTag($tag)
  136. {
  137. $this->tag = $tag;
  138. return $this;
  139. }
  140. public function setType($type)
  141. {
  142. $this->type = $type;
  143. return $this;
  144. }
  145. public function getGw6()
  146. {
  147. return $this->gw6;
  148. }
  149. public function getIp6()
  150. {
  151. return $this->ip6;
  152. }
  153. public function setGw6($gw6)
  154. {
  155. $this->gw6 = $gw6;
  156. return $this;
  157. }
  158. public function setIp6($ip6)
  159. {
  160. $this->ip6 = $ip6;
  161. return $this;
  162. }
  163. public function getTrunks()
  164. {
  165. return $this->trunks;
  166. }
  167. public function setTrunks($trunks)
  168. {
  169. $this->trunks = $trunks;
  170. return $this;
  171. }
  172. public function asConfig()
  173. {
  174. $config = array();
  175. if ($this->name)
  176. $config[] = "name=" . $this->name;
  177. if ($this->bridge)
  178. $config[] = "bridge=" . $this->bridge;
  179. if (!empty($this->hwaddr) && $this->hwaddr != 'auto')
  180. $config[] = 'hwaddr=' . $this->hwaddr;
  181. if ($this->firewall)
  182. $config[] = "firewall=1";
  183. if (!empty($this->tag))
  184. $config[] = 'tag=' . $this->tag;
  185. if (!empty($this->trunks))
  186. $config[] = 'trunks=' . $this->trunks;
  187. if ($this->ip && $this->ip != "dhcp")
  188. {
  189. $config[] = "ip=" . $this->ip . '/' . $this->getCidr();
  190. }
  191. else if ($this->ip && $this->ip == "dhcp")
  192. {
  193. $config[] = "ip=" . $this->ip;
  194. }
  195. if ($this->ip6 && $this->ip6 != "dhcp")
  196. {
  197. $config[] = "ip6=" . $this->ip6 . '/' . $this->getCidr6();
  198. }
  199. else if ($this->ip6 && $this->ip6 == "dhcp")
  200. {
  201. $config[] = "ip6=" . $this->ip6;
  202. }
  203. if ($this->gw && filter_var($this->gw, FILTER_VALIDATE_IP))
  204. $config[] = "gw=" . $this->gw;
  205. if ($this->gw6 && filter_var($this->gw6, FILTER_VALIDATE_IP))
  206. $config[] = "gw6=" . $this->gw6;
  207. if ($this->rate)
  208. $config[] = "rate=" . $this->rate;
  209. return implode(",", $config);
  210. }
  211. public function getRate()
  212. {
  213. return $this->rate;
  214. }
  215. public function setRate($rate)
  216. {
  217. $this->rate = $rate;
  218. return $this;
  219. }
  220. public function getCidr()
  221. {
  222. return $this->cidr;
  223. }
  224. public function getCidr6()
  225. {
  226. return $this->cidr6;
  227. }
  228. public function setCidr($cidr)
  229. {
  230. $this->cidr = $cidr;
  231. return $this;
  232. }
  233. public function setCidr6($cidr6)
  234. {
  235. $this->cidr6 = $cidr6;
  236. return $this;
  237. }
  238. public function getRateMbps()
  239. {
  240. return $this->rate * 8;
  241. }
  242. public function remove($ip)
  243. {
  244. if (!preg_match('/\:/', $ip) && $this->getIp() == $ip)
  245. {//v4
  246. $this->setIp(null)
  247. ->setCidr(null)
  248. ->setGw(null);
  249. return true;
  250. }
  251. else if ($this->getIp6() == $ip)
  252. {//v6
  253. $this->setIp6(null)
  254. ->setCidr6(null)
  255. ->setGw6(null);
  256. return true;
  257. }
  258. }
  259. public function getAttributes(){
  260. return [
  261. "id" => $this->getId(),
  262. "name" => $this->getName(),
  263. "bridge" => $this->getBridge(),
  264. "firewall" => $this->getFirewall(),
  265. "hwaddr" => $this->getHwaddr(),
  266. "ip" => $this->getIp(),
  267. "cidr" => $this->getCidr(),
  268. "gw" => $this->getGw(),
  269. "ip6" => $this->getIp6(),
  270. "cidr6" => $this->getCidr6(),
  271. "gw6" => $this->getGw6(),
  272. "tag" => $this->getTag(),
  273. "rate" => $this->getRate(),
  274. ];
  275. }
  276. public function isEmpty()
  277. {
  278. return ( ($this->getIp() === null || $this->getIp() === 'dhcp' ) && ( $this->getIp6() === null || $this->getIp6() === 'dhcp' ) );
  279. }
  280. }