|
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 $name |
|
101
|
|
|
* @param string $region |
|
102
|
|
|
* @param array|ForwardRuleEntity[] $forwardRules |
|
103
|
|
|
* @param string $algorithm |
|
104
|
|
|
* @param array|HealthCheckEntity[] $healthCheck |
|
105
|
|
|
* @param array|StickySessionEntity[] $stickySessions |
|
106
|
|
|
* @param bool $httpsRedirect |
|
107
|
|
|
* @param array $tags |
|
108
|
|
|
* |
|
109
|
|
|
* @throws HttpException |
|
110
|
|
|
* |
|
111
|
|
|
* @return LoadBalancerEntity |
|
112
|
|
|
*/ |
|
113
|
|
|
public function createTag( |
|
114
|
|
|
$name, |
|
115
|
|
|
$region, |
|
116
|
|
|
$forwardRules = null, |
|
117
|
|
|
$algorithm = 'round_robin', |
|
118
|
|
|
$healthCheck = [], |
|
119
|
|
|
$stickySessions = [], |
|
120
|
|
|
$httpsRedirect = false |
|
121
|
|
|
$tag = '', |
|
|
|
|
|
|
122
|
|
|
) { |
|
123
|
|
|
$data = [ |
|
124
|
|
|
'name' => $name, |
|
125
|
|
|
'algorithm' => $algorithm, |
|
126
|
|
|
'region' => $region, |
|
127
|
|
|
'forwarding_rules' => $this->formatForwardRules($forwardRules), |
|
128
|
|
|
'health_check' => $this->formatConfigurationOptions($healthCheck), |
|
129
|
|
|
'sticky_sessions' => $this->formatConfigurationOptions($stickySessions), |
|
130
|
|
|
'redirect_http_to_https' => $httpsRedirect, |
|
131
|
|
|
'tag' => $tag, |
|
132
|
|
|
]; |
|
133
|
|
|
|
|
134
|
|
|
$loadBalancer = $this->adapter->post(sprintf('%s/load_balancers', $this->endpoint), $data); |
|
135
|
|
|
|
|
136
|
|
|
$loadBalancer = json_decode($loadBalancer); |
|
137
|
|
|
|
|
138
|
|
|
return new LoadBalancerEntity($loadBalancer->load_balancer); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param string $id |
|
143
|
|
|
* @param array|LoadBalancerEntity $loadBalancerSpec |
|
144
|
|
|
* |
|
145
|
|
|
* @throws HttpException |
|
146
|
|
|
* |
|
147
|
|
|
* @return LoadBalancerEntity |
|
148
|
|
|
*/ |
|
149
|
|
|
public function update($id, $loadBalancerSpec) |
|
150
|
|
|
{ |
|
151
|
|
|
$data = $this->formatConfigurationOptions($loadBalancerSpec); |
|
152
|
|
|
|
|
153
|
|
|
$loadBalancer = $this->adapter->put(sprintf('%s/load_balancers/%s', $this->endpoint, $id), $data); |
|
154
|
|
|
|
|
155
|
|
|
$loadBalancer = json_decode($loadBalancer); |
|
156
|
|
|
|
|
157
|
|
|
return new LoadBalancerEntity($loadBalancer->load_balancer); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param string $id |
|
162
|
|
|
* |
|
163
|
|
|
* @throws HttpException |
|
164
|
|
|
*/ |
|
165
|
|
|
public function delete($id) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->adapter->delete(sprintf('%s/load_balancers/%s', $this->endpoint, $id)); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param array|AbstractEntity $forwardRules |
|
172
|
|
|
* |
|
173
|
|
|
* @return array |
|
174
|
|
|
*/ |
|
175
|
|
|
private function formatForwardRules($forwardRules) |
|
176
|
|
|
{ |
|
177
|
|
|
if (isset($forwardRules)) { |
|
178
|
|
|
return array_map(function ($rule) { |
|
179
|
|
|
return $this->formatConfigurationOptions($rule); |
|
180
|
|
|
}, $forwardRules); |
|
181
|
|
|
} else { |
|
182
|
|
|
return [ |
|
183
|
|
|
(new ForwardRuleEntity())->setStandardHttpRules()->toArray(), |
|
184
|
|
|
(new ForwardRuleEntity())->setStandardHttpsRules()->toArray(), |
|
185
|
|
|
]; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param array|AbstractEntity $config |
|
191
|
|
|
* |
|
192
|
|
|
* @return array|AbstractEntity |
|
193
|
|
|
*/ |
|
194
|
|
|
private function formatConfigurationOptions($config) |
|
195
|
|
|
{ |
|
196
|
|
|
return $config instanceof AbstractEntity ? $config->toArray() : $config; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|