Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
31 / 31 |
|
100.00% |
12 / 12 |
CRAP | |
100.00% |
1 / 1 |
LoadBalancerService | |
100.00% |
31 / 31 |
|
100.00% |
12 / 12 |
17 | |
100.00% |
1 / 1 |
getLoadBalancer | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getLoadBalancers | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
createLoadBalancer | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
updateLoadBalancer | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
deleteLoadBalancer | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getForwardingRules | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getForwardingRule | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createForwardingRule | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
deleteForwardRule | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFirewallRules | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFirewallRule | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setRules | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Vultr\VultrPhp\Services\LoadBalancers; |
6 | |
7 | use Vultr\VultrPhp\Services\VultrService; |
8 | use Vultr\VultrPhp\Util\ListOptions; |
9 | use Vultr\VultrPhp\Util\VultrUtil; |
10 | use Vultr\VultrPhp\VultrClientException; |
11 | |
12 | /** |
13 | * Load balancer service handler, for all load-balancers endpoints. |
14 | * |
15 | * @see https://www.vultr.com/api/#tag/load-balancer |
16 | */ |
17 | class LoadBalancerService extends VultrService |
18 | { |
19 | /** |
20 | * Get load balancers on the account. |
21 | * |
22 | * @see https://www.vultr.com/api/#operation/get-load-balancer |
23 | * @param $id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
24 | * @throws LoadBalancerException |
25 | * @throws VultrException |
26 | * @return LoadBalancer |
27 | */ |
28 | public function getLoadBalancer(string $id) : LoadBalancer |
29 | { |
30 | $object = $this->getObject('load-balancers/'.$id, new LoadBalancer()); |
31 | $this->setRules($object); |
32 | return $object; |
33 | } |
34 | |
35 | /** |
36 | * Get a specific load balancer on the account. |
37 | * |
38 | * @see https://www.vultr.com/api/#operation/list-load-balancers |
39 | * @param $options - ListOptions|null - Interact via reference. |
40 | * @throws LoadBalancerException |
41 | * @return LoadBalancer[] |
42 | */ |
43 | public function getLoadBalancers(?ListOptions &$options = null) : array |
44 | { |
45 | $objects = $this->getListObjects('load-balancers', new LoadBalancer(), $options); |
46 | foreach ($objects as &$object) |
47 | { |
48 | $this->setRules($object); |
49 | } |
50 | return $objects; |
51 | } |
52 | |
53 | /** |
54 | * Create a load balancer in a particular region. |
55 | * |
56 | * @see https://www.vultr.com/api/#operation/create-load-balancer |
57 | * @param $create - LoadBalancerCreate |
58 | * @throws LoadBalancerException |
59 | * @return LoadBalancer |
60 | */ |
61 | public function createLoadBalancer(LoadBalancerCreate $create) : LoadBalancer |
62 | { |
63 | $loadbalancer = $this->createObject('load-balancers', new LoadBalancer(), $create->getPayloadParams()); |
64 | $this->setRules($loadbalancer); |
65 | |
66 | return $loadbalancer; |
67 | } |
68 | |
69 | /** |
70 | * Update information for a load balancer. All attributes are optional. If not set the attributes will not be sent to the api. |
71 | * |
72 | * @see https://www.vultr.com/api/#operation/update-load-balancer |
73 | * @param $id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
74 | * @param $update - LoadBalancerUpdate |
75 | * @throws LoadBalancerException |
76 | * @return void |
77 | */ |
78 | public function updateLoadBalancer(string $id, LoadBalancerUpdate $update) : void |
79 | { |
80 | $client = $this->getClientHandler(); |
81 | |
82 | try |
83 | { |
84 | $client->patch('load-balancers/'.$id, $update->getPayloadParams()); |
85 | } |
86 | catch (VultrClientException $e) |
87 | { |
88 | throw new LoadBalancerException('Failed to update baremetal server: '.$e->getMessage(), $e->getHTTPCode(), $e); |
89 | } |
90 | } |
91 | |
92 | /** |
93 | * Delete a load balancer on the account. |
94 | * |
95 | * @see https://www.vultr.com/api/#operation/delete-load-balancer |
96 | * @param $id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
97 | * @throws LoadBalancerException |
98 | * @return void |
99 | */ |
100 | public function deleteLoadBalancer(string $id) : void |
101 | { |
102 | $this->deleteObject('load-balancers/'.$id, new LoadBalancer()); |
103 | } |
104 | |
105 | /** |
106 | * Get forwarding rules for a specific load balancer. |
107 | * |
108 | * @see https://www.vultr.com/api/#operation/list-load-balancer-forwarding-rules |
109 | * @param $id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
110 | * @param $options - ListOptions|null - Interact via reference. |
111 | * @throws LoadBalancerException |
112 | * @return ForwardRule[] |
113 | */ |
114 | public function getForwardingRules(string $id, ?ListOptions &$options = null) : array |
115 | { |
116 | return $this->getListObjects('load-balancers/'.$id.'/forwarding-rules', new ForwardRule(), $options); |
117 | } |
118 | |
119 | /** |
120 | * Get a specific forwarding rule for on a load balancer. |
121 | * |
122 | * @see https://www.vultr.com/api/#operation/get-load-balancer-forwarding-rule |
123 | * @param $loadbalancer_id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
124 | * @param $forward_id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
125 | * @throws LoadBalancerException |
126 | * @return FowardRule |
127 | */ |
128 | public function getForwardingRule(string $loadbalancer_id, string $forward_id) : ForwardRule |
129 | { |
130 | return $this->getObject('load-balancers/'.$loadbalancer_id.'/forwarding-rules/'.$forward_id, new ForwardRule()); |
131 | } |
132 | |
133 | /** |
134 | * Create a forwarding rule for a load balancer. |
135 | * |
136 | * @see https://www.vultr.com/api/#operation/create-load-balancer-forwarding-rules |
137 | * @param $id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
138 | * @throws LoadBalancerException |
139 | * @return void |
140 | */ |
141 | public function createForwardingRule(string $id, ForwardRule $rule) : void |
142 | { |
143 | try |
144 | { |
145 | $this->getClientHandler()->post('load-balancers/'.$id.'/forwarding-rules', $rule->getInitializedProps()); |
146 | } |
147 | catch (VultrClientException $e) |
148 | { |
149 | throw new LoadBalancerException('Failed to create forwarding rule for load balancer '.$id.': '.$e->getMessage(), $e->getHTTPCode(), $e); |
150 | } |
151 | } |
152 | |
153 | /** |
154 | * Delete forwarding rule on a load balancer. |
155 | * |
156 | * @see https://www.vultr.com/api/#operation/delete-load-balancer-forwarding-rule |
157 | * @param $loadbalancer_id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
158 | * @param $forward_id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
159 | * @throws LoadBalancerException |
160 | * @return void |
161 | */ |
162 | public function deleteForwardRule(string $loadbalancer_id, string $forward_id) : void |
163 | { |
164 | $this->deleteObject('load-balancers/'.$loadbalancer_id.'/forwarding-rules/'.$forward_id, new ForwardRule()); |
165 | } |
166 | |
167 | /** |
168 | * Get firewall rules for a load balancer. |
169 | * |
170 | * @see https://www.vultr.com/api/#operation/list-loadbalancer-firewall-rules |
171 | * @param $id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
172 | * @param $options - ListOptions|null - Interact via reference. |
173 | * @throws LoadBalancerException |
174 | * @return FirewallRule[] |
175 | */ |
176 | public function getFirewallRules(string $id, ?ListOptions &$options = null) : array |
177 | { |
178 | return $this->getListObjects('load-balancers/'.$id.'/firewall-rules', new FirewallRule(), $options); |
179 | } |
180 | |
181 | /** |
182 | * Get a specific firewall rule on a load balancer. |
183 | * |
184 | * @see https://www.vultr.com/api/#operation/get-loadbalancer-firewall-rule |
185 | * @param $loadbalancer_id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
186 | * @param $firewall_id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60 |
187 | * @throws LoadBalancerException |
188 | * @return FirewallRule |
189 | */ |
190 | public function getFirewallRule(string $loadbalancer_id, string $firewall_id) : FirewallRule |
191 | { |
192 | return $this->getObject('load-balancers/'.$loadbalancer_id.'/firewall-rules/'.$firewall_id, new FirewallRule()); |
193 | } |
194 | |
195 | /** |
196 | * @throws VultrException |
197 | */ |
198 | private function setRules(LoadBalancer &$object) : void |
199 | { |
200 | $rules = []; |
201 | foreach ($object->getForwardingRules() as $rule) |
202 | { |
203 | $rules[] = VultrUtil::mapObject($rule, new ForwardRule()); |
204 | } |
205 | $object->setForwardingRules($rules); |
206 | |
207 | $rules = []; |
208 | foreach ($object->getFirewallRules() as $rule) |
209 | { |
210 | $rules[] = VultrUtil::mapObject($rule, new FirewallRule()); |
211 | } |
212 | $object->setFirewallRules($rules); |
213 | } |
214 | } |