-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathngcsWP.php
281 lines (232 loc) · 8.52 KB
/
ngcsWP.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/*
Plugin Name: NGCS_WP
Plugin Script: ngcsWP.php
Plugin URI: https://github.com/uaktags/ngcsWP
Description: Wordpress Plugin to communicate with 1and1.com's Cloud Servers
Version: 0.1
License: GPL
Author: Tim Garrity
Author URI: https://timgarrity.me
=== RELEASE NOTES ===
2015-11-14 - v0.1 - first version
*/
require 'vendor/autoload.php';
use NGCSv1\Adapter\HttpAdapter;
use NGCSv1\NGCSv1;
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Online: http://www.gnu.org/licenses/gpl.txt
*/
// uncomment next line if you need functions in external PHP script;
// include_once(dirname(__FILE__).'/some-library-in-same-folder.php');
add_action( 'admin_menu', 'ngcs_api_add_admin_menu' );
add_action( 'admin_init', 'ngcs_api_settings_init' );
function my_enqueue($hook) {
wp_enqueue_script('jquery');
wp_register_style( 'jqueryDataTableCSS', '//cdn.datatables.net/1.10.9/css/jquery.dataTables.css',false, '1.0.0' );
wp_enqueue_style( 'jqueryDataTableCSS' );
wp_register_style( '1and1CSS', plugin_dir_url( __FILE__ ) . 'inc/1and1.css',false, '1.0.0' );
wp_enqueue_style( '1and1CSS' );
wp_enqueue_script( 'jqueryDataTable', '//cdn.datatables.net/1.10.9/js/jquery.dataTables.js' );
wp_enqueue_script( '1and1Jquery', plugin_dir_url( __FILE__ ) . 'inc/jquery.js' );
wp_localize_script( '1and1Jquery', 'OneandOneParams', array('path'=>plugin_dir_url( __FILE__ )) );
}
if(isset($_GET['page'])&& $_GET['page'] == '1n1_ngcs_api')
add_action( 'admin_enqueue_scripts', 'my_enqueue' );
else
add_action( 'wp_enqueue_scripts', 'my_enqueue' );
add_action( 'wp_ajax_1and1_newserver', 'newserver_callback' );
add_action( 'wp_ajax_nopriv_1and1_newserver', 'newserver_callback' );
function newserver_callback() {
$options = get_option( 'ngcs_api_settings' );
$adapter = new HttpAdapter($options['ngcs_api_text_field_0']);
$ngcs = new NGCSv1($adapter);
$server = $ngcs->appliances();
wp_die(var_dump(json_encode($server->getAll())));
$server->create('WP_New_Server', ['vcore'=>2, 'cores_per_processor'=>1, 'ram'=>'2', 'hdds'=>array(array('size'=>2000, 'is_main'=>true))], 'B5F778B85C041347BCDCFC3172AB3F3C' );
}
function ngcs_api_add_admin_menu( ) {
add_menu_page( '1&1 NGCS API', '1&1 NGCS API', 'manage_options', '1n1_ngcs_api', 'ngcs_api_options_page' );
}
function ngcs_api_settings_init( ) {
register_setting( 'pluginPage', 'ngcs_api_settings' );
add_settings_section(
'ngcs_api_pluginPage_section',
__( 'API Options', 'ngcs-api' ),
'ngcs_api_settings_section_callback',
'pluginPage'
);
add_settings_field(
'ngcs_api_text_field_0',
__( 'API Key', 'ngcs-api' ),
'ngcs_api_text_field_0_render',
'pluginPage',
'ngcs_api_pluginPage_section'
);
}
function ngcs_api_text_field_0_render( ) {
$options = get_option( 'ngcs_api_settings' );
?>
<input type='text' name='ngcs_api_settings[ngcs_api_text_field_0]' value='<?php echo $options['ngcs_api_text_field_0']; ?>'>
<?php
}
function ngcs_api_settings_section_callback( ) {
echo __( 'This section description', 'ngcs-api' );
}
function server_management_page()
{
?>
<table id="server_table" class="display">
<thead>
<tr>
<th>Server Name</th>
<th>Status</th>
<th>OS</th>
<th>IP</th>
<th>CPU</th>
<th>Ram</th>
<th>SSD</th>
</tr>
</thead>
<tbody>
<?php
$options = get_option( 'ngcs_api_settings' );
// create an adapter with your user's API Token
// found in your CloudPanel under "Users"
$adapter = new HttpAdapter($options['ngcs_api_text_field_0']);
// create a ngcs object with the previous adapter
$ngcs = new NGCSv1($adapter);
$server = $ngcs->server();
$servers = $server->getAll();
foreach($servers as $k)
{
$hardware = $k->hardware;
$status = '<div class="circle '.$k->status['state'].'"></div>';
$appliance = $ngcs->appliances()->getById($k->image->id);
$image = $appliance->os . ' ' . $appliance->architecture . 'Bit';
$osver = $appliance->osVersion;
$ip = $k->ips['0']->ip;
echo "<tr style='height:30px;'>
<td>$k->name</td>
<td><center>$status</center></td>
<td><center><span class='os $osver'>$image</span></center></td>
<th>$ip</th>
<th>$hardware->vcore</th>
<th>$hardware->ram</th>
<th>SSD</th>
</tr>
";
}
?>
</tbody></table>
<?php
}
function new_server_page()
{
?>
<label for="server_name">Server Name:</label>
<input id="server_name" name="server_name" value="My WP Server"/><br/>
<label for="ram">Ram:</label>
<select name="ram" id="ram"> <?php
$x = 1;
while($x <= 128)
{
echo '<option value="'.$x.'">'.$x .' GB</option>';
$x++;
}
?>
</select><br/>
<label for="ssd">SSD:</label>
<select name="ssd" id="ssd">
<?php
$x = 20;
$y = 20;
while($x <= 500)
{
echo '<option value="'.$x.'">'.$x .' GB</option>';
$x += $y;
}
?>
</select><br/>
<label for="cpu">Processors:</label>
<select name="cpu" id="cpu">
<?php
$x = 1;
while($x <= 16)
{
echo '<option value="'.$x.'">'.$x.' Processors</option>';
$x++;
}
?>
</select><br/>
<label for="per">Cores per Processor:</label>
<select name="per" id="per">
<?php
$x = 1;
while($x <= 16)
{
echo '<option value="'.$x.'">'.$x.' vCores</option>';
$x++;
}
?>
</select><br/>
<label for="image">Image:</label>
<select name="image" id="image">
</select><br/>
<button type="submit" id="NEWSERVER-BTN">Submit</button>
<?php
}
function ngcs_api_options_page( )
{
?>
<form action='options.php' method='post'>
</form>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2>1&1 CloudServer Management</h2>
<?php settings_errors(); ?>
<?php
$active_tab = isset($_GET['tab']) ? $_GET['tab'] : 'api';
?>
<h2 class="nav-tab-wrapper">
<a href="?page=1n1_ngcs_api&tab=api"
class="nav-tab <?php echo $active_tab == 'api' ? 'nav-tab-active' : ''; ?>">1&1 API</a>
<a href="?page=1n1_ngcs_api&tab=server_management"
class="nav-tab <?php echo $active_tab == 'server_management' ? 'nav-tab-active' : ''; ?>">Server
Management</a>
<a href="?page=1n1_ngcs_api&tab=new_server"
class="nav-tab <?php echo $active_tab == 'new_server' ? 'nav-tab-active' : ''; ?>">Create Server</a>
</h2>
<?php
if ($active_tab == 'api') {
?>
<form method="post" action="options.php">
<?php
settings_fields('pluginPage');
do_settings_sections('pluginPage');
submit_button();
?>
</form>
<?php
} else if ($active_tab == 'server_management') {
server_management_page();
} else if ($active_tab == 'new_server') {
new_server_page();
}
?>
</div>
<?php
}
add_shortcode('show_servers_clientside', 'server_management_page');
?>