-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinode.php
197 lines (167 loc) · 6.17 KB
/
linode.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
class Linode {
protected $_default_url = 'https://api.linode.com/';
protected $_config = null;
protected $_data = null;
protected $_spec = null;
private $api_version = null;
private $methods = array();
public function __construct($token,$url=null){
// Config Load?
$this->_config = array(
'key' => $token,
'url' => !is_null($url) ? $url : $this->_default_url
);
// Now lets make sure to initialize the spec
$this->spec();
}
public function __call($method,$args){
if(!method_exists($this,$method)){
// Don't exist!
$target = str_replace('_','.',$method);
if(isset($this->methods[$target])){
$function = $this->methods[$target];
$required = 0;
$data = array();
$i = 0;
foreach($function['PARAMETERS'] as $k => $pdata){
if($pdata['REQUIRED']){
$required++;
}
if(isset($args[$i])){
$data[$k] = $args[$i];
}
$i++;
}
if($required <= sizeof($args)){
return $this->execute($method,$data);
} else {
throw new Exception("Missing required parameters, use Linode->describe(".$method.") to figure out what's needed.");
}
} else {
throw new Exception("Linode API Function not found in spec.");
}
}
}
public function describe($command){
if(stristr($command,"_")){
$command = str_replace("_",".",$command);
}
if(isset($this->methods[$command])){
$c = $this->methods[$command];
$command = "<h2>".$command."</h2><code style=\"background-color:#FFFFFF;border:1px dotted #d9d9d9;padding:10px;display:block;\">mixed ".$command."(";
$params = "";
$closing = array();
$index = 0;
$previous_required = false;
$size = sizeof($c['PARAMETERS']);
foreach($c['PARAMETERS'] as $k => $d){
if($d['REQUIRED'] === false){
$params .= "[";
if($index > 0){
$params .= ",";
}
$previous_required = true;
$closing[] = "]";
}
$params .= " ".str_replace('numeric','int',$d['TYPE'])." $".$k." ";
$index++;
if($index >= $size){
$params .= implode("",$closing);
}
}
$command .= $params." );</code><br>";
$command .= $c['DESCRIPTION'];
$command .= "<hr>";
if($size > 0){
$command .= "<h2>Parameters</h2>";
foreach($c['PARAMETERS'] as $k => $d){
$command .= "<h3>".$k." - ".($d['REQUIRED'] === false ? 'optional' : 'required')."</h3>";
$command .= " ".$d['DESCRIPTION'];
}
}
$command .= "<hr><h3>Linode Spec Data</h3>".print_r($c,true);
echo $command;
} else {
throw new Exception("Linode API Function not found in spec.");
}
}
public function execute($command,$data=null){
if(is_array($data)){
$this->set_data($data);
}
$command = str_replace('_','.',$command);
if(isset($this->methods[$command])){
return json_decode($this->_execute($command));
} else {
throw new Exception("Linode API Function not found in spec.");
}
}
public function _execute($command){
$data = $this->_data;
$this->_data = null;
if(is_null($data)){
$data = array();
}
$data['api_action'] = $command;
$data['api_key'] = $this->_config['key'];
$request = Request::forge($this->_config['url'],'curl');
$request->set_params($data);
$response = $request->execute();
return $response;
}
public function set_data($key,$value=null){
if(is_array($key)){
foreach($key as $k=>$v){
$this->set_data($k,$v);
}
} else {
if(!is_array($this->_data)){
$this->_data = array();
}
$this->_data[$key] = $value;
}
return $this;
}
public function spec(){
// Spec stuff
$spec = $this->_execute('api.spec');
$this->_spec = json_decode($spec->response(),true);
$this->api_version = $this->_spec['DATA']['VERSION'];
$this->methods = $this->_spec['DATA']['METHODS'];
return $this->_spec;
}
}
// Ignore stuff below here, its a stub so that if you're working outside of FuelPHP that the class still works as expected.
if(!class_exists('Request')){
class Request {
public static function forge($url){
return new LinodeCurl($url);
}
}
class LinodeCurl {
private $url = null;
private $data = null;
private $request = null;
public function __construct($url){
$this->url = $url;
$this->request = curl_init();
curl_setopt($this->request,CURLOPT_URL,$this->url);
curl_setopt($this->request, CURLOPT_RETURNTRANSFER, 1);
}
public function set_method(){
return $this;
}
public function set_params($data){
$data_string = http_build_query($data);
curl_setopt($this->request,CURLOPT_POST, count($data));
curl_setopt($this->request,CURLOPT_POSTFIELDS, $data_string);
return $this;
}
public function execute(){
$data = curl_exec($this->request);
curl_close($this->request);
return $data;
}
}
}