1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace OpenCloud\LoadBalancer\Resource;
13:
14: use OpenCloud\Common\PersistentObject;
15: use OpenCloud\Common\Lang;
16: use OpenCloud\Common\Exceptions;
17: use OpenCloud\Common\Http\Message\Formatter;
18:
19: 20: 21: 22: 23: 24: 25:
26: class LoadBalancer extends PersistentObject
27: {
28:
29: public $id;
30:
31: 32: 33: 34: 35: 36:
37: public $name;
38:
39: 40: 41: 42: 43:
44: public $port;
45:
46: 47: 48: 49: 50:
51: public $protocol;
52:
53: 54: 55: 56: 57:
58: public $virtualIps = array();
59:
60: 61: 62: 63: 64:
65: public $nodes = array();
66:
67: 68: 69: 70: 71: 72:
73: public $accessList;
74:
75: 76: 77: 78: 79:
80: public $algorithm;
81:
82: 83: 84: 85: 86:
87: public $connectionLogging;
88:
89: 90: 91: 92: 93: 94:
95: public $connectionThrottle;
96:
97: 98: 99: 100: 101: 102:
103: public $healthMonitor;
104:
105: 106: 107: 108: 109: 110:
111: public $sessionPersistence;
112:
113: 114: 115: 116: 117: 118:
119: public $metadata = array();
120:
121: 122: 123: 124: 125: 126:
127: public $timeout;
128:
129: public $created;
130: public $updated;
131: public $status;
132: public $nodeCount;
133: public $sourceAddresses;
134: public $cluster;
135:
136: protected static $json_name = 'loadBalancer';
137: protected static $url_resource = 'loadbalancers';
138:
139: protected $associatedResources = array(
140: 'node' => 'Node',
141: 'virtualIp' => 'VirtualIp',
142: 'connectionLogging' => 'ConnectionLogging',
143: 'healthMonitor' => 'HealthMonitor',
144: 'sessionPersistance' => 'SessionPersistance'
145: );
146:
147: protected $associatedCollections = array(
148:
149: 'virtualIps' => 'VirtualIp',
150: 'accessList' => 'Access'
151: );
152:
153: private $createKeys = array(
154: 'name',
155: 'port',
156: 'protocol',
157: 'virtualIps',
158: 'nodes',
159: 'accessList',
160: 'algorithm',
161: 'connectionLogging',
162: 'connectionThrottle',
163: 'healthMonitor',
164: 'sessionPersistence'
165: );
166:
167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187:
188: public function addNode(
189: $address,
190: $port,
191: $condition = 'ENABLED',
192: $type = null,
193: $weight = null
194: ) {
195: $node = $this->Node();
196: $node->address = $address;
197: $node->port = $port;
198: $cond = strtoupper($condition);
199:
200: switch($cond) {
201: case 'ENABLED':
202: case 'DISABLED':
203: case 'DRAINING':
204: $node->condition = $cond;
205: break;
206: default:
207: throw new Exceptions\DomainError(sprintf(
208: Lang::translate('Value [%s] for Node::condition is not valid'),
209: $condition
210: ));
211: }
212:
213: if ($type !== null) {
214: switch(strtoupper($type)) {
215: case 'PRIMARY':
216: case 'SECONDARY':
217: $node->type = $type;
218: break;
219: default:
220: throw new Exceptions\DomainError(sprintf(
221: Lang::translate('Value [%s] for Node::type is not valid'),
222: $type
223: ));
224: }
225: }
226:
227: if ($weight !== null) {
228: if (is_integer($weight)) {
229: $node->weight = $weight;
230: } else {
231: throw new Exceptions\DomainError(sprintf(
232: Lang::translate('Value [%s] for Node::weight must be integer'),
233: $weight
234: ));
235: }
236: }
237:
238:
239: $this->nodes[] = $node;
240: }
241:
242: public function addNodes()
243: {
244: if (count($this->nodes) < 1) {
245: throw new Exceptions\MissingValueError(
246: Lang::translate('Cannot add nodes; no nodes are defined')
247: );
248: }
249:
250:
251: foreach($this->nodes as $node) {
252: $resp = $node->Create();
253: }
254:
255: return $resp;
256: }
257:
258: 259: 260: 261: 262: 263: 264:
265: public function removeNode($nodeId)
266: {
267: return $this->node($nodeId)->delete();
268: }
269:
270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282:
283: public function addVirtualIp($type = 'PUBLIC', $ipVersion = NULL)
284: {
285: $object = new \stdClass();
286:
287: 288: 289:
290: switch(strtoupper($type)) {
291: case 'PUBLIC':
292: case 'SERVICENET':
293: $object->type = strtoupper($type);
294: break;
295: default:
296: $object->id = $type;
297: break;
298: }
299:
300: if ($ipVersion) {
301: switch($ipVersion) {
302: case 4:
303: $object->version = 'IPV4';
304: break;
305: case 6:
306: $object->version = 'IPV6';
307: break;
308: default:
309: throw new Exceptions\DomainError(sprintf(
310: Lang::translate('Value [%s] for ipVersion is not valid'),
311: $ipVersion
312: ));
313: }
314: }
315:
316: 317: 318: 319: 320:
321: if ($this->Id()) {
322: $virtualIp = $this->virtualIp();
323: $virtualIp->type = $type;
324: $virtualIp->ipVersion = $object->version;
325: return $virtualIp->create();
326: } else {
327:
328: $this->virtualIps[] = $object;
329: }
330:
331: return true;
332: }
333:
334: 335: 336:
337: public function node($id = null)
338: {
339: $resource = new Node($this->getService());
340: $resource->setParent($this)->populate($id);
341: return $resource;
342: }
343:
344: 345: 346:
347: public function nodeList()
348: {
349: return $this->getService()->resourceList('Node', null, $this);
350: }
351:
352: 353: 354:
355: public function nodeEvent()
356: {
357: $resource = new NodeEvent($this->getService());
358: $resource->setParent($this)->initialRefresh();
359: return $resource;
360: }
361:
362: 363: 364:
365: public function nodeEventList()
366: {
367: return $this->getParent()->resourceList('NodeEvent', null, $this);
368: }
369:
370: 371: 372:
373: public function virtualIp($data = null)
374: {
375: $resource = new VirtualIp($this->getService(), $data);
376: $resource->setParent($this)->initialRefresh();
377: return $resource;
378:
379: }
380:
381: 382: 383:
384: public function virtualIpList()
385: {
386: return $this->getService()->resourceList('VirtualIp', null, $this);
387: }
388:
389: 390:
391: public function sessionPersistence()
392: {
393: $resource = new SessionPersistence($this->getService());
394: $resource->setParent($this)->initialRefresh();
395: return $resource;
396: }
397:
398: 399: 400: 401: 402: 403:
404: public function errorPage()
405: {
406: $resource = new ErrorPage($this->getService());
407: $resource->setParent($this)->initialRefresh();
408: return $resource;
409: }
410:
411: 412: 413: 414: 415: 416:
417: public function healthMonitor()
418: {
419: $resource = new HealthMonitor($this->getService());
420: $resource->setParent($this)->initialRefresh();
421: return $resource;
422: }
423:
424: 425: 426: 427: 428: 429: 430: 431:
432: public function stats()
433: {
434: $resource = new Stats($this->getService());
435: $resource->setParent($this)->initialRefresh();
436: return $resource;
437: }
438:
439: 440:
441: public function usage()
442: {
443: $resource = new Usage($this->getService());
444: $resource->setParent($this)->initialRefresh();
445: return $resource;
446: }
447:
448: 449:
450: public function access($data = null)
451: {
452: $resource = new Access($this->getService(), $data);
453: $resource->setParent($this)->initialRefresh();
454: return $resource;
455: }
456:
457: 458:
459: public function accessList()
460: {
461: return $this->getService()->resourceList('Access', null, $this);
462: }
463:
464: 465:
466: public function connectionThrottle()
467: {
468: $resource = new ConnectionThrottle($this->getService());
469: $resource->setParent($this)->initialRefresh();
470: return $resource;
471: }
472:
473: 474:
475: public function connectionLogging()
476: {
477: $resource = new ConnectionLogging($this->getService());
478: $resource->setParent($this)->initialRefresh();
479: return $resource;
480: }
481:
482: 483:
484: public function contentCaching()
485: {
486: $resource = new ContentCaching($this->getService());
487: $resource->setParent($this)->initialRefresh();
488: return $resource;
489: }
490:
491: 492:
493: public function SSLTermination()
494: {
495: $resource = new SSLTermination($this->getService());
496: $resource->setParent($this)->initialRefresh();
497: return $resource;
498: }
499:
500: 501:
502: public function metadata($data = null)
503: {
504: $resource = new Metadata($this->getService(), $data);
505: $resource->setParent($this)->initialRefresh();
506: return $resource;
507: }
508:
509: 510:
511: public function metadataList()
512: {
513: return $this->getService()->resourceList('Metadata', null, $this);
514: }
515:
516: 517: 518: 519: 520:
521: protected function createJson()
522: {
523: $element = (object) array();
524:
525: foreach ($this->createKeys as $key) {
526: if ($key == 'nodes') {
527: foreach ($this->nodes as $node) {
528: $nodeObject = (object) array();
529: foreach ($node->createKeys as $key) {
530: if (!empty($node->$key)) {
531: $nodeObject->$key = $node->$key;
532: }
533: }
534: $element->nodes[] = (object) $nodeObject;
535: }
536: } elseif($key == 'virtualIps') {
537: foreach ($this->virtualIps as $virtualIp) {
538: $element->virtualIps[] = $virtualIp;
539: }
540: } elseif (isset($this->$key)) {
541: $element->$key = $this->$key;
542: }
543: }
544:
545: $object = (object) array($this->jsonName() => $element);
546:
547: return $object;
548: }
549:
550: 551: 552: 553: 554: 555:
556: protected function updateJson($params = array())
557: {
558:
559: $updatableFields = array('name','algorithm','protocol','port','timeout','halfClosed');
560:
561:
562: $fields = array_keys($params);
563: foreach($fields as $field) {
564: if (!in_array($field, $updatableFields)) {
565: throw new Exceptions\InvalidArgumentError("You cannot update $field.");
566: }
567: }
568:
569: $object = new \stdClass();
570: $object->loadBalancer = new \stdClass();
571: foreach($params as $name => $value) {
572: $object->loadBalancer->$name = $this->$name;
573: }
574: return $object;
575: }
576: }
577: