|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Vultr PHP library. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Albert Leitato <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Vultr\Api; |
|
12
|
|
|
|
|
13
|
|
|
use Vultr\Entity\FirewallGroup as FirewallGroupEntity; |
|
14
|
|
|
use Vultr\Exceptions\EntityNotFoundException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Albert Leitato <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class FirewallGroup extends AbstractApi |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* List all firewall groups on the current account. |
|
23
|
|
|
* |
|
24
|
|
|
* @return FirewallGroupEntity |
|
25
|
|
|
*/ |
|
26
|
|
|
public function list() |
|
27
|
|
|
{ |
|
28
|
|
|
$response = $this->getAny(); |
|
29
|
|
|
|
|
30
|
|
|
return $this->handleResponse($response, self::class, true); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get a firewall group by its id. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $groupId ID for the firewall group |
|
37
|
|
|
* |
|
38
|
|
|
* @return FirewallGroupEntity |
|
39
|
|
|
* |
|
40
|
|
|
* @throws EntityNotFoundException |
|
41
|
|
|
**/ |
|
42
|
|
|
public function getById($groupId) |
|
43
|
|
|
{ |
|
44
|
|
|
$response = $this->getAny($groupId); |
|
45
|
|
|
$object = \json_decode($response); |
|
46
|
|
|
try { |
|
47
|
|
|
$group = $object->$groupId; |
|
48
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
49
|
|
|
throw new EntityNotFoundException($e->getMessage()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return new FirewallGroupEntity($group); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get a firewall group by its id. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $groupId ID for the firewall group |
|
59
|
|
|
* |
|
60
|
|
|
* @return string API response |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function getAny($groupId = null) |
|
63
|
|
|
{ |
|
64
|
|
|
$params = null !== $groupId ? '?FIREWALLGROUPID=%s' : ''; |
|
65
|
|
|
|
|
66
|
|
|
return $this->adapter->get(\sprintf('%s/firewall/group_list'.$params, $this->endpoint, $groupId)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Create a new firewall group on the current account. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $description Description of firewall group |
|
73
|
|
|
* |
|
74
|
|
|
* @return FirewallGroupEntity |
|
75
|
|
|
**/ |
|
76
|
|
|
public function create($description = null) |
|
77
|
|
|
{ |
|
78
|
|
|
$response = $this->adapter->post(sprintf('%s/firewall/group_create', $this->endpoint), http_build_query(compact('description'))); |
|
79
|
|
|
$object = json_decode($response); |
|
80
|
|
|
|
|
81
|
|
|
return $this->getById($object->FIREWALLGROUPID); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Change the description on a firewall group. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $groupId Firewall group to update |
|
88
|
|
|
* @param string $description Description of firewall group |
|
89
|
|
|
* |
|
90
|
|
|
* @throws HttpException |
|
91
|
|
|
*/ |
|
92
|
|
View Code Duplication |
public function setDescription($groupId, $description) |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
$data = [ |
|
95
|
|
|
'FIREWALLGROUPID' => $groupId, |
|
96
|
|
|
'description' => $description, |
|
97
|
|
|
]; |
|
98
|
|
|
$this->adapter->post(\sprintf('%s/firewall/group_set_description', $this->endpoint), $data); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Delete a firewall group. |
|
103
|
|
|
* |
|
104
|
|
|
* Use this function with caution because the firewall group being deleted |
|
105
|
|
|
* will be detached from all servers. This can result in open ports accessible |
|
106
|
|
|
* to the internet |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $groupId Firewall group to delete |
|
109
|
|
|
* |
|
110
|
|
|
* @throws HttpException |
|
111
|
|
|
*/ |
|
112
|
|
|
public function delete($groupId) |
|
113
|
|
|
{ |
|
114
|
|
|
$data = [ |
|
115
|
|
|
'FIREWALLGROUPID' => $groupId, |
|
116
|
|
|
]; |
|
117
|
|
|
$this->adapter->post(\sprintf('%s/firewall/group_delete', $this->endpoint), $data); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|