1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Images\v2\Models; |
4
|
|
|
|
5
|
|
|
use function GuzzleHttp\Psr7\uri_for; |
6
|
|
|
|
7
|
|
|
use OpenStack\Common\Resource\AbstractResource; |
8
|
|
|
use OpenStack\Common\Resource\Creatable; |
9
|
|
|
use OpenStack\Common\Resource\Deletable; |
10
|
|
|
use OpenStack\Common\Resource\Listable; |
11
|
|
|
use OpenStack\Common\Resource\Retrievable; |
12
|
|
|
use OpenStack\Common\Transport\Utils; |
13
|
|
|
use OpenStack\Images\v2\JsonPatch; |
14
|
|
|
use Psr\Http\Message\StreamInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @property \OpenStack\Images\v2\Api $api |
18
|
|
|
*/ |
19
|
|
|
class Image extends AbstractResource implements Creatable, Listable, Retrievable, Deletable |
20
|
|
|
{ |
21
|
|
|
/** @var string */ |
22
|
|
|
public $status; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
public $name; |
26
|
|
|
|
27
|
|
|
/** @var array */ |
28
|
|
|
public $tags; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
public $containerFormat; |
32
|
|
|
|
33
|
|
|
/** @var \DateTimeImmutable */ |
34
|
|
|
public $createdAt; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
public $diskFormat; |
38
|
|
|
|
39
|
|
|
/** @var \DateTimeImmutable */ |
40
|
|
|
public $updatedAt; |
41
|
|
|
|
42
|
|
|
/** @var string */ |
43
|
|
|
public $visibility; |
44
|
|
|
|
45
|
|
|
/** @var int */ |
46
|
|
|
public $minDisk; |
47
|
|
|
|
48
|
|
|
/** @var bool */ |
49
|
|
|
public $protected; |
50
|
|
|
|
51
|
|
|
/** @var string */ |
52
|
|
|
public $id; |
53
|
|
|
|
54
|
|
|
/** @var \GuzzleHttp\Psr7\Uri */ |
55
|
|
|
public $fileUri; |
56
|
|
|
|
57
|
|
|
/** @var string */ |
58
|
|
|
public $checksum; |
59
|
|
|
|
60
|
|
|
/** @var string */ |
61
|
|
|
public $ownerId; |
62
|
|
|
|
63
|
|
|
/** @var int */ |
64
|
|
|
public $size; |
65
|
|
|
|
66
|
|
|
/** @var int */ |
67
|
|
|
public $minRam; |
68
|
|
|
|
69
|
|
|
/** @var \GuzzleHttp\Psr7\Uri */ |
70
|
|
|
public $schemaUri; |
71
|
|
|
|
72
|
|
|
/** @var int */ |
73
|
|
|
public $virtualSize; |
74
|
|
|
|
75
|
|
|
private $jsonSchema; |
76
|
|
|
|
77
|
|
|
protected $aliases = [ |
78
|
|
|
'container_format' => 'containerFormat', |
79
|
|
|
'created_at' => 'createdAt', |
80
|
|
|
'disk_format' => 'diskFormat', |
81
|
|
|
'updated_at' => 'updatedAt', |
82
|
|
|
'min_disk' => 'minDisk', |
83
|
|
|
'owner' => 'ownerId', |
84
|
|
|
'min_ram' => 'minRam', |
85
|
|
|
'virtual_size' => 'virtualSize', |
86
|
|
|
]; |
87
|
|
|
|
88
|
6 |
|
public function populateFromArray(array $data) |
89
|
|
|
{ |
90
|
6 |
|
parent::populateFromArray($data); |
91
|
|
|
|
92
|
6 |
|
$baseUri = $this->getHttpBaseUrl(); |
93
|
|
|
|
94
|
6 |
|
if (isset($data['file'])) { |
95
|
5 |
|
$this->fileUri = Utils::appendPath($baseUri, $data['file']); |
96
|
5 |
|
} |
97
|
|
|
|
98
|
6 |
|
if (isset($data['schema'])) { |
99
|
5 |
|
$this->schemaUri = Utils::appendPath($baseUri, $data['schema']); |
100
|
6 |
|
} |
101
|
6 |
|
} |
102
|
|
|
|
103
|
1 |
|
public function create(array $data) |
104
|
|
|
{ |
105
|
1 |
|
$response = $this->execute($this->api->postImages(), $data); |
106
|
1 |
|
return $this->populateFromResponse($response); |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
public function retrieve() |
110
|
|
|
{ |
111
|
3 |
|
$response = $this->executeWithState($this->api->getImage()); |
112
|
3 |
|
return $this->populateFromResponse($response); |
113
|
|
|
} |
114
|
|
|
|
115
|
2 |
|
private function getSchema() |
116
|
|
|
{ |
117
|
2 |
|
if (null === $this->jsonSchema) { |
118
|
2 |
|
$response = $this->execute($this->api->getImageSchema()); |
119
|
2 |
|
$this->jsonSchema = new Schema(Utils::jsonDecode($response, false)); |
120
|
2 |
|
} |
121
|
|
|
|
122
|
2 |
|
return $this->jsonSchema; |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
public function update(array $data) |
126
|
|
|
{ |
127
|
|
|
// retrieve latest state so we can accurately produce a diff |
128
|
2 |
|
$this->retrieve(); |
129
|
|
|
|
130
|
2 |
|
$schema = $this->getSchema(); |
131
|
2 |
|
$data = (object) $data; |
132
|
|
|
|
133
|
|
|
// formulate src and des structures |
134
|
2 |
|
$des = $schema->normalizeObject($data, $this->aliases); |
135
|
2 |
|
$src = $schema->normalizeObject($this, $this->aliases); |
136
|
|
|
|
137
|
|
|
// validate user input |
138
|
2 |
|
$schema->validate($des); |
139
|
2 |
|
if (!$schema->isValid()) { |
140
|
1 |
|
throw new \RuntimeException($schema->getErrorString()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// formulate diff |
144
|
1 |
|
$patch = new JsonPatch(); |
145
|
1 |
|
$diff = $patch->disableRestrictedPropRemovals($patch->diff($src, $des), $schema->getPropertyPaths()); |
146
|
1 |
|
$json = json_encode($diff, JSON_UNESCAPED_SLASHES); |
147
|
|
|
|
148
|
|
|
// execute patch operation |
149
|
1 |
|
$response = $this->execute($this->api->patchImage(), [ |
150
|
1 |
|
'id' => $this->id, |
151
|
1 |
|
'patchDoc' => $json, |
152
|
|
|
'contentType' => 'application/openstack-images-v2.1-json-patch' |
153
|
1 |
|
]); |
154
|
|
|
|
155
|
1 |
|
return $this->populateFromResponse($response); |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
public function delete() |
159
|
|
|
{ |
160
|
1 |
|
$this->executeWithState($this->api->deleteImage()); |
161
|
1 |
|
} |
162
|
|
|
|
163
|
1 |
|
public function deactivate() |
164
|
|
|
{ |
165
|
1 |
|
$this->executeWithState($this->api->deactivateImage()); |
166
|
1 |
|
} |
167
|
|
|
|
168
|
1 |
|
public function reactivate() |
169
|
|
|
{ |
170
|
1 |
|
$this->executeWithState($this->api->reactivateImage()); |
171
|
1 |
|
} |
172
|
|
|
|
173
|
1 |
|
public function uploadData(StreamInterface $stream) |
174
|
|
|
{ |
175
|
1 |
|
$this->execute($this->api->postImageData(), [ |
176
|
1 |
|
'id' => $this->id, |
177
|
1 |
|
'data' => $stream, |
178
|
1 |
|
'contentType' => 'application/octet-stream', |
179
|
1 |
|
]); |
180
|
1 |
|
} |
181
|
|
|
|
182
|
1 |
|
public function downloadData() |
183
|
|
|
{ |
184
|
1 |
|
$response = $this->executeWithState($this->api->getImageData()); |
185
|
1 |
|
return $response->getBody(); |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
public function addMember($memberId) |
189
|
|
|
{ |
190
|
1 |
|
return $this->model(Member::class, ['imageId' => $this->id, 'id' => $memberId])->create([]); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
1 |
|
public function listMembers() |
194
|
|
|
{ |
195
|
1 |
|
return $this->model(Member::class)->enumerate($this->api->getImageMembers(), ['imageId' => $this->id]); |
196
|
|
|
} |
197
|
|
|
|
198
|
1 |
|
public function getMember($memberId) |
199
|
|
|
{ |
200
|
1 |
|
return $this->model(Member::class, ['imageId' => $this->id, 'id' => $memberId]); |
201
|
|
|
} |
202
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: