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