Overview

Namespaces

  • None
  • OpenCloud
    • Autoscale
      • Resource
    • CloudMonitoring
      • Exception
      • Resource
    • Common
      • Exceptions
      • Log
      • Request
        • Response
    • Compute
    • Database
    • DNS
    • LoadBalancer
      • Resources
    • ObjectStore
      • Resource
    • Orchestration
    • Volume
  • 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\Resources;
 13: 
 14: use OpenCloud\Common\PersistentObject;
 15: use OpenCloud\Common\Lang;
 16: use OpenCloud\Common\Exceptions;
 17: 
 18: /**
 19:  * A load balancer is a logical device which belongs to a cloud account. It is 
 20:  * used to distribute workloads between multiple back-end systems or services, 
 21:  * based on the criteria defined as part of its configuration.
 22:  * 
 23:  * 
 24:  */
 25: class LoadBalancer extends PersistentObject 
 26: {
 27: 
 28:     public $id;
 29:     
 30:     /**
 31:      * Name of the load balancer to create. The name must be 128 characters or 
 32:      * less in length, and all UTF-8 characters are valid.
 33:      * 
 34:      * @var string 
 35:      */
 36:     public $name;
 37:     
 38:     /**
 39:      * Port number for the service you are load balancing.
 40:      * 
 41:      * @var int 
 42:      */
 43:     public $port;
 44:     
 45:     /**
 46:      * Protocol of the service which is being load balanced.
 47:      * 
 48:      * @var string 
 49:      */
 50:     public $protocol;
 51:     
 52:     /**
 53:      * Type of virtual IP to add along with the creation of a load balancer.
 54:      * 
 55:      * @var array 
 56:      */
 57:     public $virtualIps = array();
 58:     
 59:     /**
 60:      * Nodes to be added to the load balancer.
 61:      * 
 62:      * @var array 
 63:      */
 64:     public $nodes = array();
 65:     
 66:     /**
 67:      * The access list management feature allows fine-grained network access 
 68:      * controls to be applied to the load balancer's virtual IP address.
 69:      * 
 70:      * @var Collection 
 71:      */
 72:     public $accessList;
 73:     
 74:     /**
 75:      * Algorithm that defines how traffic should be directed between back-end nodes.
 76:      *
 77:      * @var string 
 78:      */
 79:     public $algorithm;
 80:     
 81:     /**
 82:      * Current connection logging configuration.
 83:      * 
 84:      * @var string 
 85:      */
 86:     public $connectionLogging;
 87:     
 88:     /**
 89:      * Specifies limits on the number of connections per IP address to help 
 90:      * mitigate malicious or abusive traffic to your applications.
 91:      * 
 92:      * @var string 
 93:      */
 94:     public $connectionThrottle;
 95:     
 96:     /**
 97:      * The type of health monitor check to perform to ensure that the service is 
 98:      * performing properly.
 99:      * 
100:      * @var string 
101:      */
102:     public $healthMonitor;
103:     public $sessionPersistence;
104:     
105:     /**
106:      * Information (metadata) that can be associated with each load balancer for 
107:      * the client's personal use.
108:      * 
109:      * @var array|Metadata 
110:      */
111:     public $metadata = array();
112:     
113:     /**
114:      * The timeout value for the load balancer and communications with its nodes. 
115:      * Defaults to 30 seconds with a maximum of 120 seconds.
116:      * 
117:      * @var int 
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:      * adds a node to the load balancer
147:      *
148:      * This method creates a Node object and adds it to a list of Nodes
149:      * to be added to the LoadBalancer. *Very important:* this method *NEVER*
150:      * adds the nodes directly to the load balancer itself; it stores them
151:      * on the object, and the nodes are added later, in one of two ways:
152:      *
153:      * * for a new LoadBalancer, the Nodes are added as part of the Create()
154:      *   method call.
155:      * * for an existing LoadBalancer, you must call the AddNodes() method
156:      *
157:      * @api
158:      * @param string $address the IP address of the node
159:      * @param integer $port the port # of the node
160:      * @param boolean $condition the initial condition of the node
161:      * @param string $type either PRIMARY or SECONDARY
162:      * @param integer $weight the node weight (for round-robin)
163:      * @throws \OpenCloud\DomainError if value is not valid
164:      * @return void
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:      * adds queued nodes to the load balancer
221:      *
222:      * In many cases, Nodes will be added to the Load Balancer when it is
223:      * created (via the `Create()` method), but this method is provided when
224:      * a set of Nodes needs to be added after the fact.
225:      *
226:      * @api
227:      * @return HttpResponse
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:         // iterate through all the nodes
238:         foreach($this->nodes as $node) {
239:             $resp = $node->Create();
240:         }
241: 
242:         return $resp;
243:     }
244: 
245:     /**
246:      * adds a virtual IP to the load balancer
247:      *
248:      * You can use the strings `'PUBLIC'` or `'SERVICENET`' to indicate the
249:      * public or internal networks, or you can pass the `Id` of an existing
250:      * IP address.
251:      *
252:      * @api
253:      * @param string $id either 'public' or 'servicenet' or an ID of an
254:      *      existing IP address
255:      * @param integer $ipVersion either null, 4, or 6 (both, IPv4, or IPv6)
256:      * @return void
257:      */
258:     public function addVirtualIp($type = 'PUBLIC', $ipVersion = NULL) 
259:     {
260:         $object = new \stdClass();
261: 
262:         /**
263:          * check for PUBLIC or SERVICENET
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:          * If the load balancer exists, we want to add it immediately.
293:          * If not, we add it to the virtualIps list and add it when the load
294:          * balancer is created.
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:             // queue it
309:             $this->virtualIps[] = $object;
310:         }
311: 
312:         return true;
313:     }
314: 
315:     /**
316:      * returns a Node object
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:      * returns a Collection of Nodes
327:      */
328:     public function nodeList() 
329:     {
330:         return $this->getParent()->Collection('\OpenCloud\LoadBalancer\Resources\Node', null, $this);
331:     }
332: 
333:     /**
334:      * returns a NodeEvent object
335:      */
336:     public function nodeEvent() 
337:     {
338:         $resource = new NodeEvent($this->getService());
339:         $resource->setParent($this)->initialRefresh();
340:         return $resource;
341:     }
342: 
343:     /**
344:      * returns a Collection of NodeEvents
345:      */
346:     public function nodeEventList() 
347:     {
348:         return $this->getParent()->Collection('\OpenCloud\LoadBalancer\Resources\NodeEvent', null, $this);
349:     }
350: 
351:     /**
352:      * returns a single Virtual IP (not called publicly)
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:      * returns  a Collection of Virtual Ips
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:      * returns the load balancer's error page object
381:      *
382:      * @api
383:      * @return ErrorPage
384:      */
385:     public function errorPage() 
386:     {
387:         $resource = new ErrorPage($this->getService());
388:         $resource->setParent($this)->initialRefresh();
389:         return $resource;
390:     }
391: 
392:     /**
393:      * returns the load balancer's health monitor object
394:      *
395:      * @api
396:      * @return HealthMonitor
397:      */
398:     public function healthMonitor() 
399:     {
400:         $resource = new HealthMonitor($this->getService());
401:         $resource->setParent($this)->initialRefresh();
402:         return $resource;
403:     }
404: 
405:     /**
406:      * returns statistics on the load balancer operation
407:      *
408:      * cannot be created, updated, or deleted
409:      *
410:      * @api
411:      * @return Stats
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:      * returns the JSON object for Create()
499:      *
500:      * @return stdClass
501:      */
502:     protected function createJson() 
503:     {
504:         $object = new \stdClass();
505:         $elem = $this->JsonName();
506:         $object->$elem = new \stdClass();
507: 
508:         // set the properties
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: 
PHP OpenCloud API API documentation generated by ApiGen 2.8.0