-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDOAPI.php
183 lines (172 loc) · 4.74 KB
/
DOAPI.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
$root = dirname( __FILE__ );
include_once $root . '/includes.php';
class digitalocean
{
/**
* Creates the droplet array object and sends it via curl
*
* @param array $dropletData
*
* @return string $resp Droplet creation curl response
*/
public function createDroplet($dropletData)
{
manage::printMessage(1, "Building request.");
$ssh_keys = SSH_KEY_ID;
$backups = false;
$ipv6 = false;
$user_data = null;
$private_networking = null;
$volumes = null;
$tags = null;
$data = array(
"name" => $dropletData[0],
"region" => $dropletData[2],
"size" => $dropletData[1],
"image" => $dropletData[3],
"ssh_keys" => $ssh_keys,
"backups" => $backups,
"ipv6" => $ipv6,
"user_data" => $user_data,
"private_networking" => $private_networking,
"volumes" => $volumes,
"tags" => $tags
);
$data_string = json_encode($data);
manage::printMessage(1, "Establishing Connection.");
$curl = curl_init(API_URL);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . API_TOKEN,
'Content-Length: ' . strlen($data_string)
));
$resp = curl_exec($curl);
curl_close($curl);
manage::printMessage(1, "Request sent. Response Received.");
return $resp;
}
/**
* Lists current droplets attached to current API token
*
* @return array $resp Results of curl request
*/
public static function listDroplets()
{
return json_decode(digitalocean::makeRequest('?page=1&per_page=50'), true);
}
/**
* Gets the Droplet's IPv4 address via it's Droplet ID
*
* @param int $ID
*
* @return string $ret IPv4 Address
*/
public static function getDropletIPByID($ID)
{
manage::printMessage(1, "Sleeping for 5 seconds to give the API a moment to catch up.");
//Sleep required for creation process
sleep(5);
$ret = json_decode(digitalocean::makeRequest('/' . $ID), true);
if(!empty($ret['droplet']))
{
manage::printMessage(0, "Successfully found Droplet.");
return $ret['droplet']['networks']['v4'][0]['ip_address'];
}
else
{
manage::printMessage(3, "The Droplet for the ID provided can not be found.");
return null;
}
}
/**
* Destroy's a droplet by it's ID
*
* @param int $dropletID
*
* @return array $ret curl response
*/
public static function destroyDroplet($dropletID)
{
if(!preg_match('/^[1-9][0-9]*$/', $dropletID))
{
$dropletID = digitalocean::getDropletIDByName($dropletID);
if(empty($dropletID))
{
manage::printMessage(3, "Droplet not found!");
return null;
}
}
manage::printMessage(1, "Establishing Connection.");
$curl = curl_init(API_URL . '/' . $dropletID);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . API_TOKEN
));
$resp = curl_exec($curl);
$info = curl_getinfo($curl);
if($info['http_code'] == 204)
{
if(JSON_OUTPUT)
echo json_encode(array("success" => $dropletID));
}
else
{
if(JSON_OUTPUT)
echo json_encode(array("fail" => $dropletID));
}
curl_close($curl);
manage::printMessage(0, "Droplet successfully destroyed!");
return json_decode($resp, true);
}
/**
* Gets the Droplet's ID based on it's name
*
* @param string $dName Droplet's hostname
*
* @return int Droplet ID
*/
public static function getDropletIDByName($dName)
{
manage::printMessage(1, "Hitting API for all Droplets");
$ret = json_decode(digitalocean::makeRequest(""), true);
if(!empty($ret['droplets']))
{
foreach($ret['droplets'] as $idx)
{
if($idx['name'] == $dName)
{
manage::printMessage(0, "Found a matching Droplet by name.");
return $idx['id'];
}
}
}
return null;
}
/**
* Creates and sends the curl request (Will be switched to Guzzle soon)
*
* @param string $appUrl String to be appended to the API URL
*
* @return string $resp json_encoded curl response
*/
public static function makeRequest($appUrl)
{
manage::printMessage(1, "Establishing Connection.");
$curl = curl_init(API_URL . $appUrl);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . API_TOKEN
));
$resp = curl_exec($curl);
curl_close($curl);
return $resp;
}
}