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
|
|
|
public function listResourceTypes(): \Generator |
20
|
|
|
{ |
21
|
|
|
return $this->model(ResourceType::class)->enumerate($this->api->getResourceTypes(), []); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function listResources(array $options = []): \Generator |
25
|
|
|
{ |
26
|
|
|
$this->injectGenericType($options); |
27
|
|
|
|
28
|
|
|
return $this->model(Resource::class)->enumerate($this->api->getResources(), $options); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $options |
33
|
|
|
* |
34
|
|
|
* @return \OpenStack\Metric\v1\Gnocchi\Models\Resource |
35
|
|
|
*/ |
36
|
|
|
public function getResource(array $options = []): \OpenStack\Metric\v1\Gnocchi\Models\Resource |
37
|
|
|
{ |
38
|
|
|
$this->injectGenericType($options); |
39
|
|
|
|
40
|
|
|
$resource = $this->model(Resource::class); |
41
|
|
|
$resource->populateFromArray($options); |
42
|
|
|
|
43
|
|
|
return $resource; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function searchResources(array $options = []): \Generator |
47
|
|
|
{ |
48
|
|
|
$this->injectGenericType($options); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* $options['criteria'] must send as STRING |
52
|
|
|
* This will check input $options and perform json_encode if needed. |
53
|
|
|
*/ |
54
|
|
|
if (isset($options['criteria']) && !is_string($options['criteria'])) { |
55
|
|
|
$options['criteria'] = json_encode($options['criteria']); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* We need to manually add content-type header to this request |
60
|
|
|
* since searchResources method sends RAW request body. |
61
|
|
|
*/ |
62
|
|
|
$options['contentType'] = 'application/json'; |
63
|
|
|
|
64
|
|
|
return $this->model(Resource::class)->enumerate($this->api->searchResources(), $options); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getMetric($id): Metric |
68
|
|
|
{ |
69
|
|
|
$metric = $this->model(Metric::class); |
70
|
|
|
$metric->populateFromArray(['id' => $id]); |
71
|
|
|
|
72
|
|
|
return $metric; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function listMetrics(array $options = []): \Generator |
76
|
|
|
{ |
77
|
|
|
return $this->model(Metric::class)->enumerate($this->api->getMetrics(), $options); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function injectGenericType(array &$options) |
81
|
|
|
{ |
82
|
|
|
if (empty($options) || !isset($options['type'])) { |
83
|
|
|
$options['type'] = Resource::RESOURCE_TYPE_GENERIC; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: