1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the DigitalOceanV2 library. |
5
|
|
|
* |
6
|
|
|
* (c) Antoine Corcy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DigitalOceanV2\Api; |
13
|
|
|
|
14
|
|
|
use DigitalOceanV2\Entity\AbstractEntity; |
15
|
|
|
use DigitalOceanV2\Entity\ForwardingRule as ForwardRuleEntity; |
16
|
|
|
use DigitalOceanV2\Entity\HealthCheck as HealthCheckEntity; |
17
|
|
|
use DigitalOceanV2\Entity\LoadBalancer as LoadBalancerEntity; |
18
|
|
|
use DigitalOceanV2\Exception\HttpException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Jacob Holmes <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class LoadBalancer extends AbstractApi |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @return LoadBalancerEntity[] |
27
|
|
|
*/ |
28
|
|
|
public function getAll() |
29
|
|
|
{ |
30
|
|
|
$loadBalancers = $this->adapter->get(sprintf('%s/load_balancers', $this->endpoint)); |
31
|
|
|
|
32
|
|
|
$loadBalancers = json_decode($loadBalancers); |
33
|
|
|
|
34
|
|
|
$this->extractMeta($loadBalancers); |
35
|
|
|
|
36
|
|
|
return array_map(function ($key) { |
37
|
|
|
return new LoadBalancerEntity($key); |
38
|
|
|
}, $loadBalancers->load_balancers); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $id |
43
|
|
|
* |
44
|
|
|
* @throws HttpException |
45
|
|
|
* |
46
|
|
|
* @return LoadBalancerEntity |
47
|
|
|
*/ |
48
|
|
|
public function getById($id) |
49
|
|
|
{ |
50
|
|
|
$loadBalancer = $this->adapter->get(sprintf('%s/load_balancers/%s', $this->endpoint, $id)); |
51
|
|
|
|
52
|
|
|
$loadBalancer = json_decode($loadBalancer); |
53
|
|
|
|
54
|
|
|
return new LoadBalancerEntity($loadBalancer->load_balancer); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $name |
59
|
|
|
* @param string $region |
60
|
|
|
* @param array|ForwardRuleEntity[] $forwardRules |
61
|
|
|
* @param string $algorithm |
62
|
|
|
* @param array|HealthCheckEntity[] $healthCheck |
63
|
|
|
* @param array|StickySessionEntity[] $stickySessions |
64
|
|
|
* @param array $dropletIds |
65
|
|
|
* @param bool $httpsRedirect |
66
|
|
|
* |
67
|
|
|
* @throws HttpException |
68
|
|
|
* |
69
|
|
|
* @return LoadBalancerEntity |
70
|
|
|
*/ |
71
|
|
|
public function create( |
72
|
|
|
$name, |
73
|
|
|
$region, |
74
|
|
|
$forwardRules = null, |
75
|
|
|
$algorithm = 'round_robin', |
76
|
|
|
$healthCheck = [], |
77
|
|
|
$stickySessions = [], |
78
|
|
|
$dropletIds = [], |
79
|
|
|
$httpsRedirect = false |
80
|
|
|
) { |
81
|
|
|
$data = [ |
82
|
|
|
'name' => $name, |
83
|
|
|
'algorithm' => $algorithm, |
84
|
|
|
'region' => $region, |
85
|
|
|
'forwarding_rules' => $this->formatForwardRules($forwardRules), |
|
|
|
|
86
|
|
|
'health_check' => $this->formatConfigurationOptions($healthCheck), |
87
|
|
|
'sticky_sessions' => $this->formatConfigurationOptions($stickySessions), |
88
|
|
|
'droplet_ids' => $dropletIds, |
89
|
|
|
'redirect_http_to_https' => $httpsRedirect, |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
$loadBalancer = $this->adapter->post(sprintf('%s/load_balancers', $this->endpoint), $data); |
93
|
|
|
|
94
|
|
|
$loadBalancer = json_decode($loadBalancer); |
95
|
|
|
|
96
|
|
|
return new LoadBalancerEntity($loadBalancer->load_balancer); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $id |
101
|
|
|
* @param array|LoadBalancerEntity $loadBalancerSpec |
102
|
|
|
* |
103
|
|
|
* @throws HttpException |
104
|
|
|
* |
105
|
|
|
* @return LoadBalancerEntity |
106
|
|
|
*/ |
107
|
|
|
public function update($id, $loadBalancerSpec) |
108
|
|
|
{ |
109
|
|
|
$data = $this->formatConfigurationOptions($loadBalancerSpec); |
110
|
|
|
|
111
|
|
|
$loadBalancer = $this->adapter->put(sprintf('%s/load_balancers/%s', $this->endpoint, $id), $data); |
|
|
|
|
112
|
|
|
|
113
|
|
|
$loadBalancer = json_decode($loadBalancer); |
114
|
|
|
|
115
|
|
|
return new LoadBalancerEntity($loadBalancer->load_balancer); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $id |
120
|
|
|
* |
121
|
|
|
* @throws HttpException |
122
|
|
|
*/ |
123
|
|
|
public function delete($id) |
124
|
|
|
{ |
125
|
|
|
$this->adapter->delete(sprintf('%s/load_balancers/%s', $this->endpoint, $id)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param array|AbstractEntity $forwardRules |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
private function formatForwardRules($forwardRules) |
134
|
|
|
{ |
135
|
|
|
if (isset($forwardRules)) { |
136
|
|
|
return array_map(function ($rule) { |
137
|
|
|
return $this->formatConfigurationOptions($rule); |
138
|
|
|
}, $forwardRules); |
139
|
|
|
} else { |
140
|
|
|
return [ |
141
|
|
|
(new ForwardRuleEntity())->setStandardHttpRules()->toArray(), |
142
|
|
|
(new ForwardRuleEntity())->setStandardHttpsRules()->toArray(), |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param array|AbstractEntity $config |
149
|
|
|
* |
150
|
|
|
* @return array|AbstractEntity |
151
|
|
|
*/ |
152
|
|
|
private function formatConfigurationOptions($config) |
153
|
|
|
{ |
154
|
|
|
return $config instanceof AbstractEntity ? $config->toArray() : $config; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.