-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathVolume.php
More file actions
156 lines (127 loc) · 4.11 KB
/
Volume.php
File metadata and controls
156 lines (127 loc) · 4.11 KB
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
<?php declare(strict_types=1);
namespace OpenStack\BlockStorage\v2\Models;
use OpenStack\Common\Resource\OperatorResource;
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\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 array */
public $metadata = [];
protected $resourceKey = 'volume';
protected $resourcesKey = 'volumes';
protected $markerKey = 'id';
protected $aliases = [
'availability_zone' => 'availabilityZone',
'source_volid' => 'sourceVolumeId',
'snapshot_id' => 'snapshotId',
'created_at' => 'createdAt',
'volume_type' => 'volumeTypeName',
'os-vol-tenant-attr:tenant_id' => 'tenantId',
'os-vol-host-attr:host' => 'host'
];
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}
*
* @return Creatable
*/
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 resetStatus(string $status)
{
$response = $this->execute($this->api->resetVolumeStatus(), [
'id' => $this->id,
'status' => $status
]);
$this->populateFromResponse($response);
}
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'] : [];
}
public function extend(int $size_in_gb)
{
$response = $this->execute($this->api->extendVolume(), [
'id' => $this->id,
'new_size' => $size_in_gb
]);
$this->populateFromResponse($response);
}
}