Overview

Namespaces

  • OpenCloud
    • Autoscale
      • Resource
    • CloudMonitoring
      • Exception
      • Resource
    • Common
      • Collection
      • Constants
      • Exceptions
      • Http
        • Message
      • Identity
      • Log
      • Service
    • Compute
      • Constants
      • Exception
      • Resource
    • Database
      • Resource
    • DNS
      • Resource
    • LoadBalancer
      • Resource
    • ObjectStore
      • Constants
      • Exception
      • Resource
      • Upload
    • Orchestration
    • Queues
      • Exception
      • Resource
    • Volume
      • Resource
  • PHP

Classes

  • Access
  • Algorithm
  • AllowedDomain
  • BillableLoadBalancer
  • ConnectionLogging
  • ConnectionThrottle
  • ContentCaching
  • ErrorPage
  • HealthMonitor
  • LoadBalancer
  • Metadata
  • Node
  • NodeEvent
  • Protocol
  • Readonly
  • SessionPersistence
  • SSLTermination
  • Stats
  • SubResource
  • Usage
  • VirtualIp
  • Overview
  • Namespace
  • Class
  • Tree
  • Download
  1: <?php
  2: /**
  3:  * PHP OpenCloud library.
  4:  * 
  5:  * @copyright Copyright 2013 Rackspace US, Inc. See COPYING for licensing information.
  6:  * @license   https://www.apache.org/licenses/LICENSE-2.0 Apache 2.0
  7:  * @version   1.6.0
  8:  * @author    Glen Campbell <glen.campbell@rackspace.com>
  9:  * @author    Jamie Hannaford <jamie.hannaford@rackspace.com>
 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:  * A load balancer is a logical device which belongs to a cloud account. It is 
 21:  * used to distribute workloads between multiple back-end systems or services, 
 22:  * based on the criteria defined as part of its configuration.
 23:  * 
 24:  * 
 25:  */
 26: class LoadBalancer extends PersistentObject 
 27: {
 28: 
 29:     public $id;
 30:     
 31:     /**
 32:      * Name of the load balancer to create. The name must be 128 characters or 
 33:      * less in length, and all UTF-8 characters are valid.
 34:      * 
 35:      * @var string 
 36:      */
 37:     public $name;
 38: 
 39:     /**
 40:      * Port of the service which is being laod balanced.
 41:      *
 42:      * @var string
 43:      */
 44:     public $port;
 45:     
 46:     /**
 47:      * Protocol of the service which is being load balanced.
 48:      * 
 49:      * @var string 
 50:      */
 51:     public $protocol;
 52:     
 53:     /**
 54:      * Type of virtual IP to add along with the creation of a load balancer.
 55:      * 
 56:      * @var array|Collection
 57:      */
 58:     public $virtualIps = array();
 59:     
 60:     /**
 61:      * Nodes to be added to the load balancer.
 62:      * 
 63:      * @var array|Collection
 64:      */
 65:     public $nodes = array();
 66:     
 67:     /**
 68:      * The access list management feature allows fine-grained network access 
 69:      * controls to be applied to the load balancer's virtual IP address.
 70:      * 
 71:      * @var Collection 
 72:      */
 73:     public $accessList;
 74:     
 75:     /**
 76:      * Algorithm that defines how traffic should be directed between back-end nodes.
 77:      *
 78:      * @var Algorithm
 79:      */
 80:     public $algorithm;
 81:     
 82:     /**
 83:      * Current connection logging configuration.
 84:      * 
 85:      * @var ConnectionLogging
 86:      */
 87:     public $connectionLogging;
 88:     
 89:     /**
 90:      * Specifies limits on the number of connections per IP address to help 
 91:      * mitigate malicious or abusive traffic to your applications.
 92:      * 
 93:      * @var ConnectionThrottle
 94:      */
 95:     public $connectionThrottle;
 96:     
 97:     /**
 98:      * The type of health monitor check to perform to ensure that the service is 
 99:      * performing properly.
100:      * 
101:      * @var HealthMonitor
102:      */
103:     public $healthMonitor;
104: 
105:     /**
106:      * Forces multiple requests, of the same protocol, from clients to be
107:      * directed to the same node.
108:      *
109:      * @var SessionPersistance
110:      */
111:     public $sessionPersistence;
112:     
113:     /**
114:      * Information (metadata) that can be associated with each load balancer for 
115:      * the client's personal use.
116:      * 
117:      * @var array|Metadata 
118:      */
119:     public $metadata = array();
120:     
121:     /**
122:      * The timeout value for the load balancer and communications with its nodes. 
123:      * Defaults to 30 seconds with a maximum of 120 seconds.
124:      * 
125:      * @var int 
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:         //'nodes'      => 'Node',
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:      * adds a node to the load balancer
169:      *
170:      * This method creates a Node object and adds it to a list of Nodes
171:      * to be added to the LoadBalancer. *Very important:* this method *NEVER*
172:      * adds the nodes directly to the load balancer itself; it stores them
173:      * on the object, and the nodes are added later, in one of two ways:
174:      *
175:      * * for a new LoadBalancer, the Nodes are added as part of the Create()
176:      *   method call.
177:      * * for an existing LoadBalancer, you must call the AddNodes() method
178:      *
179:      * @api
180:      * @param string $address the IP address of the node
181:      * @param integer $port the port # of the node
182:      * @param boolean $condition the initial condition of the node
183:      * @param string $type either PRIMARY or SECONDARY
184:      * @param integer $weight the node weight (for round-robin)
185:      * @throws \OpenCloud\DomainError if value is not valid
186:      * @return void
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:         // queue it
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:         // iterate through all the nodes
251:         foreach($this->nodes as $node) {
252:             $resp = $node->Create();
253:         }
254: 
255:         return $resp;
256:     }
257:     
258:     /**
259:      * Remove a node from this load-balancer
260:      *
261:      * @api
262:      * @param int $id id of the node
263:      * @return mixed
264:      */
265:     public function removeNode($nodeId)
266:     {
267:         return $this->node($nodeId)->delete();
268:     }
269: 
270:     /**
271:      * adds a virtual IP to the load balancer
272:      *
273:      * You can use the strings `'PUBLIC'` or `'SERVICENET`' to indicate the
274:      * public or internal networks, or you can pass the `Id` of an existing
275:      * IP address.
276:      *
277:      * @api
278:      * @param string $id either 'public' or 'servicenet' or an ID of an
279:      *      existing IP address
280:      * @param integer $ipVersion either null, 4, or 6 (both, IPv4, or IPv6)
281:      * @return void
282:      */
283:     public function addVirtualIp($type = 'PUBLIC', $ipVersion = NULL) 
284:     {
285:         $object = new \stdClass();
286: 
287:         /**
288:          * check for PUBLIC or SERVICENET
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:          * If the load balancer exists, we want to add it immediately.
318:          * If not, we add it to the virtualIps list and add it when the load
319:          * balancer is created.
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:             // queue it
328:             $this->virtualIps[] = $object;
329:         }
330: 
331:         return true;
332:     }
333: 
334:     /**
335:      * returns a Node object
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:      * returns a Collection of Nodes
346:      */
347:     public function nodeList() 
348:     {
349:         return $this->getService()->resourceList('Node', null, $this);
350:     }
351: 
352:     /**
353:      * returns a NodeEvent object
354:      */
355:     public function nodeEvent() 
356:     {
357:         $resource = new NodeEvent($this->getService());
358:         $resource->setParent($this)->initialRefresh();
359:         return $resource;
360:     }
361: 
362:     /**
363:      * returns a Collection of NodeEvents
364:      */
365:     public function nodeEventList() 
366:     {
367:         return $this->getParent()->resourceList('NodeEvent', null, $this);
368:     }
369: 
370:     /**
371:      * returns a single Virtual IP (not called publicly)
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:      * returns  a Collection of Virtual Ips
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:      * returns the load balancer's error page object
400:      *
401:      * @api
402:      * @return ErrorPage
403:      */
404:     public function errorPage() 
405:     {
406:         $resource = new ErrorPage($this->getService());
407:         $resource->setParent($this)->initialRefresh();
408:         return $resource;
409:     }
410: 
411:     /**
412:      * returns the load balancer's health monitor object
413:      *
414:      * @api
415:      * @return HealthMonitor
416:      */
417:     public function healthMonitor() 
418:     {
419:         $resource = new HealthMonitor($this->getService());
420:         $resource->setParent($this)->initialRefresh();
421:         return $resource;
422:     }
423: 
424:     /**
425:      * returns statistics on the load balancer operation
426:      *
427:      * cannot be created, updated, or deleted
428:      *
429:      * @api
430:      * @return Stats
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:      * returns the JSON object for Create()
518:      *
519:      * @return stdClass
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:      * returns the JSON object for Update()
552:      *
553:      * @return stdClass
554:      * @throws \OpenCloud\Common\Exceptions\InvalidParameterError
555:      */
556:     protected function updateJson($params = array())
557:     {
558: 
559:         $updatableFields = array('name','algorithm','protocol','port','timeout','halfClosed');
560: 
561:         //Validate supplied fields
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: 
PHP OpenCloud API API documentation generated by ApiGen 2.8.0