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