1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 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 public $imageRef;
42 public $bootable;
43
44 protected static $json_name = 'volume';
45 protected static $url_resource = 'volumes';
46
47 protected $createKeys = array(
48 'snapshot_id',
49 'display_name',
50 'display_description',
51 'size',
52 'volume_type',
53 'availability_zone',
54 'metadata',
55 'source_volid',
56 'bootable',
57 'imageRef'
58 );
59
60 protected $associatedResources = array();
61
62 public function update($params = array())
63 {
64 throw new Exceptions\UpdateError(
65 Lang::translate('Block storage volumes cannot be updated')
66 );
67 }
68
69 70 71 72 73 74 75
76 public function rename(array $params = array())
77 {
78 $data = array();
79
80 $keys = array('display_description', 'display_name');
81
82 foreach ($params as $key => $value) {
83 if (in_array($key, $keys)) {
84 $data[$key] = $value;
85 } else {
86 throw new \InvalidArgumentException(sprintf(
87 'You cannot update the %s volume property. Valid keys are: %s',
88 $key, implode($keys, ',')
89 ));
90 }
91 }
92
93 $json = json_encode(array(
94 'volume' => $data
95 ));
96
97 return $this->getClient()
98 ->put($this->getUrl(), self::getJsonHeader(), $json)
99 ->send();
100 }
101
102 public function name()
103 {
104 return $this->display_name;
105 }
106
107 protected function createJson()
108 {
109 $element = parent::createJson();
110
111 if ($this->getProperty('volume_type') instanceof VolumeType) {
112 $element->volume->volume_type = $this->volume_type->name();
113 }
114
115 return $element;
116 }
117 }
118