1: <?php
2:
3: namespace Saklient\Cloud\Resources;
4:
5: require_once __DIR__ . "/../../../Saklient/Util.php";
6: use \Saklient\Util;
7: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/LbServer.php";
8: use \Saklient\Cloud\Resources\LbServer;
9: require_once __DIR__ . "/../../../Saklient/Errors/SaklientException.php";
10: use \Saklient\Errors\SaklientException;
11:
12: 13: 14: 15: 16: 17: 18: 19:
20: class LbVirtualIp {
21:
22: 23: 24: 25: 26: 27:
28: protected $_virtualIpAddress;
29:
30: 31: 32: 33: 34:
35: public function get_virtualIpAddress()
36: {
37: return $this->_virtualIpAddress;
38: }
39:
40: 41: 42: 43: 44: 45:
46: public function set_virtualIpAddress($v)
47: {
48: Util::validateArgCount(func_num_args(), 1);
49: Util::validateType($v, "string");
50: $this->_virtualIpAddress = $v;
51: return $this->_virtualIpAddress;
52: }
53:
54:
55:
56: 57: 58: 59: 60: 61:
62: protected $_port;
63:
64: 65: 66: 67: 68:
69: public function get_port()
70: {
71: return $this->_port;
72: }
73:
74: 75: 76: 77: 78: 79:
80: public function set_port($v)
81: {
82: Util::validateArgCount(func_num_args(), 1);
83: Util::validateType($v, "int");
84: $this->_port = $v;
85: return $this->_port;
86: }
87:
88:
89:
90: 91: 92: 93: 94: 95:
96: protected $_delayLoop;
97:
98: 99: 100: 101: 102:
103: public function get_delayLoop()
104: {
105: return $this->_delayLoop;
106: }
107:
108: 109: 110: 111: 112: 113:
114: public function set_delayLoop($v)
115: {
116: Util::validateArgCount(func_num_args(), 1);
117: Util::validateType($v, "int");
118: $this->_delayLoop = $v;
119: return $this->_delayLoop;
120: }
121:
122:
123:
124: 125: 126: 127: 128: 129:
130: protected $_servers;
131:
132: 133: 134: 135: 136:
137: public function get_servers()
138: {
139: return $this->_servers;
140: }
141:
142:
143:
144: 145: 146: 147: 148:
149: public function __construct($obj=null)
150: {
151: if ($obj == null) {
152: $obj = (object)[];
153: }
154: $vip = Util::getByPathAny(new \ArrayObject([$obj]), new \ArrayObject([
155: "VirtualIPAddress",
156: "virtualIpAddress",
157: "virtual_ip_address",
158: "vip"
159: ]));
160: $this->_virtualIpAddress = $vip;
161: $port = Util::getByPathAny(new \ArrayObject([$obj]), new \ArrayObject(["Port", "port"]));
162: $this->_port = null;
163: if ($port != null) {
164: $this->_port = intval($port);
165: }
166: if ($this->_port == 0) {
167: $this->_port = null;
168: }
169: $delayLoop = Util::getByPathAny(new \ArrayObject([$obj]), new \ArrayObject([
170: "DelayLoop",
171: "delayLoop",
172: "delay_loop",
173: "delay"
174: ]));
175: $this->_delayLoop = null;
176: if ($delayLoop != null) {
177: $this->_delayLoop = intval($delayLoop);
178: }
179: if ($this->_delayLoop == 0) {
180: $this->_delayLoop = null;
181: }
182: $this->_servers = new \ArrayObject([]);
183: $serversDyn = Util::getByPathAny(new \ArrayObject([$obj]), new \ArrayObject(["Servers", "servers"]));
184: if ($serversDyn != null) {
185: $servers = $serversDyn;
186: foreach ($servers as $server) {
187: $this->_servers->append(new LbServer($server));
188: }
189: }
190: }
191:
192: 193: 194: 195: 196: 197: 198:
199: public function addServer($settings=null)
200: {
201: $ret = new LbServer($settings);
202: $this->_servers->append($ret);
203: return $ret;
204: }
205:
206: 207: 208: 209:
210: public function toRawSettings()
211: {
212: $servers = new \ArrayObject([]);
213: foreach ($this->_servers as $server) {
214: $servers->append($server->toRawSettings());
215: }
216: return (object)[
217: 'VirtualIPAddress' => $this->_virtualIpAddress,
218: 'Port' => $this->_port,
219: 'DelayLoop' => $this->_delayLoop,
220: 'Servers' => $servers
221: ];
222: }
223:
224: 225: 226: 227: 228: 229: 230:
231: public function getServerByAddress($address)
232: {
233: Util::validateArgCount(func_num_args(), 1);
234: Util::validateType($address, "string");
235: foreach ($this->_servers as $srv) {
236: if ($srv->ipAddress == $address) {
237: return $srv;
238: }
239: }
240: return null;
241: }
242:
243: 244: 245: 246: 247: 248: 249:
250: public function removeServerByAddress($address)
251: {
252: Util::validateArgCount(func_num_args(), 1);
253: Util::validateType($address, "string");
254: $servers = new \ArrayObject([]);
255: foreach ($this->_servers as $srv) {
256: if ($srv->ipAddress != $address) {
257: $servers->append($srv);
258: }
259: }
260: $this->_servers = $servers;
261: return $this;
262: }
263:
264: 265: 266: 267: 268: 269:
270: public function updateStatus($srvs)
271: {
272: Util::validateArgCount(func_num_args(), 1);
273: Util::validateType($srvs, "\\ArrayObject");
274: foreach ($srvs as $srvDyn) {
275: $ip = $srvDyn->{"IPAddress"};
276: $srv = $this->getServerByAddress($ip);
277: if ($srv == null) {
278: continue;
279: }
280: $srv->updateStatus($srvDyn);
281: }
282: return $this;
283: }
284:
285: 286: 287:
288: public function __get($key) {
289: switch ($key) {
290: case "virtualIpAddress": return $this->get_virtualIpAddress();
291: case "port": return $this->get_port();
292: case "delayLoop": return $this->get_delayLoop();
293: case "servers": return $this->get_servers();
294: default: return null;
295: }
296: }
297:
298: 299: 300:
301: public function __set($key, $v) {
302: switch ($key) {
303: case "virtualIpAddress": return $this->set_virtualIpAddress($v);
304: case "port": return $this->set_port($v);
305: case "delayLoop": return $this->set_delayLoop($v);
306: default: throw new SaklientException('non_writable_field', 'Non-writable field: Saklient\\Cloud\\Resources\\LbVirtualIp#'.$key);
307: }
308: }
309:
310: }
311:
312: