-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathActions.php
More file actions
126 lines (108 loc) · 3.18 KB
/
Actions.php
File metadata and controls
126 lines (108 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace LKDev\HetznerCloud\Models\Actions;
use LKDev\HetznerCloud\APIResponse;
use LKDev\HetznerCloud\HetznerAPIClient;
use LKDev\HetznerCloud\Models\Contracts\Resources;
use LKDev\HetznerCloud\Models\Meta;
use LKDev\HetznerCloud\Models\Model;
use LKDev\HetznerCloud\Models\Servers\Server;
use LKDev\HetznerCloud\RequestOpts;
use LKDev\HetznerCloud\Traits\GetFunctionTrait;
class Actions extends Model implements Resources
{
use GetFunctionTrait;
/**
* @var
*/
protected $actions;
public function all(?RequestOpts $requestOpts = null): array
{
if ($requestOpts == null) {
$requestOpts = new RequestOpts();
}
return $this->_all($requestOpts);
}
/**
* @param RequestOpts $requestOpts
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function list(?RequestOpts $requestOpts = null): ?APIResponse
{
if ($requestOpts == null) {
$requestOpts = new RequestOpts();
}
$response = $this->httpClient->get('actions'.$requestOpts->buildQuery());
if (! HetznerAPIClient::hasError($response)) {
$resp = json_decode((string) $response->getBody());
return APIResponse::create([
'meta' => Meta::parse($resp->meta),
'actions' => self::parse($resp->{$this->_getKeys()['many']})->{$this->_getKeys()['many']},
], $response->getHeaders());
}
return null;
}
/**
* @param $actionId
* @return \LKDev\HetznerCloud\Models\Actions\Action|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function getById(int $actionId): ?Action
{
$response = $this->httpClient->get('actions/'.$actionId);
if (! HetznerAPIClient::hasError($response)) {
return Action::parse(json_decode((string) $response->getBody())->action);
}
return null;
}
public function getByName(string $name)
{
throw new \BadMethodCallException('getByName is not possible on Actions');
}
/**
* @param $input
* @return $this
*/
public function setAdditionalData($input)
{
$this->actions = array_map(function ($action) {
return Action::parse($action);
}, $input);
return $this;
}
/**
* @param $input
* @param Server $server
* @return $this|static
*/
public static function parse($input)
{
return (new self())->setAdditionalData($input);
}
/**
* Wait for an action to complete.
*
* @param Action $action
* @param float $pollingInterval in seconds
* @return bool
*
* @throws \LKDev\HetznerCloud\APIException
*/
public static function waitActionCompleted(Action $action, $pollingInterval = 0.5)
{
while ($action->status == 'running') {
usleep($pollingInterval * 1000000);
$action = $action->refresh();
}
return $action->status == 'success';
}
/**
* @return array
*/
public function _getKeys(): array
{
return ['one' => 'action', 'many' => 'actions'];
}
}