      1: <?php
      2: /**
      3:  * Copyright 2012-2014 Rackspace US, Inc.
      4:  *
      5:  * Licensed under the Apache License, Version 2.0 (the "License");
      6:  * you may not use this file except in compliance with the License.
      7:  * You may obtain a copy of the License at
      8:  *
      9:  * http://www.apache.org/licenses/LICENSE-2.0
     10:  *
     11:  * Unless required by applicable law or agreed to in writing, software
     12:  * distributed under the License is distributed on an "AS IS" BASIS,
     13:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14:  * See the License for the specific language governing permissions and
     15:  * limitations under the License.
     16:  */
     17:
     18: namespace OpenCloud\Volume\Resource;
     19:
     20: use OpenCloud\Common\Exceptions;
     21: use OpenCloud\Common\Lang;
     22: use OpenCloud\Common\Resource\PersistentResource;
     23:
     24: /**
     25:  * The Volume class represents a single block storage volume
     26:  */
     27: class Volume extends PersistentResource
     28: {
     29:     public $id;
     30:     public $status;
     31:     public $display_name;
     32:     public $display_description;
     33:     public $size;
     34:     public $volume_type;
     35:     public $metadata = array();
     36:     public $availability_zone;
     37:     public $snapshot_id;
     38:     public $attachments = array();
     39:     public $created_at;
     40:     public $source_volid;
     41:
     42:     /**
     43:      * OpenStack only
     44:      */
     45:     public $imageRef;
     46:     public $bootable;
     47:
     48:     protected static $json_name = 'volume';
     49:     protected static $url_resource = 'volumes';
     50:
     51:     protected $createKeys = array(
     52:         'snapshot_id',
     53:         'display_name',
     54:         'display_description',
     55:         'size',
     56:         'volume_type',
     57:         'availability_zone',
     58:         'metadata',
     59:         'source_volid',
     60:         'bootable',
     61:         'imageRef'
     62:     );
     63:
     64:     protected $associatedResources = array();
     65:
     66:     public function update($params = array())
     67:     {
     68:         throw new Exceptions\UpdateError(
     69:             Lang::translate('Block storage volumes cannot be updated')
     70:         );
     71:     }
     72:
     73:     /**
     74:      * Rename either the `display_description` or the `display_name` properties
     75:      *
     76:      * @param array $params
     77:      * @return \Guzzle\Http\Message\Response
     78:      * @throws \InvalidArgumentException
     79:      */
     80:     public function rename(array $params = array())
     81:     {
     82:         $data = array();
     83:
     84:         $keys = array('display_description', 'display_name');
     85:
     86:         foreach ($params as $key => $value) {
     87:             if (in_array($key, $keys)) {
     88:                 $data[$key] = $value;
     89:             } else {
     90:                 throw new \InvalidArgumentException(sprintf(
     91:                     'You cannot update the %s volume property. Valid keys are: %s',
     92:                     $key, implode($keys, ',')
     93:                 ));
     94:             }
     95:         }
     96:
     97:         $json = json_encode(array(
     98:            'volume' => $data
     99:         ));
    100:
    101:         return $this->getClient()
    102:             ->put($this->getUrl(), self::getJsonHeader(), $json)
    103:             ->send();
    104:     }
    105:
    106:     public function name()
    107:     {
    108:         return $this->display_name;
    109:     }
    110:
    111:     protected function createJson()
    112:     {
    113:         $element = parent::createJson();
    114:
    115:         if ($this->getProperty('volume_type') instanceof VolumeType) {
    116:             $element->volume->volume_type = $this->volume_type->name();
    117:         }
    118:
    119:         return $element;
    120:     }
    121: }
    122: