Skip to content

Commit 0b02551

Browse files
committed
Adding simple host handling
1 parent a7a8a66 commit 0b02551

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

src/Compute/v2/Api.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,4 +641,24 @@ public function getHypervisorStatistics(): array
641641
]
642642
];
643643
}
644+
645+
public function getHosts(): array
646+
{
647+
return [
648+
'method' => 'GET',
649+
'path' => 'os-hosts',
650+
'params' => [
651+
'name' => $this->params->filterName()
652+
],
653+
];
654+
}
655+
656+
public function getHost(): array
657+
{
658+
return [
659+
'method' => 'GET',
660+
'path' => 'os-hosts/{name}',
661+
'params' => ['name' => $this->params->urlId('host')]
662+
];
663+
}
644664
}

src/Compute/v2/Models/Host.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace OpenStack\Compute\v2\Models;
4+
5+
use OpenStack\Common\Resource\HasWaiterTrait;
6+
use OpenStack\Common\Resource\Creatable;
7+
use OpenStack\Common\Resource\Deletable;
8+
use OpenStack\Common\Resource\Listable;
9+
use OpenStack\Common\Resource\Retrievable;
10+
use OpenStack\Common\Resource\Updateable;
11+
use OpenStack\Common\Resource\OperatorResource;
12+
use OpenStack\Common\Transport\Utils;
13+
use OpenStack\BlockStorage\v2\Models\VolumeAttachment;
14+
use OpenStack\Compute\v2\Enum;
15+
use OpenStack\Networking\v2\Extensions\SecurityGroups\Models\SecurityGroup;
16+
use Psr\Http\Message\ResponseInterface;
17+
18+
/**
19+
* @property \OpenStack\Compute\v2\Api $api
20+
*/
21+
class Server extends OperatorResource implements
22+
Creatable,
23+
Updateable,
24+
Deletable,
25+
Retrievable,
26+
Listable
27+
{
28+
use HasWaiterTrait;
29+
30+
/** @var string */
31+
public $id;
32+
33+
/** @var string */
34+
public $name;
35+
36+
protected $resourceKey = 'os-host';
37+
protected $resourcesKey = 'os-hosts';
38+
protected $markerKey = 'id';
39+
40+
/**
41+
* {@inheritDoc}
42+
*
43+
* @param array $userOptions {@see \OpenStack\Compute\v2\Api::postServer}
44+
*/
45+
public function create(array $userOptions): Creatable
46+
{
47+
$response = $this->execute($this->api->postServer(), $userOptions);
48+
return $this->populateFromResponse($response);
49+
}
50+
51+
/**
52+
* {@inheritDoc}
53+
*/
54+
public function update()
55+
{
56+
$response = $this->execute($this->api->putServer(), $this->getAttrs(['id', 'name', 'ipv4', 'ipv6']));
57+
$this->populateFromResponse($response);
58+
}
59+
60+
/**
61+
* {@inheritDoc}
62+
*/
63+
public function delete()
64+
{
65+
$this->execute($this->api->deleteServer(), $this->getAttrs(['id']));
66+
}
67+
68+
/**
69+
* {@inheritDoc}
70+
*/
71+
public function retrieve()
72+
{
73+
$response = $this->execute($this->api->getServer(), $this->getAttrs(['id']));
74+
$this->populateFromResponse($response);
75+
}
76+
}

src/Compute/v2/Service.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,18 @@ public function getHypervisorStatistics(): HypervisorStatistic
199199
$statistics->populateFromResponse($this->execute($this->api->getHypervisorStatistics(), []));
200200
return $statistics;
201201
}
202+
203+
/**
204+
* List hosts.
205+
*
206+
* @param array $options {@see \OpenStack\Compute\v2\Api::getHosts}
207+
* @param callable $mapFn A callable function that will be invoked on every iteration of the list.
208+
*
209+
* @return \Generator
210+
*/
211+
public function listHosts(array $options = [], callable $mapFn = null): \Generator
212+
{
213+
$def = $this->api->getHosts();
214+
return $this->model(Host::class)->enumerate($def, $options, $mapFn);
215+
}
202216
}

0 commit comments

Comments
 (0)