Skip to content

Commit 5f2a461

Browse files
committed
Added draft sdk for Gnocchi
1 parent 641ff74 commit 5f2a461

File tree

3 files changed

+249
-0
lines changed

3 files changed

+249
-0
lines changed

src/Metric/v1/Gnocchi/Api.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace OpenStack\Metric\v1\Gnocchi;
4+
5+
use OpenStack\Common\Api\AbstractApi;
6+
7+
class Api extends AbstractApi
8+
{
9+
private $pathPrefix = 'v1';
10+
11+
public function __construct()
12+
{
13+
$this->params = new Params();
14+
}
15+
16+
public function getResources(): array
17+
{
18+
return [
19+
'path' => $this->pathPrefix.'/resource/{type}',
20+
'method' => 'GET',
21+
'params' => [
22+
'limit' => $this->params->limit(),
23+
'marker' => $this->params->marker(),
24+
'sort' => $this->params->sort(),
25+
'type' => $this->params->resourceType(),
26+
],
27+
];
28+
}
29+
30+
public function getResource(): array
31+
{
32+
return [
33+
'path' => $this->pathPrefix.'/resource/{type}/{id}',
34+
'method' => 'GET',
35+
'params' => [
36+
'id' => $this->params->idUrl('resource'),
37+
]
38+
];
39+
}
40+
41+
public function searchResources(): array
42+
{
43+
return [
44+
'path' => $this->pathPrefix.'/search/resource/{type}',
45+
'method' => 'POST',
46+
'params' => [
47+
'limit' => $this->params->limit(),
48+
'marker' => $this->params->marker(),
49+
'sort' => $this->params->sort(),
50+
'type' => $this->params->resourceType(),
51+
'criteria' => $this->params->criteria(),
52+
'contentType' => $this->params->headerContentType(),
53+
],
54+
];
55+
}
56+
57+
public function getResourceTypes(): array
58+
{
59+
return [
60+
'path' => $this->pathPrefix.'/resource_type',
61+
'method' => 'GET',
62+
'params' => [],
63+
];
64+
}
65+
66+
public function getMetric(): array
67+
{
68+
return [
69+
'path' => $this->pathPrefix.'/metric/{id}',
70+
'method' => 'GET',
71+
'params' => [
72+
'id' => $this->params->idUrl('metric'),
73+
],
74+
];
75+
}
76+
77+
public function getMetrics(): array
78+
{
79+
return [
80+
'path' => $this->pathPrefix.'/metric',
81+
'method' => 'GET',
82+
'params' => [
83+
'limit' => $this->params->limit(),
84+
'marker' => $this->params->marker(),
85+
'sort' => $this->params->sort()
86+
],
87+
];
88+
}
89+
90+
public function getResourceMetrics(): array
91+
{
92+
return [
93+
'path' => $this->pathPrefix.'/resource/generic/{resourceId}/metric',
94+
'method' => 'GET',
95+
'params' => [
96+
'resourceId' => $this->params->idUrl('metric'),
97+
]
98+
];
99+
}
100+
101+
public function getResourceMetric(): array
102+
{
103+
return [
104+
'path' => $this->pathPrefix.'/resource/{type}/{resourceId}/metric/{metric}',
105+
'method' => 'GET',
106+
'params' => [
107+
'resourceId' => $this->params->idUrl('resource'),
108+
'metric' => $this->params->idUrl('metric'),
109+
'type' => $this->params->resourceType(),
110+
]
111+
];
112+
}
113+
}

src/Metric/v1/Gnocchi/Params.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace OpenStack\Metric\v1\Gnocchi;
4+
5+
use OpenStack\Common\Api\AbstractParams;
6+
7+
class Params extends AbstractParams
8+
{
9+
public function resourceType(): array
10+
{
11+
return [
12+
'location' => self::URL,
13+
'type' => self::STRING_TYPE,
14+
'description' => 'Resource type',
15+
'required' => true,
16+
];
17+
}
18+
19+
public function sort(): array
20+
{
21+
return [
22+
'location' => self::QUERY,
23+
'type' => self::STRING_TYPE,
24+
'description' => 'Sort criteria',
25+
];
26+
}
27+
28+
public function criteria(): array
29+
{
30+
return [
31+
'location' => self::RAW,
32+
'type' => self::STRING_TYPE,
33+
'description' => 'Filter resources based on attributes values. See http://gnocchi.xyz/stable_4.0/rest.html#searching-for-resources',
34+
];
35+
}
36+
37+
public function headerContentType(): array
38+
{
39+
return [
40+
'location' => self::HEADER,
41+
'type' => self::STRING_TYPE,
42+
'sentAs' => 'Content-Type',
43+
'description' => 'Override request header',
44+
'documented' => false,
45+
];
46+
}
47+
48+
public function idUrl($type)
49+
{
50+
return [
51+
'required' => true,
52+
'location' => self::URL,
53+
'description' => sprintf('The unique ID, or identifier, for the %s', $type)
54+
];
55+
}
56+
}

src/Metric/v1/Gnocchi/Service.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
/**
12+
* Gnocci Metric v1 Service class
13+
*
14+
* @property Api $api
15+
*
16+
* @package OpenStack\Metric\v1\Gnocchi
17+
*/
18+
class Service extends AbstractService
19+
{
20+
public function listResourceTypes(): \Generator
21+
{
22+
return $this->model(ResourceType::class)->enumerate($this->api->getResourceTypes(), []);
23+
}
24+
25+
public function listResources(array $options = []): \Generator
26+
{
27+
$this->injectGenericType($options);
28+
29+
return $this->model(Resource::class)->enumerate($this->api->getResources(), $options);
30+
}
31+
32+
public function getResource(array $options = []): Resource
33+
{
34+
$this->injectGenericType($options);
35+
36+
$resource = $this->model(Resource::class);
37+
$resource->populateFromArray($options);
38+
39+
return $resource;
40+
}
41+
42+
public function searchResources(array $options = []): \Generator
43+
{
44+
/**
45+
* $options['criteria'] must send as STRING
46+
* This will check input $options and perform json_encode if needed.
47+
*/
48+
if (isset($options['criteria']) && !is_string($options['criteria'])) {
49+
$options['criteria'] = json_encode($options['criteria']);
50+
}
51+
52+
/**
53+
* We need to manually add content-type header to this request
54+
* since searchResources method sends RAW request body.
55+
*/
56+
$options['contentType'] = 'application/json';
57+
58+
return $this->model(Resource::class)->enumerate($this->api->searchResources(), $options);
59+
}
60+
61+
public function getMetric($id): Metric
62+
{
63+
$metric = $this->model(Metric::class);
64+
$metric->populateFromArray(['id' => $id]);
65+
66+
return $metric;
67+
}
68+
69+
public function listMetrics(array $options = []): \Generator
70+
{
71+
return $this->model(Metric::class)->enumerate($this->api->getMetrics(), $options);
72+
}
73+
74+
private function injectGenericType(array &$options)
75+
{
76+
if (empty($options) || !isset($options['type'])) {
77+
$options['type'] = Resource::RESOURCE_TYPE_GENERIC;
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)