-
Notifications
You must be signed in to change notification settings - Fork 1
/
linode.js
249 lines (236 loc) · 13.6 KB
/
linode.js
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
'use strict';
const linode_api_url = 'https://api.linode.com/v4/';
// The mothod to update linode account information UI
const update_linode_account_info = (api_key, div_id, dashboard_div_id) => {
if (api_key) {
// Load linode account information
$.ajax({
type: "GET",
url: linode_api_url + 'account',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', "Bearer " + api_key);
},
success: function (result) {
// parse result JSON
let address_1 = result['address_1']
let address_2 = result['address_2']
let city = result['city']
let postcode = result['zip']
let country = result['country']
let balance = result['balance']
let phone = result['phone']
let email = result['email']
// construct HTML for 'Linode' tab and the 'Dashboard'
let account_html_block = '<div class="row">';
let dashboard_account_html_block = '<div class="row">';
account_html_block += '<div class="col-sm-4">Address</div><div class="col-sm-8">' + address_1 + '<br/>' + address_2 + '<br/>' + city + ' ' + country + ' ' + postcode + '</div>';
account_html_block += '<div class="col-sm-4">Phone</div><div class="col-sm-8">' + phone + '</div>';
account_html_block += '<div class="col-sm-4">Email</div><div class="col-sm-8">' + email + '</div>';
account_html_block += '<div class="col-sm-4">Balance</div><div class="col-sm-8">';
dashboard_account_html_block += '<div class="col-sm-6">Linode Balance</div><div class="col-sm-6">';
if(balance<0) {
account_html_block += '<i class="fa fa-check" style="color:green;font-size:16px;"></i>';
dashboard_account_html_block += '<i class="fa fa-check" style="color:green;font-size:16px;"></i>';
}else{
account_html_block += '<i class="fa fa-question" style="color:red;font-size:16px;"></i>Insufficient balance! ';
dashboard_account_html_block += '<i class="fa fa-question" style="color:red;font-size:16px;"></i>Insufficient balance! ';
}
account_html_block += balance + '</div>';
dashboard_account_html_block += balance + '</div>';
account_html_block += '</div>';
dashboard_account_html_block += '</div>';
// Update 'Linode' tab
$("#" + div_id).html(account_html_block);
// Update Dashboard
$("#" + dashboard_div_id).html(dashboard_account_html_block);
updateStatus('Loaded account information.');
},
error: function (request, status, error) {
let html = '<span style="color:red;">Error: ' + request.responseText + '</span>';
updateStatus(html);
},
complete: function (data) {
}
});
}else{
let html = '<span style="color:red;">Invalid API key.</span>';
updateStatus(html);
}
}
// Update Linode profile UI
const update_linode_profile_info = (api_key, div_id) => {
if (api_key) {
let options = {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + api_key
}
};
fetch(linode_api_url + 'profile', options).then(function(response) {
return response.json();
}).then(function(data) {
// construct HTML for profile UI
let profile_html_block = '<div class="row">';
profile_html_block += '<div class="col-sm-4">Username</div><div class="col-sm-8">' + data['username'] + '</div>';
profile_html_block += '<div class="col-sm-4">Email</div><div class="col-sm-8">' + data['email'] + '</div>';
profile_html_block += '<div class="col-sm-4">Two factor auth enabled</div><div class="col-sm-8">' + data['two_factor_auth'] + '</div>';
profile_html_block += '<div class="col-sm-4">Referrals code</div><div class="col-sm-8">' + data['referrals']['code'] + '</div>';
profile_html_block += '<div class="col-sm-4">Referral URL</div><div class="col-sm-8">' + data['referrals']['url'] + '</div>';
profile_html_block += '<div class="col-sm-4">Referrals</div><div class="col-sm-8">';
profile_html_block += 'Completed: ' + data['referrals']['completed'] + '<br/>';
profile_html_block += 'Total: ' + data['referrals']['total'] + '<br/>';
profile_html_block += 'Pending: ' + data['referrals']['pending'] + '<br/>';
profile_html_block += 'Credit: ' + data['referrals']['credit'];
profile_html_block += '</div>';
profile_html_block += '</div>';
// Update profile UI
$("#" + div_id).html(profile_html_block);
// Update status bar
updateStatus('Loaded profile information.');
}).catch(function(err) {
let html = '<span style="color:red;">Error: ' + err + '</span>';
updateStatus(html);
});
}else{
let html = '<span style="color:red;">Invalid API key.</span>';
updateStatus(html);
}
}
// Update Linode invoices in the UI
const update_linode_invoices_info = (api_key, div_id) => {
if (api_key) {
// Load linode invoices
$.ajax({
type: "GET",
url: linode_api_url + 'account/invoices',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', "Bearer " + api_key);
},
success: function (result) {
// Construct invoice HTML
let invoice_html_block = '';
for(var s in result["data"]) { // update instances info
invoice_html_block += '<div class="row divblock">';
invoice_html_block += '<div class="col-sm-4">ID</div><div class="col-sm-8">' + result["data"][s]['id'] + '</div>';
invoice_html_block += '<div class="col-sm-4">Label</div><div class="col-sm-8">' + result["data"][s]['label'] + '</div>';
invoice_html_block += '<div class="col-sm-4">Date</div><div class="col-sm-8">' + $.format.date(result["data"][s]['date'], "yyyy-MM-dd HH:mm:ss") + '</div>';
invoice_html_block += '<div class="col-sm-4">Total</div><div class="col-sm-8">' + result["data"][s]['total'] + '</div>';
invoice_html_block += '</div>';
}
// Update invoice UI
$("#" + div_id).html(invoice_html_block);
updateStatus('Loaded invoices.');
},
error: function (request, status, error) {
let html = '<span style="color:red;">Error: ' + request.responseText + '</span>';
updateStatus(html);
},
complete: function (data) {
}
});
}else{
let html = '<span style="color:red;">Invalid API key.</span>';
updateStatus(html);
}
}
// Update Linode instances information in the UI
const update_linode_instances_info = (api_key, div_id, dashboard_div_id) => {
if (api_key) {
// Load linode instances
$.ajax({
type: "GET",
url: linode_api_url + 'linode/instances',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', "Bearer " + api_key);
},
success: function (result) {
// construct HTML for server information in 'Linode' tab and the dashboard
let server_html_block = '';
let dashboard_server_html_block = '';
for(var s in result["data"]) { // update instances info
server_html_block += '<div class="row divblock">';
dashboard_server_html_block += '<div class="row">';
server_html_block += '<div class="col-sm-4">Linode</div><div class="col-sm-8">' + result["data"][s]['label'] + '</div>';
server_html_block += '<div class="col-sm-4">ID</div><div class="col-sm-8">' + result["data"][s]['id'] + '</div>';
server_html_block += '<div class="col-sm-4">Created</div><div class="col-sm-8">' + $.format.date(result["data"][s]['created'], "yyyy-MM-dd HH:mm:ss") + '</div>';
server_html_block += '<div class="col-sm-4">Hypervisor</div><div class="col-sm-8">' + result["data"][s]['hypervisor'] + '</div>';
server_html_block += '<div class="col-sm-4">Status</div><div class="col-sm-8">';
if (result["data"][s]['status'].trim()=='running') {
server_html_block += '<i class="fa fa-check" style="color:green;font-size:16px;"></i>';
}else{
server_html_block += '<i class="fa fa-question" style="color:red;font-size:16px;"></i>';
}
// HTML in Linode dashboard
dashboard_server_html_block += '<div class="col-sm-6">Linode server: ' + result["data"][s]['label'] + '</div>';
dashboard_server_html_block += '<div class="col-sm-6">';
if (result["data"][s]['status'].trim()=='running') {
dashboard_server_html_block += '<i class="fa fa-power-off" style="color:red;font-size:16px;"></i>';
}else{
dashboard_server_html_block += '<i class="fa fa-power-off" style="color:lightgrey;font-size:16px;"></i>';
}
dashboard_server_html_block += ' ' + result["data"][s]['status'] + '</div>';
server_html_block += ' ' + result["data"][s]['status'].trim() + '</div>';
server_html_block += '<div class="col-sm-4">Region</div><div class="col-sm-8">' + result["data"][s]['region'] + '</div>';
server_html_block += '<div class="col-sm-4">IP</div><div class="col-sm-8">' + result["data"][s]['ipv4'] + '</div>';
server_html_block += '<div class="col-sm-4">Type</div><div class="col-sm-8">' + result["data"][s]['type'] + '</div>';
server_html_block += '<div class="col-sm-4">RAM</div><div class="col-sm-8">' + parseFloat(result["data"][s]['specs']['memory'])/1024 + ' GB</div>';
server_html_block += '<div class="col-sm-4">Disk</div><div class="col-sm-8">' + Math.round(parseFloat(result["data"][s]['specs']['disk'])/1024) + ' GB</div>';
server_html_block += '<div class="col-sm-4">Network</div><div class="col-sm-8">' + result["data"][s]['specs']['transfer'] + ' GB</div>';
server_html_block += '</div>';
dashboard_server_html_block += '</div>';
}
// Update server section UI in Linode 'tab'
$("#" + div_id).html(server_html_block);
// Update dashboard UI
$("#" + dashboard_div_id).html(dashboard_server_html_block);
updateStatus('Loaded instances.');
},
error: function (request, status, error) {
let html = '<span style="color:red;">Error: ' + request.responseText + '</span>';
updateStatus(html);
},
complete: function (data) {
}
});
}else{
let html = '<span style="color:red;">Invalid API key.</span>';
updateStatus(html);
}
}
// Update domain information in 'Linode' tab
const update_linode_domains_info = (api_key, div_id) => {
if (api_key) {
// Load linode domains
$.ajax({
type: "GET",
url: linode_api_url + 'domains',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', "Bearer " + api_key);
},
success: function (result) {
// Construct HTML for domain section in Linode tab
let domain_html_block = '';
for(var s in result["data"]) {
domain_html_block += '<div class="row divblock">';
domain_html_block += '<div class="col-sm-4">Domain</div><div class="col-sm-8">' + result["data"][s]['domain'] + '</div>';
domain_html_block += '<div class="col-sm-4">Description</div><div class="col-sm-8">' + result["data"][s]['description'] + '</div>';
domain_html_block += '<div class="col-sm-4">Status</div><div class="col-sm-8">' + result["data"][s]['status'] + '</div>';
domain_html_block += '<div class="col-sm-4">IPs</div><div class="col-sm-8">' + result["data"][s]['master_ips'] + '</div>';
domain_html_block += '</div>';
}
// Update domain section UI
$("#" + div_id).html(domain_html_block);
updateStatus('Loaded domains.');
},
error: function (request, status, error) {
let html = '<span style="color:red;">Error: ' + request.responseText + '</span>';
updateStatus(html);
},
complete: function (data) {
}
});
}else{
let html = '<span style="color:red;">Invalid API key.</span>';
updateStatus(html);
}
}