1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Metric\v1\Gnocchi; |
4
|
|
|
|
5
|
|
|
use OpenStack\Common\Service\AbstractService; |
6
|
|
|
use OpenStack\Metric\v1\Gnocchi\Models\Metric; |
7
|
|
|
use OpenStack\Metric\v1\Gnocchi\Models\Resource; |
8
|
|
|
use OpenStack\Metric\v1\Gnocchi\Models\ResourceType; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Gnocci Metric v1 Service class |
12
|
|
|
* |
13
|
|
|
* @property Api $api |
14
|
|
|
* |
15
|
|
|
* @package OpenStack\Metric\v1\Gnocchi |
16
|
|
|
*/ |
17
|
|
|
class Service extends AbstractService |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Retrieves a collection of \OpenStack\Metric\v1\Gnocchi\Models\ResourceType type in a generator format. |
21
|
|
|
* |
22
|
|
|
* @return \Generator |
23
|
|
|
*/ |
24
|
|
|
public function listResourceTypes(): \Generator |
25
|
|
|
{ |
26
|
|
|
return $this->model(ResourceType::class)->enumerate($this->api->getResourceTypes(), []); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Retrieves a collection of \OpenStack\Metric\v1\Gnocchi\Models\Resource type in a generator format. |
31
|
|
|
* |
32
|
|
|
* @param array $options {@see \OpenStack\Metric\v1\Gnocchi\Api::getResources} |
33
|
|
|
* |
34
|
|
|
* @return \Generator |
35
|
|
|
*/ |
36
|
|
|
public function listResources(array $options = []): \Generator |
37
|
|
|
{ |
38
|
|
|
$this->injectGenericType($options); |
39
|
|
|
|
40
|
|
|
return $this->model(Resource::class)->enumerate($this->api->getResources(), $options); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Retrieves a Resource object and populates its unique identifier object. This operation will not perform a GET or |
45
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
46
|
|
|
* |
47
|
|
|
* @param array $options |
48
|
|
|
* |
49
|
|
|
* @return Resource |
50
|
|
|
*/ |
51
|
|
|
public function getResource(array $options = []): Resource |
52
|
|
|
{ |
53
|
|
|
$this->injectGenericType($options); |
54
|
|
|
|
55
|
|
|
/** @var Resource $resource */ |
56
|
|
|
$resource = $this->model(Resource::class); |
57
|
|
|
$resource->populateFromArray($options); |
58
|
|
|
|
59
|
|
|
return $resource; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Retrieves a collection of \OpenStack\Metric\v1\Gnocchi\Models\Resource type in a generator format. |
64
|
|
|
* |
65
|
|
|
* @param array $options {@see \OpenStack\Metric\v1\Gnocchi\Api::searchResources} |
66
|
|
|
* |
67
|
|
|
* @return \Generator |
68
|
|
|
*/ |
69
|
|
|
public function searchResources(array $options = []): \Generator |
70
|
|
|
{ |
71
|
|
|
$this->injectGenericType($options); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* $options['criteria'] must send as STRING |
75
|
|
|
* This will check input $options and perform json_encode if needed. |
76
|
|
|
*/ |
77
|
|
|
if (isset($options['criteria']) && !is_string($options['criteria'])) { |
78
|
|
|
$options['criteria'] = json_encode($options['criteria']); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* We need to manually add content-type header to this request |
83
|
|
|
* since searchResources method sends RAW request body. |
84
|
|
|
*/ |
85
|
|
|
$options['contentType'] = 'application/json'; |
86
|
|
|
|
87
|
|
|
return $this->model(Resource::class)->enumerate($this->api->searchResources(), $options); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Retrieves a Metric object and populates its unique identifier object. This operation will not perform a GET or |
92
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
93
|
|
|
* |
94
|
|
|
* @param string $id |
95
|
|
|
* |
96
|
|
|
* @return Metric |
97
|
|
|
*/ |
98
|
|
|
public function getMetric(string $id): Metric |
99
|
|
|
{ |
100
|
|
|
/** @var Metric $metric */ |
101
|
|
|
$metric = $this->model(Metric::class); |
102
|
|
|
$metric->populateFromArray(['id' => $id]); |
103
|
|
|
|
104
|
|
|
return $metric; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Retrieves a collection of Metric type in a generator format. |
109
|
|
|
* |
110
|
|
|
* @param array $options {@see \OpenStack\Metric\v1\Gnocchi\Api::getMetrics} |
111
|
|
|
* |
112
|
|
|
* @return \Generator |
113
|
|
|
*/ |
114
|
|
|
public function listMetrics(array $options = []): \Generator |
115
|
|
|
{ |
116
|
|
|
return $this->model(Metric::class)->enumerate($this->api->getMetrics(), $options); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* If options does not have type, this will inject $options['type'] = 'generic' |
121
|
|
|
* |
122
|
|
|
* @internal |
123
|
|
|
* @param array $options |
124
|
|
|
*/ |
125
|
|
|
private function injectGenericType(array &$options) |
126
|
|
|
{ |
127
|
|
|
if (empty($options) || !isset($options['type'])) { |
128
|
|
|
$options['type'] = Resource::RESOURCE_TYPE_GENERIC; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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: