|
1
|
|
|
<?php |
|
2
|
|
|
namespace Billow; |
|
3
|
|
|
use Billow\Droplets\DropletFactory; |
|
4
|
|
|
use Billow\Droplets\DropletFactoryInterface; |
|
5
|
|
|
use Billow\Actions\ActionInterface; |
|
6
|
|
|
use Billow\Droplets\DropletInterface; |
|
7
|
|
|
use Billow\Exceptions\ProvisionException; |
|
8
|
|
|
use Billow\Exceptions\DropletException; |
|
9
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Matt Frost<[email protected]> |
|
13
|
|
|
* @package Billow |
|
14
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
15
|
|
|
* @method setClient set the HTTP Client |
|
16
|
|
|
* @method getClient retrieve or create an HTTP Client |
|
17
|
|
|
* @method create create a droplet |
|
18
|
|
|
* @method retrieve retrieve a droplet by id |
|
19
|
|
|
* @method retrieveAll retrieve a paginated list of droplets |
|
20
|
|
|
* @method performAction perform a predefined action on a droplet |
|
21
|
|
|
*/ |
|
22
|
|
|
class DropletService |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Droplet Factory |
|
26
|
|
|
* |
|
27
|
|
|
* @var \Billow\Droplets\DropletFactoryInterface $factory |
|
28
|
|
|
*/ |
|
29
|
|
|
private $factory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Client to make http requests |
|
33
|
|
|
* |
|
34
|
|
|
* @var \Billow\ClientInterface $client |
|
35
|
|
|
*/ |
|
36
|
|
|
private $client; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Method to set factory |
|
40
|
|
|
* |
|
41
|
|
|
* @param \Billow\Droplets\DropletFactoryInterface $factory |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function setFactory(DropletFactoryInterface $factory) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$this->factory = $factory; |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Method to retrieve or instantiate a Droplet Factory |
|
50
|
|
|
* |
|
51
|
|
|
* @return \Billow\Droplets\DropletFactoryInterface |
|
52
|
|
|
*/ |
|
53
|
4 |
|
public function getFactory() |
|
54
|
|
|
{ |
|
55
|
4 |
|
if (!is_null($this->factory)) { |
|
56
|
1 |
|
return $this->factory; |
|
57
|
|
|
} |
|
58
|
3 |
|
return new DropletFactory(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Method to set client |
|
63
|
|
|
* |
|
64
|
|
|
* @param \Billow\ClientInterface $client |
|
65
|
|
|
*/ |
|
66
|
12 |
|
public function setClient(ClientInterface $client) |
|
67
|
|
|
{ |
|
68
|
12 |
|
$this->client = $client; |
|
69
|
12 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Method to retrieve set client or generate |
|
73
|
|
|
* a new client if one is not set |
|
74
|
|
|
* |
|
75
|
|
|
* @return \Billow\ClientInterface |
|
76
|
|
|
*/ |
|
77
|
13 |
|
public function getClient() |
|
78
|
|
|
{ |
|
79
|
13 |
|
if ($this->client === null) { |
|
80
|
1 |
|
$this->client = new Client(); |
|
81
|
|
|
} |
|
82
|
13 |
|
return $this->client; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Method to create a new Digital Ocean Droplet |
|
87
|
|
|
* |
|
88
|
|
|
* @param Array $dropletRequest |
|
89
|
|
|
* @param Array $headers |
|
90
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
91
|
|
|
* @throws \Billow\Exceptions\ProvisionException |
|
92
|
|
|
*/ |
|
93
|
4 |
|
public function create(Array $dropletRequest, Array $headers = []) |
|
94
|
|
|
{ |
|
95
|
4 |
|
$headers = $this->prepareHeaders($headers); |
|
96
|
|
|
|
|
97
|
4 |
|
$dropletJSON = json_encode($dropletRequest); |
|
98
|
4 |
|
$params = ['headers' => $headers, 'body' => $dropletJSON]; |
|
99
|
|
|
|
|
100
|
4 |
|
$client = $this->getClient(); |
|
101
|
|
|
try { |
|
102
|
4 |
|
$response = $client->post('droplets', $params); |
|
103
|
2 |
|
return $response; |
|
104
|
2 |
|
} catch (RequestException $e) { |
|
105
|
2 |
|
$message = 'Failed to provision new droplet'; |
|
106
|
2 |
|
$code = 0; |
|
107
|
2 |
|
if ($e->hasResponse()) { |
|
108
|
1 |
|
$response = $e->getResponse(); |
|
109
|
1 |
|
$message = $response->getBody(); |
|
110
|
1 |
|
$code = $response->getStatusCode(); |
|
111
|
|
|
} |
|
112
|
2 |
|
throw new ProvisionException($message, $code, $e); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Method to retrieve a droplet by id |
|
118
|
|
|
* |
|
119
|
|
|
* @param int $dropletId |
|
120
|
|
|
* @param Array $headers |
|
121
|
|
|
* @return \Billow\Droplets\Droplet |
|
122
|
|
|
* @throws \Billow\Exceptions\DropletException |
|
123
|
|
|
*/ |
|
124
|
3 |
|
public function retrieve($dropletId, Array $headers = []) |
|
125
|
|
|
{ |
|
126
|
3 |
|
$headers = $this->prepareHeaders($headers); |
|
127
|
3 |
|
$params = ['headers' => $headers]; |
|
128
|
3 |
|
$client = $this->getClient(); |
|
129
|
|
|
try { |
|
130
|
3 |
|
$response = $client->get('droplets/'.$dropletId, $params); |
|
131
|
1 |
|
$factory = $this->getFactory(); |
|
132
|
1 |
|
$dropletArray = json_decode($response->getBody(), true); |
|
133
|
1 |
|
return $factory->getDroplet($dropletArray['droplet']); |
|
134
|
2 |
|
} catch (RequestException $e) { |
|
135
|
2 |
|
$message = 'Retrieval of droplet failed'; |
|
136
|
2 |
|
$code = 0; |
|
137
|
2 |
|
if ($e->hasResponse()) { |
|
138
|
1 |
|
$response = $e->getResponse(); |
|
139
|
1 |
|
$message = $response->getBody(); |
|
140
|
1 |
|
$code = $response->getStatusCode(); |
|
141
|
|
|
} |
|
142
|
2 |
|
throw new DropletException($message, $code, $e); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Method to retrieve a paginated list of droplets |
|
148
|
|
|
* |
|
149
|
|
|
* @param Array $headers |
|
150
|
|
|
* @param int $per_page |
|
151
|
|
|
* @param int $page |
|
152
|
|
|
* @return Array |
|
153
|
|
|
*/ |
|
154
|
3 |
|
public function retrieveAll(Array $headers = [], $per_page = 25, $page = 1) |
|
155
|
|
|
{ |
|
156
|
3 |
|
$headers = $this->prepareHeaders($headers); |
|
157
|
3 |
|
$params = ['headers' => $headers]; |
|
158
|
3 |
|
$client = $this->getClient(); |
|
159
|
3 |
|
$endpoint = 'droplets?page='.$page.'&per_page='.$per_page; |
|
160
|
3 |
|
$dropletArray = []; |
|
161
|
3 |
|
$dropletArray['droplets'] = []; |
|
162
|
3 |
|
$dropletArray['meta'] = []; |
|
163
|
3 |
|
$dropletArray['links'] = []; |
|
164
|
|
|
try { |
|
165
|
3 |
|
$response = $client->get($endpoint, $params); |
|
166
|
1 |
|
$factory = $this->getFactory(); |
|
167
|
1 |
|
$responseArray = json_decode($response->getBody(), true); |
|
168
|
1 |
|
foreach ($responseArray['droplets'] as $droplet) { |
|
169
|
1 |
|
$dropletArray['droplets'][] = $factory->getDroplet($droplet); |
|
170
|
|
|
} |
|
171
|
1 |
|
$dropletArray['meta'] = $responseArray['meta']; |
|
172
|
1 |
|
$dropletArray['links'] = $responseArray['links']; |
|
173
|
1 |
|
return $dropletArray; |
|
174
|
2 |
|
} catch (RequestException $e) { |
|
175
|
2 |
|
$message = 'Retrieval of droplets failed'; |
|
176
|
2 |
|
$code = 0; |
|
177
|
2 |
|
if ($e->hasResponse()) { |
|
178
|
1 |
|
$response = $e->getResponse(); |
|
179
|
1 |
|
$message = $response->getBody(); |
|
180
|
1 |
|
$code = $response->getStatusCode(); |
|
181
|
|
|
} |
|
182
|
2 |
|
throw new DropletException($message, $code, $e); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Method to perform an action on a Digital Ocean Droplet |
|
188
|
|
|
* |
|
189
|
|
|
* @param \Billow\Droplets\DropletInterface |
|
190
|
|
|
* @param \Billow\Actions\ActionInterface $action |
|
191
|
|
|
* @param Array $headers |
|
192
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
193
|
|
|
*/ |
|
194
|
1 |
|
public function performAction(DropletInterface $droplet, ActionInterface $action, Array $headers = []) |
|
195
|
|
|
{ |
|
196
|
1 |
|
$headers = $this->prepareHeaders($headers); |
|
197
|
1 |
|
$dropletValues = $droplet->toArray(); |
|
198
|
1 |
|
$id = $dropletValues['id']; |
|
199
|
1 |
|
$action->setId($id); |
|
200
|
1 |
|
$request = $action->getRequest($headers, $action->getBody()); |
|
201
|
1 |
|
$client = $this->getClient(); |
|
202
|
1 |
|
$response = $client->send($request); |
|
203
|
1 |
|
return $response; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Method to set the default Content-type if one is not sent |
|
208
|
|
|
* |
|
209
|
|
|
* @param Array $headers |
|
210
|
|
|
* @return Array |
|
211
|
|
|
*/ |
|
212
|
11 |
|
private function prepareHeaders(array $headers) |
|
213
|
|
|
{ |
|
214
|
11 |
|
if (!isset($headers['Content-type']) && !isset($headers['Content-Type'])) { |
|
215
|
3 |
|
$headers['Content-type'] = 'application/json'; |
|
216
|
|
|
} |
|
217
|
11 |
|
return $headers; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|