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
|
|
|
const RESOURCE_TYPE_CEPH_ACCOUNT = 'ceph_account'; |
16
|
|
|
const RESOURCE_TYPE_HOST = 'host'; |
17
|
|
|
const RESOURCE_TYPE_HOST_DISK = 'host_disk'; |
18
|
|
|
const RESOURCE_TYPE_HOST_NETWORK_INTERFACE = 'host_network_interface'; |
19
|
|
|
const RESOURCE_TYPE_IDENTITY = 'identity'; |
20
|
|
|
const RESOURCE_TYPE_IMAGE = 'image'; |
21
|
|
|
const RESOURCE_TYPE_INSTANCE = 'instance'; |
22
|
|
|
const RESOURCE_TYPE_INSTANCE_DISK = 'instance_disk'; |
23
|
|
|
const RESOURCE_TYPE_INSTANCE_NETWORK_INTERFACE = 'instance_network_interface'; |
24
|
|
|
const RESOURCE_TYPE_IPMI = 'ipmi'; |
25
|
|
|
const RESOURCE_TYPE_NETWORK = 'network'; |
26
|
|
|
const RESOURCE_TYPE_STACK = 'stack'; |
27
|
|
|
const RESOURCE_TYPE_SWIFT_ACCOUNT = 'swift_account'; |
28
|
|
|
const RESOURCE_TYPE_VOLUME = 'volume'; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
public $createdByUserId; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
public $startedAt; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
public $displayName; |
38
|
|
|
|
39
|
|
|
/** @var string */ |
40
|
|
|
public $revisionEnd; |
41
|
|
|
|
42
|
|
|
/** @var string */ |
43
|
|
|
public $userId; |
44
|
|
|
|
45
|
|
|
/** @var string */ |
46
|
|
|
public $createdByProjectId; |
47
|
|
|
|
48
|
|
|
/** @var string */ |
49
|
|
|
public $id; |
50
|
|
|
|
51
|
|
|
/** @var array */ |
52
|
|
|
public $metrics; |
53
|
|
|
|
54
|
|
|
/** @var string */ |
55
|
|
|
public $host; |
56
|
|
|
|
57
|
|
|
/** @var string */ |
58
|
|
|
public $imageRef; |
59
|
|
|
|
60
|
|
|
/** @var string */ |
61
|
|
|
public $flavorId; |
62
|
|
|
|
63
|
|
|
/** @var string */ |
64
|
|
|
public $serverGroup; |
65
|
|
|
|
66
|
|
|
/** @var string */ |
67
|
|
|
public $originalResourceId; |
68
|
|
|
|
69
|
|
|
/** @var string */ |
70
|
|
|
public $revisionStart; |
71
|
|
|
|
72
|
|
|
/** @var string */ |
73
|
|
|
public $projectId; |
74
|
|
|
|
75
|
|
|
/** @var string */ |
76
|
|
|
public $type; |
77
|
|
|
|
78
|
|
|
/** @var string */ |
79
|
|
|
public $endedAt; |
80
|
|
|
|
81
|
|
|
protected $aliases = [ |
82
|
|
|
'created_by_user_id' => 'createdByUserId', |
83
|
|
|
'started_at' => 'startedAt', |
84
|
|
|
'display_name' => 'displayName', |
85
|
|
|
'revision_end' => 'revisionEnd', |
86
|
|
|
'user_id' => 'userId', |
87
|
|
|
'created_by_project_id' => 'createdByProjectId', |
88
|
|
|
'image_ref' => 'imageRef', |
89
|
|
|
'flavor_id' => 'flavorId', |
90
|
|
|
'server_group' => 'serverGroup', |
91
|
|
|
'original_resource_id' => 'originalResourceId', |
92
|
|
|
'revision_start' => 'revisionStart', |
93
|
|
|
'project_id' => 'projectId', |
94
|
|
|
'ended_at' => 'endedAt', |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
public function retrieve() |
98
|
|
|
{ |
99
|
|
|
$response = $this->execute($this->api->getResource(), ['type' => $this->type, 'id' => $this->id]); |
100
|
|
|
$this->populateFromResponse($response); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $metric |
105
|
|
|
* |
106
|
|
|
* @return Metric |
107
|
|
|
*/ |
108
|
|
|
public function getMetric(string $metric): Metric |
109
|
|
|
{ |
110
|
|
|
$response = $this->execute( |
111
|
|
|
$this->api->getResourceMetric(), |
112
|
|
|
[ |
113
|
|
|
'resourceId' => $this->id, |
114
|
|
|
'metric' => $metric, |
115
|
|
|
'type' => $this->type, |
116
|
|
|
] |
117
|
|
|
); |
118
|
|
|
$metric = $this->model(Metric::class)->populateFromResponse($response); |
119
|
|
|
|
120
|
|
|
return $metric; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param array $options {@see \OpenStack\Metric\v1\Gnocchi\Api::getResourceMetricMeasures} |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function getMetricMeasures(array $options = []): array |
129
|
|
|
{ |
130
|
|
|
$options = array_merge( |
131
|
|
|
$options, |
132
|
|
|
[ |
133
|
|
|
'resourceId' => $this->id, |
134
|
|
|
'type' => $this->type, |
135
|
|
|
] |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
$response = $this->execute($this->api->getResourceMetricMeasures(), $options); |
139
|
|
|
|
140
|
|
|
return \GuzzleHttp\json_decode($response->getBody()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param array $options {@see \OpenStack\Metric\v1\Gnocchi\Api::getResourceMetrics} |
145
|
|
|
* |
146
|
|
|
* @return \Generator |
147
|
|
|
*/ |
148
|
|
|
public function listResourceMetrics(array $options = []): \Generator |
149
|
|
|
{ |
150
|
|
|
$options['resourceId'] = $this->id; |
151
|
|
|
|
152
|
|
|
return $this->model(Metric::class)->enumerate($this->api->getResourceMetrics(), $options); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
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: