-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathFirewall.php
More file actions
257 lines (230 loc) · 6.8 KB
/
Firewall.php
File metadata and controls
257 lines (230 loc) · 6.8 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/**
* Created by PhpStorm.
* User: lukaskammerling
* Date: 28.01.18
* Time: 20:59.
*/
namespace LKDev\HetznerCloud\Models\Firewalls;
use LKDev\HetznerCloud\APIException;
use LKDev\HetznerCloud\APIResponse;
use LKDev\HetznerCloud\HetznerAPIClient;
use LKDev\HetznerCloud\Models\Actions\Action;
use LKDev\HetznerCloud\Models\Contracts\Resource;
use LKDev\HetznerCloud\Models\Model;
use LKDev\HetznerCloud\Models\Servers\Server;
class Firewall extends Model implements Resource
{
/**
* @var int
*/
public $id;
/**
* @var string
*/
public $name;
/**
* @var string
*/
public $created;
/**
* @var array
*/
public $labels;
/**
* @var FirewallRule[]
*/
public $rules;
/**
* @var FirewallResource[]
*/
public $applied_to;
/**
* @var FirewallResource[]
*
* @deprecated Use $applied_to instead
*/
public $appliedTo;
/**
* Firewall constructor.
*
* @param int $id
* @param string $name
* @param array $rules
* @param array $appliedTo
* @param array $labels
* @param string $created
*/
public function __construct(
int $id,
string $name = '',
array $rules = [],
array $appliedTo = [],
array $labels = [],
string $created = ''
) {
$this->id = $id;
$this->name = $name;
$this->labels = $labels;
$this->created = $created;
$this->name = $name;
$this->rules = $rules;
$this->applied_to = $appliedTo;
$this->appliedTo = $appliedTo;
parent::__construct();
}
/**
* Update a Firewall.
*
* @see https://docs.hetzner.cloud/#firewalls-update-a-firewall
*
* @param array $data
* @return static|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function update(array $data): ?self
{
$response = $this->httpClient->put('firewalls/'.$this->id, [
'json' => $data,
]);
if (! HetznerAPIClient::hasError($response)) {
return self::parse(json_decode((string) $response->getBody())->firewall);
}
return null;
}
/**
* @param $input
* @return \LKDev\HetznerCloud\Models\Firewalls\Firewall|static|null
*/
public static function parse($input): ?self
{
if ($input == null) {
return null;
}
$appliedTo = [];
$rules = [];
foreach ($input->rules as $r) {
$rules[] = new FirewallRule($r->direction, $r->protocol, $r->source_ips, $r->destination_ips, (string) $r->port);
}
foreach ($input->applied_to as $a) {
if ($a->type === FirewallResource::TYPE_SERVER) {
$appliedTo[] = new FirewallResource($a->type, new Server($a->server->id));
} elseif ($a->type == FirewallResource::TYPE_LABEL_SELECTOR) {
$appliedTo[] = new FirewallResource($a->type, null, $a->label_selector->selector);
}
}
return new self($input->id, $input->name, $rules, $appliedTo, get_object_vars($input->labels), $input->created);
}
/**
* Sets the rules of a Firewall.
*
* @see https://docs.hetzner.cloud/#firewall-actions-set-rules
*
* @param FirewallRule[] $rules
* @return ?APIResponse|null
*
* @throws APIException
*/
public function setRules(array $rules): ?ApiResponse
{
$response = $this->httpClient->post('firewalls/'.$this->id.'/actions/set_rules', [
'json' => [
'rules' => collect($rules)->map(function ($r) {
return $r->toRequestSchema();
}),
],
]);
if (! HetznerAPIClient::hasError($response)) {
$payload = json_decode((string) $response->getBody());
return APIResponse::create([
'actions' => collect($payload->actions)->map(function ($action) {
return Action::parse($action);
})->toArray(),
], $response->getHeaders());
}
return null;
}
/**
* Deletes a Firewall.
*
* @see https://docs.hetzner.cloud/#firewalls-delete-a-firewall
*
* @return bool
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function delete(): bool
{
$response = $this->httpClient->delete('firewalls/'.$this->id);
if (! HetznerAPIClient::hasError($response)) {
return true;
}
return false;
}
/**
* Applies one Firewall to multiple resources.
*
* @see https://docs.hetzner.cloud/#firewall-actions-apply-to-resources
*
* @param FirewallResource[] $resources
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function applyToResources(array $resources): ?APIResponse
{
$response = $this->httpClient->post('firewalls/'.$this->id.'/actions/apply_to_resources', [
'json' => [
'apply_to' => collect($resources)->map(function ($r) {
return $r->toRequestSchema();
}),
],
]);
if (! HetznerAPIClient::hasError($response)) {
$payload = json_decode((string) $response->getBody());
return APIResponse::create([
'actions' => collect($payload->actions)->map(function ($action) {
return Action::parse($action);
})->toArray(),
], $response->getHeaders());
}
return null;
}
/**
* Removes one Firewall from multiple resources.
*
* @see https://docs.hetzner.cloud/#firewall-actions-remove-from-resources
*
* @param FirewallResource[] $resources
* @return APIResponse|null
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function removeFromResources(array $resources): ?APIResponse
{
$response = $this->httpClient->post('firewalls/'.$this->id.'/actions/remove_from_resources', [
'json' => [
'remove_from' => collect($resources)->map(function ($r) {
return $r->toRequestSchema();
}),
],
]);
if (! HetznerAPIClient::hasError($response)) {
$payload = json_decode((string) $response->getBody());
return APIResponse::create([
'actions' => collect($payload->actions)->map(function ($action) {
return Action::parse($action);
})->toArray(),
], $response->getHeaders());
}
return null;
}
/**
* @return mixed
*/
public function reload()
{
return HetznerAPIClient::$instance->firewalls()->get($this->id);
}
}