1: <?php
2:
3: namespace Saklient\Cloud\Resources;
4:
5: require_once __DIR__ . "/../../../Saklient/Errors/SaklientException.php";
6: use \Saklient\Errors\SaklientException;
7: require_once __DIR__ . "/../../../Saklient/Cloud/Client.php";
8: use \Saklient\Cloud\Client;
9: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Appliance.php";
10: use \Saklient\Cloud\Resources\Appliance;
11: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/LbVirtualIp.php";
12: use \Saklient\Cloud\Resources\LbVirtualIp;
13: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Swytch.php";
14: use \Saklient\Cloud\Resources\Swytch;
15: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Ipv4Net.php";
16: use \Saklient\Cloud\Resources\Ipv4Net;
17: require_once __DIR__ . "/../../../Saklient/Cloud/Enums/EApplianceClass.php";
18: use \Saklient\Cloud\Enums\EApplianceClass;
19: require_once __DIR__ . "/../../../Saklient/Util.php";
20: use \Saklient\Util;
21:
22: 23: 24: 25: 26: 27: 28: 29:
30: class LoadBalancer extends Appliance {
31:
32: 33: 34: 35: 36: 37:
38: protected $_virtualIps;
39:
40: 41: 42: 43: 44:
45: public function get_virtualIps()
46: {
47: return $this->_virtualIps;
48: }
49:
50:
51:
52: 53: 54: 55: 56:
57: public function get_defaultRoute()
58: {
59: return Util::getByPath($this->rawAnnotation, "Network.DefaultRoute");
60: }
61:
62: 63: 64: 65: 66: 67:
68: public function set_defaultRoute($v)
69: {
70: Util::validateArgCount(func_num_args(), 1);
71: Util::validateType($v, "string");
72: Util::setByPath($this->rawAnnotation, "Network.DefaultRoute", $v);
73: return $v;
74: }
75:
76:
77:
78: 79: 80: 81: 82:
83: public function get_maskLen()
84: {
85: $maskLen = Util::getByPath($this->rawAnnotation, "Network.NetworkMaskLen");
86: if ($maskLen == null) {
87: throw new SaklientException("invalid_data", "Data of the resource is invalid");
88: }
89: return intval($maskLen);
90: }
91:
92: 93: 94: 95: 96: 97:
98: public function set_maskLen($v)
99: {
100: Util::validateArgCount(func_num_args(), 1);
101: Util::validateType($v, "int");
102: Util::setByPath($this->rawAnnotation, "Network.NetworkMaskLen", $v);
103: return $v;
104: }
105:
106:
107:
108: 109: 110: 111: 112:
113: public function get_vrid()
114: {
115: $vrid = Util::getByPath($this->rawAnnotation, "VRRP.VRID");
116: if ($vrid == null) {
117: throw new SaklientException("invalid_data", "Data of the resource is invalid");
118: }
119: return intval($vrid);
120: }
121:
122: 123: 124: 125: 126: 127:
128: public function set_vrid($v)
129: {
130: Util::validateArgCount(func_num_args(), 1);
131: Util::validateType($v, "int");
132: Util::setByPath($this->rawAnnotation, "VRRP.VRID", $v);
133: return $v;
134: }
135:
136:
137:
138: 139: 140: 141: 142: 143: 144:
145: public function __construct(\Saklient\Cloud\Client $client, $obj, $wrapped=false)
146: {
147: parent::__construct($client, $obj, $wrapped);
148: Util::validateArgCount(func_num_args(), 2);
149: Util::validateType($client, "\\Saklient\\Cloud\\Client");
150: Util::validateType($wrapped, "boolean");
151: if ($this->rawAnnotation == null) {
152: $this->rawAnnotation = (object)[];
153: }
154: }
155:
156: 157: 158: 159: 160: 161: 162: 163:
164: protected function _onAfterApiDeserialize($r, $root)
165: {
166: Util::validateArgCount(func_num_args(), 2);
167: if ($this->rawAnnotation == null) {
168: $this->rawAnnotation = (object)[];
169: }
170: $this->_virtualIps = new \ArrayObject([]);
171: $settings = $this->rawSettings;
172: if ($settings != null) {
173: $lb = $settings->{"LoadBalancer"};
174: if ($lb == null) {
175: $lb = new \ArrayObject([]);
176: }
177: $vips = $lb;
178: foreach ($vips as $vip) {
179: $this->_virtualIps->append(new LbVirtualIp($vip));
180: }
181: }
182: }
183:
184: 185: 186: 187: 188: 189: 190:
191: protected function _onBeforeApiSerialize($withClean)
192: {
193: Util::validateArgCount(func_num_args(), 1);
194: Util::validateType($withClean, "boolean");
195: $lb = new \ArrayObject([]);
196: foreach ($this->_virtualIps as $vip) {
197: $lb->append($vip->toRawSettings());
198: }
199: if ($this->rawSettings == null) {
200: $this->rawSettings = (object)[];
201: }
202: $this->rawSettings->{"LoadBalancer"} = $lb;
203: if ($this->isNew) {
204: $this->clazz = EApplianceClass::loadbalancer;
205: }
206: }
207:
208: 209: 210: 211: 212: 213: 214: 215: 216:
217: public function setInitialParams(\Saklient\Cloud\Resources\Swytch $swytch, $vrid, $realIps, $isHighSpec=false)
218: {
219: Util::validateArgCount(func_num_args(), 3);
220: Util::validateType($swytch, "\\Saklient\\Cloud\\Resources\\Swytch");
221: Util::validateType($vrid, "int");
222: Util::validateType($realIps, "\\ArrayObject");
223: Util::validateType($isHighSpec, "boolean");
224: $annot = $this->rawAnnotation;
225: $this->vrid = $vrid;
226: Util::setByPath($annot, "Switch.ID", $swytch->_id());
227: if ($swytch->ipv4Nets != null && 0 < $swytch->ipv4Nets->count()) {
228: $net = $swytch->ipv4Nets[0];
229: $this->defaultRoute = $net->defaultRoute;
230: $this->maskLen = $net->maskLen;
231: }
232: else {
233: $this->defaultRoute = $swytch->userDefaultRoute;
234: $this->maskLen = $swytch->userMaskLen;
235: }
236: $servers = new \ArrayObject([]);
237: foreach ($realIps as $ip) {
238: $servers->append((object)['IPAddress' => $ip]);
239: }
240: Util::setByPath($annot, "Servers", $servers);
241: $this->planId = $isHighSpec ? 2 : 1;
242: return $this;
243: }
244:
245: 246: 247: 248:
249: public function clearVirtualIps()
250: {
251: while (0 < $this->_virtualIps->count()) {
252: Util::popAobj($this->_virtualIps);
253: }
254: return $this;
255: }
256:
257: 258: 259: 260: 261: 262: 263:
264: public function addVirtualIp($settings=null)
265: {
266: $ret = new LbVirtualIp($settings);
267: $this->_virtualIps->append($ret);
268: return $ret;
269: }
270:
271: 272: 273: 274: 275: 276: 277:
278: public function getVirtualIpByAddress($address)
279: {
280: Util::validateArgCount(func_num_args(), 1);
281: Util::validateType($address, "string");
282: foreach ($this->_virtualIps as $vip) {
283: if ($vip->virtualIpAddress == $address) {
284: return $vip;
285: }
286: }
287: return null;
288: }
289:
290: 291: 292: 293: 294: 295:
296: public function reloadStatus()
297: {
298: $result = $this->requestRetry("GET", $this->_apiPath() . "/" . Util::urlEncode($this->_id()) . "/status");
299: if ($result != null && array_key_exists("LoadBalancer", (array)($result))) {
300: $vips = $result->{"LoadBalancer"};
301: foreach ($vips as $vipDyn) {
302: $vipStr = $vipDyn->{"VirtualIPAddress"};
303: $vip = $this->getVirtualIpByAddress($vipStr);
304: if ($vip == null) {
305: continue;
306: }
307: $vip->updateStatus($vipDyn->{"Servers"});
308: }
309: }
310: return $this;
311: }
312:
313: 314: 315:
316: public function __get($key) {
317: switch ($key) {
318: case "virtualIps": return $this->get_virtualIps();
319: case "defaultRoute": return $this->get_defaultRoute();
320: case "maskLen": return $this->get_maskLen();
321: case "vrid": return $this->get_vrid();
322: default: return parent::__get($key);
323: }
324: }
325:
326: 327: 328:
329: public function __set($key, $v) {
330: switch ($key) {
331: case "defaultRoute": return $this->set_defaultRoute($v);
332: case "maskLen": return $this->set_maskLen($v);
333: case "vrid": return $this->set_vrid($v);
334: default: return parent::__set($key, $v);
335: }
336: }
337:
338: }
339:
340: