1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Compute\v2\Models; |
4
|
|
|
|
5
|
|
|
use OpenStack\Common\Resource\Alias; |
6
|
|
|
use OpenStack\Common\Resource\OperatorResource; |
7
|
|
|
use OpenStack\Common\Resource\Deletable; |
8
|
|
|
use OpenStack\Common\Resource\HasMetadata; |
9
|
|
|
use OpenStack\Common\Resource\Listable; |
10
|
|
|
use OpenStack\Common\Resource\Retrievable; |
11
|
|
|
use OpenStack\Common\Transport\Utils; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Represents a Compute v2 Image |
16
|
|
|
* |
17
|
|
|
* @property \OpenStack\Compute\v2\Api $api |
18
|
|
|
*/ |
19
|
|
|
class Image extends OperatorResource implements Listable, Retrievable, Deletable, HasMetadata |
20
|
|
|
{ |
21
|
|
|
/** @var string */ |
22
|
|
|
public $id; |
23
|
|
|
|
24
|
|
|
/** @var array */ |
25
|
|
|
public $links; |
26
|
|
|
|
27
|
|
|
/** @var array */ |
28
|
|
|
public $metadata; |
29
|
|
|
|
30
|
|
|
/** @var int */ |
31
|
|
|
public $minDisk; |
32
|
|
|
|
33
|
|
|
/** @var int */ |
34
|
|
|
public $minRam; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
public $name; |
38
|
|
|
|
39
|
|
|
/** @var string */ |
40
|
|
|
public $progress; |
41
|
|
|
|
42
|
|
|
/** @var string */ |
43
|
|
|
public $status; |
44
|
|
|
|
45
|
|
|
/** @var \DateTimeImmutable */ |
46
|
|
|
public $created; |
47
|
|
|
|
48
|
|
|
/** @var \DateTimeImmutable */ |
49
|
|
|
public $updated; |
50
|
|
|
|
51
|
|
|
protected $resourceKey = 'image'; |
52
|
|
|
protected $resourcesKey = 'images'; |
53
|
|
|
|
54
|
1 |
|
/** |
55
|
|
|
* @inheritdoc |
56
|
1 |
|
*/ |
57
|
1 |
|
protected function getAliases(): array |
58
|
1 |
|
{ |
59
|
|
|
return parent::getAliases() + [ |
60
|
|
|
'created' => new Alias('created', \DateTimeImmutable::class), |
61
|
|
|
'updated' => new Alias('updated', \DateTimeImmutable::class) |
62
|
|
|
]; |
63
|
1 |
|
} |
64
|
|
|
|
65
|
1 |
|
/** |
66
|
1 |
|
* {@inheritDoc} |
67
|
|
|
*/ |
68
|
|
|
public function retrieve() |
69
|
|
|
{ |
70
|
|
|
$response = $this->execute($this->api->getImage(), ['id' => (string)$this->id]); |
71
|
|
|
$this->populateFromResponse($response); |
72
|
|
|
} |
73
|
1 |
|
|
74
|
|
|
/** |
75
|
1 |
|
* {@inheritDoc} |
76
|
1 |
|
*/ |
77
|
|
|
public function delete() |
78
|
|
|
{ |
79
|
|
|
$this->execute($this->api->deleteImage(), ['id' => (string)$this->id]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Retrieves metadata from the API. |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
1 |
|
public function getMetadata(): array |
88
|
|
|
{ |
89
|
1 |
|
$response = $this->execute($this->api->getImageMetadata(), ['id' => $this->id]); |
90
|
1 |
|
return $this->parseMetadata($response); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Resets all the metadata for this image with the values provided. All existing metadata keys |
95
|
|
|
* will either be replaced or removed. |
96
|
|
|
* |
97
|
|
|
* @param array $metadata {@see \OpenStack\Compute\v2\Api::putImageMetadata} |
98
|
|
|
*/ |
99
|
|
|
public function resetMetadata(array $metadata) |
100
|
|
|
{ |
101
|
|
|
$response = $this->execute($this->api->putImageMetadata(), ['id' => $this->id, 'metadata' => $metadata]); |
102
|
1 |
|
$this->metadata = $this->parseMetadata($response); |
103
|
|
|
} |
104
|
1 |
|
|
105
|
1 |
|
/** |
106
|
|
|
* Merges the existing metadata for the image with the values provided. Any existing keys |
107
|
|
|
* referenced in the user options will be replaced with the user's new values. All other |
108
|
|
|
* existing keys will remain unaffected. |
109
|
|
|
* |
110
|
|
|
* @param array $metadata {@see \OpenStack\Compute\v2\Api::postImageMetadata} |
111
|
|
|
*/ |
112
|
|
|
public function mergeMetadata(array $metadata) |
113
|
|
|
{ |
114
|
|
|
$response = $this->execute($this->api->postImageMetadata(), ['id' => $this->id, 'metadata' => $metadata]); |
115
|
1 |
|
$this->metadata = $this->parseMetadata($response); |
116
|
|
|
} |
117
|
1 |
|
|
118
|
1 |
|
/** |
119
|
|
|
* Retrieve the value for a specific metadata key. |
120
|
|
|
* |
121
|
|
|
* @param string $key {@see \OpenStack\Compute\v2\Api::getImageMetadataKey} |
122
|
|
|
* |
123
|
|
|
* @return mixed |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function getMetadataItem(string $key) |
|
|
|
|
126
|
1 |
|
{ |
127
|
|
|
$response = $this->execute($this->api->getImageMetadataKey(), ['id' => $this->id, 'key' => $key]); |
128
|
1 |
|
$value = $this->parseMetadata($response)[$key]; |
129
|
1 |
|
$this->metadata[$key] = $value; |
130
|
|
|
return $value; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Remove a specific metadata key. |
135
|
|
|
* |
136
|
|
|
* @param string $key {@see \OpenStack\Compute\v2\Api::deleteImageMetadataKey} |
137
|
|
|
*/ |
138
|
|
View Code Duplication |
public function deleteMetadataItem(string $key) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
if (isset($this->metadata[$key])) { |
141
|
|
|
unset($this->metadata[$key]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$this->execute($this->api->deleteImageMetadataKey(), ['id' => $this->id, 'key' => $key]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function parseMetadata(ResponseInterface $response): array |
148
|
|
|
{ |
149
|
|
|
return Utils::jsonDecode($response)['metadata']; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.