http://modulesgarden.com * CONTACT -> contact@modulesgarden.com * * * This software is furnished under a license and may be used and copied * only in accordance with the terms of such license and with the * inclusion of the above copyright notice. This software or any other * copies thereof may not be provided or otherwise made available to any * other person. No title to and ownership of the software is hereby * transferred. * * * ******************************************************************** */ namespace MGProvision\Proxmox\v2\models; /** * Description of NetworkDeviceLxc * * @author Pawel Kopec * @version 1.0.0 */ class NetworkDeviceLxc extends AbstractObject { protected $id; protected $name; protected $bridge; protected $firewall; protected $gw; protected $gw6; protected $hwaddr; protected $ip; protected $cidr; protected $ip6; protected $cidr6; protected $tag; protected $trunks; protected $type; protected $rate; public function __construct($id=null, $config = null) { $this->id = $id; if ($config !== null) { $config = self::asArray($config); if ($config['ip'] && preg_match("/\\//", $config['ip'])) { list($ip, $cidr) = explode("/", $config['ip']); $config['ip'] = $ip; $config['cidr'] = $cidr; } if ($config['ip6'] && preg_match("/\//", $config['ip6'])) { list($ip, $cidr) = explode("/", $config['ip6']); $config['ip6'] = $ip; $config['cidr6'] = $cidr; } $this->setAttributes($config); } } public function getId() { return $this->id; } public function getName() { return $this->name; } public function getBridge() { return $this->bridge; } public function getFirewall() { return $this->firewall; } public function getGw() { return $this->gw; } public function getHwaddr() { return $this->hwaddr; } public function getIp() { return $this->ip; } public function getTag() { return $this->tag; } public function getType() { return $this->type; } public function setId($id) { $this->id = $id; return $this; } public function setName($name) { $this->name = $name; return $this; } public function setBridge($bridge) { $this->bridge = $bridge; return $this; } public function setFirewall($firewall) { $this->firewall = $firewall; return $this; } public function setGw($gw) { $this->gw = $gw; return $this; } public function setHwaddr($hwaddr) { $this->hwaddr = $hwaddr; return $this; } public function setIp($ip) { $this->ip = $ip; return $this; } public function setTag($tag) { $this->tag = $tag; return $this; } public function setType($type) { $this->type = $type; return $this; } public function getGw6() { return $this->gw6; } public function getIp6() { return $this->ip6; } public function setGw6($gw6) { $this->gw6 = $gw6; return $this; } public function setIp6($ip6) { $this->ip6 = $ip6; return $this; } public function getTrunks() { return $this->trunks; } public function setTrunks($trunks) { $this->trunks = $trunks; return $this; } public function asConfig() { $config = array(); if ($this->name) $config[] = "name=" . $this->name; if ($this->bridge) $config[] = "bridge=" . $this->bridge; if (!empty($this->hwaddr) && $this->hwaddr != 'auto') $config[] = 'hwaddr=' . $this->hwaddr; if ($this->firewall) $config[] = "firewall=1"; if (!empty($this->tag)) $config[] = 'tag=' . $this->tag; if (!empty($this->trunks)) $config[] = 'trunks=' . $this->trunks; if ($this->ip && $this->ip != "dhcp") { $config[] = "ip=" . $this->ip . '/' . $this->getCidr(); } else if ($this->ip && $this->ip == "dhcp") { $config[] = "ip=" . $this->ip; } if ($this->ip6 && $this->ip6 != "dhcp") { $config[] = "ip6=" . $this->ip6 . '/' . $this->getCidr6(); } else if ($this->ip6 && $this->ip6 == "dhcp") { $config[] = "ip6=" . $this->ip6; } if ($this->gw && filter_var($this->gw, FILTER_VALIDATE_IP)) $config[] = "gw=" . $this->gw; if ($this->gw6 && filter_var($this->gw6, FILTER_VALIDATE_IP)) $config[] = "gw6=" . $this->gw6; if ($this->rate) $config[] = "rate=" . $this->rate; return implode(",", $config); } public function getRate() { return $this->rate; } public function setRate($rate) { $this->rate = $rate; return $this; } public function getCidr() { return $this->cidr; } public function getCidr6() { return $this->cidr6; } public function setCidr($cidr) { $this->cidr = $cidr; return $this; } public function setCidr6($cidr6) { $this->cidr6 = $cidr6; return $this; } public function getRateMbps() { return $this->rate * 8; } public function remove($ip) { if (!preg_match('/\:/', $ip) && $this->getIp() == $ip) {//v4 $this->setIp(null) ->setCidr(null) ->setGw(null); return true; } else if ($this->getIp6() == $ip) {//v6 $this->setIp6(null) ->setCidr6(null) ->setGw6(null); return true; } } public function getAttributes(){ return [ "id" => $this->getId(), "name" => $this->getName(), "bridge" => $this->getBridge(), "firewall" => $this->getFirewall(), "hwaddr" => $this->getHwaddr(), "ip" => $this->getIp(), "cidr" => $this->getCidr(), "gw" => $this->getGw(), "ip6" => $this->getIp6(), "cidr6" => $this->getCidr6(), "gw6" => $this->getGw6(), "tag" => $this->getTag(), "rate" => $this->getRate(), ]; } public function isEmpty() { return ( ($this->getIp() === null || $this->getIp() === 'dhcp' ) && ( $this->getIp6() === null || $this->getIp6() === 'dhcp' ) ); } }