1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Metric\v1\Gnocchi\Models; |
4
|
|
|
|
5
|
|
|
use OpenStack\Common\Resource\OperatorResource; |
6
|
|
|
use OpenStack\Common\Resource\Retrievable; |
7
|
|
|
use OpenStack\Metric\v1\Gnocchi\Api; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @property Api $api |
11
|
|
|
*/ |
12
|
|
|
class Resource extends OperatorResource implements Retrievable |
13
|
|
|
{ |
14
|
|
|
const RESOURCE_TYPE_GENERIC = 'generic'; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
public $createdByUserId; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
public $startedAt; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
public $displayName; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
public $revisionEnd; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
public $userId; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
public $createdByProjectId; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
public $id; |
36
|
|
|
|
37
|
|
|
/** @var array */ |
38
|
|
|
public $metrics; |
39
|
|
|
|
40
|
|
|
/** @var string */ |
41
|
|
|
public $host; |
42
|
|
|
|
43
|
|
|
/** @var string */ |
44
|
|
|
public $imageRef; |
45
|
|
|
|
46
|
|
|
/** @var string */ |
47
|
|
|
public $flavorId; |
48
|
|
|
|
49
|
|
|
/** @var string */ |
50
|
|
|
public $serverGroup; |
51
|
|
|
|
52
|
|
|
/** @var string */ |
53
|
|
|
public $originalResourceId; |
54
|
|
|
|
55
|
|
|
/** @var string */ |
56
|
|
|
public $revisionStart; |
57
|
|
|
|
58
|
|
|
/** @var string */ |
59
|
|
|
public $projectId; |
60
|
|
|
|
61
|
|
|
/** @var string */ |
62
|
|
|
public $type; |
63
|
|
|
|
64
|
|
|
/** @var string */ |
65
|
|
|
public $endedAt; |
66
|
|
|
|
67
|
|
|
protected $aliases = [ |
68
|
|
|
'created_by_user_id' => 'createdByUserId', |
69
|
|
|
'started_at' => 'startedAt', |
70
|
|
|
'display_name' => 'displayName', |
71
|
|
|
'revision_end' => 'revisionEnd', |
72
|
|
|
'user_id' => 'userId', |
73
|
|
|
'created_by_project_id' => 'createdByProjectId', |
74
|
|
|
'image_ref' => 'imageRef', |
75
|
|
|
'flavor_id' => 'flavorId', |
76
|
|
|
'server_group' => 'serverGroup', |
77
|
|
|
'original_resource_id' => 'originalResourceId', |
78
|
|
|
'revision_start' => 'revisionStart', |
79
|
|
|
'project_id' => 'projectId', |
80
|
|
|
'ended_at' => 'endedAt', |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
public function retrieve() |
84
|
|
|
{ |
85
|
|
|
$response = $this->execute($this->api->getResource(), ['type' => $this->type, 'id' => $this->id]); |
86
|
|
|
$this->populateFromResponse($response); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $metric |
92
|
|
|
* |
93
|
|
|
* @return Metric |
94
|
|
|
*/ |
95
|
|
|
public function getMetric(string $metric): Metric |
96
|
|
|
{ |
97
|
|
|
$response = $this->execute( |
98
|
|
|
$this->api->getResourceMetric(), |
99
|
|
|
[ |
100
|
|
|
'resourceId' => $this->id, |
101
|
|
|
'metric' => $metric, |
102
|
|
|
'type' => $this->type, |
103
|
|
|
] |
104
|
|
|
); |
105
|
|
|
$metric = $this->model(Metric::class)->populateFromResponse($response); |
106
|
|
|
|
107
|
|
|
return $metric; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $options {@see \OpenStack\Metric\v1\Gnocchi\Api::getResourceMetricMeasures} |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public function getMetricMeasures(array $options = []): array |
116
|
|
|
{ |
117
|
|
|
$options = array_merge( |
118
|
|
|
$options, |
119
|
|
|
[ |
120
|
|
|
'resourceId' => $this->id, |
121
|
|
|
'type' => $this->type, |
122
|
|
|
] |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$response = $this->execute($this->api->getResourceMetricMeasures(), $options); |
126
|
|
|
|
127
|
|
|
return \GuzzleHttp\json_decode($response->getBody()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param array $options {@see \OpenStack\Metric\v1\Gnocchi\Api::getResourceMetrics} |
132
|
|
|
* |
133
|
|
|
* @return \Generator |
134
|
|
|
*/ |
135
|
|
|
public function listResourceMetrics(array $options = []): \Generator |
136
|
|
|
{ |
137
|
|
|
$options['resourceId'] = $this->id; |
138
|
|
|
|
139
|
|
|
return $this->model(Metric::class)->enumerate($this->api->getResourceMetrics(), $options); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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: