-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathImage.php
More file actions
155 lines (129 loc) · 4.09 KB
/
Copy pathImage.php
File metadata and controls
155 lines (129 loc) · 4.09 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
<?php
declare(strict_types=1);
namespace OpenStack\Compute\v2\Models;
use OpenStack\Common\Resource\Alias;
use OpenStack\Common\Resource\OperatorResource;
use OpenStack\Common\Resource\Deletable;
use OpenStack\Common\Resource\HasMetadata;
use OpenStack\Common\Resource\Listable;
use OpenStack\Common\Resource\Retrievable;
use OpenStack\Common\Transport\Utils;
use Psr\Http\Message\ResponseInterface;
/**
* Represents a Compute v2 Image.
*
* @property \OpenStack\Compute\v2\Api $api
*/
class Image extends OperatorResource implements Listable, Retrievable, Deletable, HasMetadata
{
/** @var string */
public $id;
/** @var array */
public $links;
/** @var array */
public $metadata;
/** @var int */
public $minDisk;
/** @var int */
public $minRam;
/** @var string */
public $name;
/** @var string */
public $progress;
/** @var string */
public $status;
/** @var \DateTimeImmutable */
public $created;
/** @var \DateTimeImmutable */
public $updated;
protected $resourceKey = 'image';
protected $resourcesKey = 'images';
/**
* {@inheritdoc}
*/
protected function getAliases(): array
{
return parent::getAliases() + [
'created' => new Alias('created', \DateTimeImmutable::class),
'updated' => new Alias('updated', \DateTimeImmutable::class),
];
}
/**
* {@inheritdoc}
*/
public function retrieve()
{
$response = $this->execute($this->api->getImage(), ['id' => (string) $this->id]);
$this->populateFromResponse($response);
}
/**
* {@inheritdoc}
*/
public function delete()
{
$this->execute($this->api->deleteImage(), ['id' => (string) $this->id]);
}
/**
* Retrieves metadata from the API.
*
* @return array
*/
public function getMetadata(): array
{
$response = $this->execute($this->api->getImageMetadata(), ['id' => $this->id]);
return $this->parseMetadata($response);
}
/**
* Resets all the metadata for this image with the values provided. All existing metadata keys
* will either be replaced or removed.
*
* @param array $metadata {@see \OpenStack\Compute\v2\Api::putImageMetadata}
*/
public function resetMetadata(array $metadata)
{
$response = $this->execute($this->api->putImageMetadata(), ['id' => $this->id, 'metadata' => $metadata]);
$this->metadata = $this->parseMetadata($response);
}
/**
* Merges the existing metadata for the image with the values provided. Any existing keys
* referenced in the user options will be replaced with the user's new values. All other
* existing keys will remain unaffected.
*
* @param array $metadata {@see \OpenStack\Compute\v2\Api::postImageMetadata}
*/
public function mergeMetadata(array $metadata)
{
$response = $this->execute($this->api->postImageMetadata(), ['id' => $this->id, 'metadata' => $metadata]);
$this->metadata = $this->parseMetadata($response);
}
/**
* Retrieve the value for a specific metadata key.
*
* @param string $key {@see \OpenStack\Compute\v2\Api::getImageMetadataKey}
*
* @return mixed
*/
public function getMetadataItem(string $key)
{
$response = $this->execute($this->api->getImageMetadataKey(), ['id' => $this->id, 'key' => $key]);
$value = $this->parseMetadata($response)[$key];
$this->metadata[$key] = $value;
return $value;
}
/**
* Remove a specific metadata key.
*
* @param string $key {@see \OpenStack\Compute\v2\Api::deleteImageMetadataKey}
*/
public function deleteMetadataItem(string $key)
{
if (isset($this->metadata[$key])) {
unset($this->metadata[$key]);
}
$this->execute($this->api->deleteImageMetadataKey(), ['id' => $this->id, 'key' => $key]);
}
public function parseMetadata(ResponseInterface $response): array
{
return Utils::jsonDecode($response)['metadata'];
}
}