1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
namespace OpenStack\BlockStorage\v2\Models; |
3
|
|
|
|
4
|
|
|
use OpenStack\Common\Resource\OperatorResource; |
5
|
|
|
use OpenStack\Common\Resource\Creatable; |
6
|
|
|
use OpenStack\Common\Resource\Deletable; |
7
|
|
|
use OpenStack\Common\Resource\HasMetadata; |
8
|
|
|
use OpenStack\Common\Resource\HasWaiterTrait; |
9
|
|
|
use OpenStack\Common\Resource\Listable; |
10
|
|
|
use OpenStack\Common\Resource\Retrievable; |
11
|
|
|
use OpenStack\Common\Resource\Updateable; |
12
|
|
|
use OpenStack\Common\Transport\Utils; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @property \OpenStack\BlockStorage\v2\Api $api |
17
|
|
|
*/ |
18
|
|
|
class Volume extends OperatorResource implements Creatable, Listable, Updateable, Deletable, Retrievable, HasMetadata |
19
|
|
|
{ |
20
|
|
|
use HasWaiterTrait; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
public $id; |
24
|
|
|
|
25
|
|
|
/** @var int */ |
26
|
|
|
public $size; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
public $status; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
public $name; |
33
|
|
|
|
34
|
|
|
/** @var array */ |
35
|
|
|
public $attachments; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
public $availabilityZone; |
39
|
|
|
|
40
|
|
|
/** @var \DateTimeImmutable */ |
41
|
|
|
public $createdAt; |
42
|
|
|
|
43
|
|
|
/** @var string */ |
44
|
|
|
public $description; |
45
|
|
|
|
46
|
|
|
/** @var string */ |
47
|
|
|
public $volumeTypeName; |
48
|
|
|
|
49
|
|
|
/** @var string */ |
50
|
|
|
public $snapshotId; |
51
|
|
|
|
52
|
|
|
/** @var string */ |
53
|
|
|
public $sourceVolumeId; |
54
|
|
|
|
55
|
|
|
/** @var string */ |
56
|
|
|
public $tenantId; |
57
|
|
|
|
58
|
|
|
/** @var string */ |
59
|
|
|
public $host; |
60
|
|
|
|
61
|
|
|
/** @var array */ |
62
|
|
|
public $metadata = []; |
63
|
|
|
|
64
|
|
|
protected $resourceKey = 'volume'; |
65
|
|
|
protected $resourcesKey = 'volumes'; |
66
|
|
|
protected $markerKey = 'id'; |
67
|
|
|
|
68
|
|
|
protected $aliases = [ |
69
|
|
|
'availability_zone' => 'availabilityZone', |
70
|
3 |
|
'source_volid' => 'sourceVolumeId', |
71
|
|
|
'snapshot_id' => 'snapshotId', |
72
|
3 |
|
'created_at' => 'createdAt', |
73
|
3 |
|
'volume_type' => 'volumeTypeName', |
74
|
3 |
|
'os-vol-tenant-attr:tenant_id' => 'tenantId', |
75
|
|
|
'os-vol-host-attr:host' => 'host' |
76
|
|
|
]; |
77
|
1 |
|
|
78
|
|
|
public function populateFromResponse(ResponseInterface $response): self |
79
|
1 |
|
{ |
80
|
1 |
|
parent::populateFromResponse($response); |
81
|
|
|
$this->metadata = $this->parseMetadata($response); |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function retrieve() |
86
|
|
|
{ |
87
|
|
|
$response = $this->executeWithState($this->api->getVolume()); |
88
|
1 |
|
$this->populateFromResponse($response); |
89
|
|
|
} |
90
|
1 |
|
|
91
|
1 |
|
/** |
92
|
|
|
* @param array $userOptions {@see \OpenStack\BlockStorage\v2\Api::postVolumes} |
93
|
|
|
* |
94
|
|
|
* @return Creatable |
95
|
|
|
*/ |
96
|
|
|
public function create(array $userOptions): Creatable |
97
|
1 |
|
{ |
98
|
|
|
$response = $this->execute($this->api->postVolumes(), $userOptions); |
99
|
1 |
|
return $this->populateFromResponse($response); |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
public function update() |
103
|
1 |
|
{ |
104
|
|
|
$response = $this->executeWithState($this->api->putVolume()); |
105
|
1 |
|
$this->populateFromResponse($response); |
106
|
1 |
|
} |
107
|
|
|
|
108
|
1 |
|
public function delete() |
109
|
|
|
{ |
110
|
1 |
|
$this->executeWithState($this->api->deleteVolume()); |
111
|
1 |
|
} |
112
|
1 |
|
|
113
|
|
|
public function getMetadata(): array |
114
|
|
|
{ |
115
|
1 |
|
$response = $this->executeWithState($this->api->getVolumeMetadata()); |
116
|
|
|
$this->metadata = $this->parseMetadata($response); |
117
|
1 |
|
return $this->metadata; |
118
|
1 |
|
} |
119
|
1 |
|
|
120
|
1 |
|
public function mergeMetadata(array $metadata) |
121
|
|
|
{ |
122
|
1 |
|
$this->getMetadata(); |
123
|
|
|
$this->metadata = array_merge($this->metadata, $metadata); |
124
|
1 |
|
$this->executeWithState($this->api->putVolumeMetadata()); |
125
|
1 |
|
} |
126
|
1 |
|
|
127
|
|
|
public function resetMetadata(array $metadata) |
128
|
4 |
|
{ |
129
|
|
|
$this->metadata = $metadata; |
130
|
4 |
|
$this->executeWithState($this->api->putVolumeMetadata()); |
131
|
4 |
|
} |
132
|
|
|
|
133
|
|
|
public function parseMetadata(ResponseInterface $response): array |
134
|
|
|
{ |
135
|
|
|
$json = Utils::jsonDecode($response); |
136
|
|
|
return isset($json['metadata']) ? $json['metadata'] : []; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function extend(int $size_in_gb) |
140
|
|
|
{ |
141
|
|
|
$response = $this->execute($this->api->extendVolume(), [ |
142
|
|
|
'id' => $this->id, |
143
|
|
|
'new_size' => $size_in_gb |
144
|
|
|
]); |
145
|
|
|
|
146
|
|
|
$this->populateFromResponse($response); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|