NetworkDeviceKvm.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2016-12-08)
  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 KvmNetworkDevice
  22. *
  23. * @author Pawel Kopec <pawelk@modulesgarden.com>
  24. * @version 1.0.0
  25. */
  26. class NetworkDeviceKvm extends AbstractObject
  27. {
  28. protected $id;
  29. protected $model;
  30. protected $macAddress;
  31. protected $bridge;
  32. protected $firewall;
  33. protected $linkDown;
  34. protected $queues;
  35. protected $rate;
  36. protected $tag;
  37. protected $trunks;
  38. protected $node;
  39. protected $vmid;
  40. public function __construct($id=null, $config = null)
  41. {
  42. $this->id = $id;
  43. if ($config !== null)
  44. {
  45. $config = self::asArray($config);
  46. $model = array_search(current($config), $config);
  47. $this->setBridge($config['bridge'])
  48. ->setRate($config['rate'])
  49. ->setMacAddress($config)
  50. ->setModel($model)
  51. ->setTag($config['tag'])
  52. ->setTrunks($config['trunks'])
  53. ->setFirewall($config['firewall'])
  54. ->setQueues($config['queues']);
  55. }
  56. }
  57. public function getId()
  58. {
  59. return $this->id;
  60. }
  61. public function getModel()
  62. {
  63. return $this->model;
  64. }
  65. public function getMacAddress()
  66. {
  67. return $this->macAddress;
  68. }
  69. public function getBridge()
  70. {
  71. return $this->bridge;
  72. }
  73. public function getFirewall()
  74. {
  75. return $this->firewall;
  76. }
  77. public function getLinkDown()
  78. {
  79. return $this->linkDown;
  80. }
  81. public function getQueues()
  82. {
  83. return $this->queues;
  84. }
  85. public function getRate()
  86. {
  87. return $this->rate;
  88. }
  89. public function getTag()
  90. {
  91. return $this->tag;
  92. }
  93. public function getNode()
  94. {
  95. return $this->node;
  96. }
  97. public function getVmid()
  98. {
  99. return $this->vmid;
  100. }
  101. public function setId($id)
  102. {
  103. $this->id = $id;
  104. return $this;
  105. }
  106. public function setModel($model)
  107. {
  108. $this->model = $model;
  109. return $this;
  110. }
  111. public function setMacAddress($macAddress)
  112. {
  113. if (is_array($macAddress))
  114. {
  115. $models = array('e1000', 'i82551', 'i82557b', 'i82559er', 'ne2k_isa', 'ne2k_pci', 'pcnet', 'rtl8139', 'virtio', 'vmxnet3');
  116. $config = $macAddress;
  117. foreach ($config as $k => $v)
  118. {
  119. foreach ($models as $model)
  120. {
  121. if (preg_match("/{$model}/", $k))
  122. {
  123. $macAddress = $v;
  124. break;
  125. }
  126. }
  127. }
  128. }
  129. if($macAddress =="auto"){
  130. $macAddress = null;
  131. }
  132. if ($macAddress && !preg_match("/^[0-9a-fA-F]{2}(?=([:;.]?))(?:\\1[0-9a-fA-F]{2}){5}$/", $macAddress))
  133. throw new \MGProvision\Proxmox\v2\ProxmoxApiException(sprintf("Invalid MAC Address ('%s') ", $macAddress));
  134. $this->macAddress = $macAddress;
  135. return $this;
  136. }
  137. public function setBridge($bridge)
  138. {
  139. $this->bridge = $bridge;
  140. return $this;
  141. }
  142. public function setFirewall($firewall)
  143. {
  144. $this->firewall = $firewall;
  145. return $this;
  146. }
  147. public function setLinkDown($linkDown)
  148. {
  149. $this->linkDown = $linkDown;
  150. return $this;
  151. }
  152. public function setQueues($queues)
  153. {
  154. $this->queues = $queues;
  155. return $this;
  156. }
  157. public function setRate($rate)
  158. {
  159. $this->rate = $rate;
  160. return $this;
  161. }
  162. public function setTag($tag)
  163. {
  164. $this->tag = $tag;
  165. return $this;
  166. }
  167. public function setNode($node)
  168. {
  169. $this->node = $node;
  170. return $this;
  171. }
  172. public function setVmid($vmid)
  173. {
  174. $this->vmid = $vmid;
  175. return $this;
  176. }
  177. public function getTrunks()
  178. {
  179. return $this->trunks;
  180. }
  181. public function setTrunks($trunks)
  182. {
  183. $this->trunks = $trunks;
  184. return $this;
  185. }
  186. public function asConfig()
  187. {
  188. $config = array();
  189. if ($this->macAddress)
  190. $config[] = $this->model . "=" . $this->macAddress;
  191. else if ($this->model)
  192. $config[] = $this->model;
  193. if ($this->bridge)
  194. $config[] = "bridge=" . $this->bridge;
  195. if ($this->firewall)
  196. $config[] = "firewall=1";
  197. if (!empty($this->linkDown))
  198. $config[] = 'link_down=' . $this->linkDown;
  199. if (!empty($this->tag))
  200. $config[] = 'tag=' . $this->tag;
  201. if (!empty($this->trunks))
  202. $config[] = 'trunks=' . $this->trunks;
  203. if ($this->rate)
  204. $config[] = "rate=" . $this->rate;
  205. if(!empty($this->queues)){
  206. $config[] = "queues=" . $this->queues;
  207. }
  208. return implode(",", $config);
  209. }
  210. public function getRateMbps()
  211. {
  212. return $this->rate * 8;
  213. }
  214. public function getAttributes(){
  215. return [
  216. "id" => $this->getId(),
  217. "bridge" => $this->getBridge(),
  218. "firewall" => $this->getFirewall(),
  219. "tag" => $this->getTag(),
  220. "macAddress" => $this->getMacAddress(),
  221. "rate" => $this->getRate(),
  222. "queues" => $this->getQueues()
  223. ];
  224. }
  225. }