/
Volume.php
184 lines (145 loc) · 4.88 KB
/
Volume.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
declare(strict_types=1);
namespace OpenStack\BlockStorage\v2\Models;
use OpenStack\Common\Resource\Alias;
use OpenStack\Common\Resource\Creatable;
use OpenStack\Common\Resource\Deletable;
use OpenStack\Common\Resource\HasMetadata;
use OpenStack\Common\Resource\HasWaiterTrait;
use OpenStack\Common\Resource\Listable;
use OpenStack\Common\Resource\OperatorResource;
use OpenStack\Common\Resource\Retrievable;
use OpenStack\Common\Resource\Updateable;
use OpenStack\Common\Transport\Utils;
use Psr\Http\Message\ResponseInterface;
/**
* @property \OpenStack\BlockStorage\v2\Api $api
*/
class Volume extends OperatorResource implements Creatable, Listable, Updateable, Deletable, Retrievable, HasMetadata
{
use HasWaiterTrait;
/** @var string */
public $id;
/** @var int */
public $size;
/** @var string */
public $status;
/** @var string */
public $name;
/** @var array */
public $attachments;
/** @var string */
public $availabilityZone;
/** @var \DateTimeImmutable */
public $createdAt;
/** @var string */
public $description;
/** @var string */
public $volumeTypeName;
/** @var string */
public $snapshotId;
/** @var string */
public $sourceVolumeId;
/** @var string */
public $tenantId;
/** @var string */
public $host;
/** @var string */
public $bootable;
/** @var array */
public $metadata = [];
/** @var array */
public $volumeImageMetadata = [];
protected $resourceKey = 'volume';
protected $resourcesKey = 'volumes';
protected $markerKey = 'id';
protected $aliases = [
'availability_zone' => 'availabilityZone',
'source_volid' => 'sourceVolumeId',
'snapshot_id' => 'snapshotId',
'volume_type' => 'volumeTypeName',
'os-vol-tenant-attr:tenant_id' => 'tenantId',
'os-vol-host-attr:host' => 'host',
'volume_image_metadata' => 'volumeImageMetadata',
];
protected function getAliases(): array
{
return parent::getAliases() + [
'created_at' => new Alias('createdAt', \DateTimeImmutable::class),
];
}
public function populateFromResponse(ResponseInterface $response): self
{
parent::populateFromResponse($response);
$this->metadata = $this->parseMetadata($response);
return $this;
}
public function retrieve()
{
$response = $this->executeWithState($this->api->getVolume());
$this->populateFromResponse($response);
}
/**
* @param array $userOptions {@see \OpenStack\BlockStorage\v2\Api::postVolumes}
*/
public function create(array $userOptions): Creatable
{
$response = $this->execute($this->api->postVolumes(), $userOptions);
return $this->populateFromResponse($response);
}
public function update()
{
$response = $this->executeWithState($this->api->putVolume());
$this->populateFromResponse($response);
}
public function delete()
{
$this->executeWithState($this->api->deleteVolume());
}
public function getMetadata(): array
{
$response = $this->executeWithState($this->api->getVolumeMetadata());
$this->metadata = $this->parseMetadata($response);
return $this->metadata;
}
public function mergeMetadata(array $metadata)
{
$this->getMetadata();
$this->metadata = array_merge($this->metadata, $metadata);
$this->executeWithState($this->api->putVolumeMetadata());
}
public function resetMetadata(array $metadata)
{
$this->metadata = $metadata;
$this->executeWithState($this->api->putVolumeMetadata());
}
public function parseMetadata(ResponseInterface $response): array
{
$json = Utils::jsonDecode($response);
return isset($json['metadata']) ? $json['metadata'] : [];
}
/**
* Update the bootable status for a volume, mark it as a bootable volume.
*/
public function setBootable(bool $bootable = true)
{
$this->execute($this->api->postVolumeBootable(), ['id' => $this->id, 'bootable' => $bootable]);
}
/**
* Sets the image metadata for a volume.
*/
public function setImageMetadata(array $metadata)
{
$this->execute($this->api->postImageMetadata(), ['id' => $this->id, 'metadata' => $metadata]);
}
/**
* Administrator only. Resets the status, attach status, and migration status for a volume. Specify the os-reset_status action in the request body.
*
* @see https://developer.openstack.org/api-ref/block-storage/v2/index.html#volume-actions-volumes-action
*/
public function resetStatus(array $options)
{
$options = array_merge($options, ['id' => $this->id]);
$this->execute($this->api->postResetStatus(), $options);
}
}