web/lib/Zend/Service/Rackspace/Servers/SharedIpGroup.php
changeset 808 6b6c2214f778
child 1230 68c69c656a2c
equal deleted inserted replaced
807:877f952ae2bd 808:6b6c2214f778
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * Zend Framework
       
     5  *
       
     6  * LICENSE
       
     7  *
       
     8  * This source file is subject to the new BSD license that is bundled
       
     9  * with this package in the file LICENSE.txt.
       
    10  * It is also available through the world-wide-web at this URL:
       
    11  * http://framework.zend.com/license/new-bsd
       
    12  * If you did not receive a copy of the license and are unable to
       
    13  * obtain it through the world-wide-web, please send an email
       
    14  * to license@zend.com so we can send you a copy immediately.
       
    15  *
       
    16  * @category   Zend
       
    17  * @package    Zend_Service_Rackspace
       
    18  * @subpackage Servers
       
    19  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
       
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    21  */
       
    22 
       
    23 require_once 'Zend/Service/Rackspace/Servers.php';
       
    24 
       
    25 class Zend_Service_Rackspace_Servers_SharedIpGroup
       
    26 {
       
    27     const ERROR_PARAM_CONSTRUCT  = 'You must pass a Zend_Service_Rackspace_Servers object and an array';
       
    28     const ERROR_PARAM_NO_NAME    = 'You must pass the image\'s name in the array (name)';
       
    29     const ERROR_PARAM_NO_ID      = 'You must pass the image\'s id in the array (id)';
       
    30     const ERROR_PARAM_NO_SERVERS = 'The servers parameter must be an array of Ids';
       
    31     /**
       
    32      * Name of the shared IP group
       
    33      * 
       
    34      * @var string 
       
    35      */
       
    36     protected $name;
       
    37     /**
       
    38      * Id of the shared IP group
       
    39      * 
       
    40      * @var string 
       
    41      */
       
    42     protected $id;
       
    43     /**
       
    44      * Array of servers of the shared IP group
       
    45      * 
       
    46      * @var array 
       
    47      */
       
    48     protected $serversId = array();
       
    49     /**
       
    50      * The service that has created the image object
       
    51      *
       
    52      * @var Zend_Service_Rackspace_Servers
       
    53      */
       
    54     protected $service;
       
    55     /**
       
    56      * Construct
       
    57      * 
       
    58      * @param  Zend_Service_Rackspace_Servers $service
       
    59      * @param  array $data
       
    60      * @return void
       
    61      */
       
    62     public function __construct($service, $data)
       
    63     {
       
    64         if (!($service instanceof Zend_Service_Rackspace_Servers) || !is_array($data)) {
       
    65             require_once 'Zend/Service/Rackspace/Servers/Exception.php';
       
    66             throw new Zend_Service_Rackspace_Servers_Exception(self::ERROR_PARAM_CONSTRUCT);
       
    67         }
       
    68         if (!array_key_exists('name', $data)) {
       
    69             require_once 'Zend/Service/Rackspace/Servers/Exception.php';
       
    70             throw new Zend_Service_Rackspace_Servers_Exception(self::ERROR_PARAM_NO_NAME);
       
    71         }
       
    72         if (!array_key_exists('id', $data)) {
       
    73             require_once 'Zend/Service/Rackspace/Servers/Exception.php';
       
    74             throw new Zend_Service_Rackspace_Servers_Exception(self::ERROR_PARAM_NO_ID);
       
    75         }
       
    76         if (isset($data['servers']) && !is_array($data['servers'])) {
       
    77             require_once 'Zend/Service/Rackspace/Servers/Exception.php';
       
    78             throw new Zend_Service_Rackspace_Servers_Exception(self::ERROR_PARAM_NO_SERVERS);
       
    79         } 
       
    80         $this->service= $service;
       
    81         $this->name = $data['name'];
       
    82         $this->id = $data['id'];
       
    83         if (isset($data['servers'])) {
       
    84             $this->serversId= $data['servers'];
       
    85         }    
       
    86     }
       
    87     /**
       
    88      * Get the name of the shared IP group
       
    89      *
       
    90      * @return string
       
    91      */
       
    92     public function getName()
       
    93     {
       
    94         return $this->name;
       
    95     }
       
    96     /**
       
    97      * Get the id of the shared IP group
       
    98      * 
       
    99      * @return string 
       
   100      */
       
   101     public function getId()
       
   102     {
       
   103         return $this->id;
       
   104     }
       
   105     /**
       
   106      * Get the server's array of the shared IP group
       
   107      * 
       
   108      * @return string 
       
   109      */
       
   110     public function getServersId()
       
   111     {
       
   112         if (empty($this->serversId)) {
       
   113             $info= $this->service->getSharedIpGroup($this->id);
       
   114             if (($info!==false)) {
       
   115                 $info= $info->toArray();
       
   116                 if (isset($info['servers'])) {
       
   117                     $this->serversId= $info['servers'];
       
   118                 }
       
   119             }    
       
   120         }
       
   121         return $this->serversId;
       
   122     }
       
   123     /**
       
   124      * Get the server 
       
   125      * 
       
   126      * @param integer $id
       
   127      * @return Zend_Service_Rackspace_Servers_Server|boolean
       
   128      */
       
   129     public function getServer($id)
       
   130     {
       
   131         if (empty($this->serversId)) {
       
   132             $this->getServersId();
       
   133         }
       
   134         if (in_array($id,$this->serversId)) {
       
   135             return $this->service->getServer($id);
       
   136         }
       
   137         return false;
       
   138     }
       
   139     /**
       
   140      * Create a server in the shared Ip Group
       
   141      * 
       
   142      * @param  array $data
       
   143      * @param  array $metadata
       
   144      * @param  array $files 
       
   145      * @return Zend_Service_Rackspace_Servers_Server|boolean
       
   146      */
       
   147     public function createServer(array $data, $metadata=array(),$files=array()) 
       
   148     {
       
   149         $data['sharedIpGroupId']= (integer) $this->id;
       
   150         return $this->service->createServer($data,$metadata,$files);
       
   151     }
       
   152     /**
       
   153      * To Array
       
   154      * 
       
   155      * @return array 
       
   156      */
       
   157     public function toArray()
       
   158     {
       
   159         return array (
       
   160             'name'    => $this->name,
       
   161             'id'      => $this->id,
       
   162             'servers' => $this->serversId
       
   163         );
       
   164     }
       
   165 }