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