-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
166 lines (148 loc) · 5.27 KB
/
main.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
var needle = require("needle");
var os = require("os");
var config = {};
config.token = "9eb414058ee0d0e5bcb60e428cfb5e2e0ba3962599944d4dddebf1dabd47ef1d";
var headers =
{
'Content-Type':'application/json',
Authorization: 'Bearer ' + config.token
};
// Documentation for needle:
// https://github.com/tomas/needle
var client =
{
listRegions: function( onResponse )
{
needle.get("https://api.digitalocean.com/v2/regions", {headers:headers}, onResponse)
},
listImages: function( onResponse)
{
needle.get("https://api.digitalocean.com/v2/images",{headers:headers},onResponse)
},
createDroplet: function (dropletName, region, imageName, onResponse)
{
var data =
{
"name": dropletName,
"region":region,
"size":"512mb",
"image":imageName,
// Id to ssh_key already associated with account.
"ssh_keys":[625870],
"backups":false,
"ipv6":true,
"user_data":null,
"private_networking":null
}
console.log("Attempting to create: "+ JSON.stringify(data) );
needle.post("https://api.digitalocean.com/v2/droplets", data, {headers:headers,json:true}, onResponse );
},
retrieveDroplet: function (dropletId,onResponse)
{
console.log("Attempting to retrieve: dropletId="+ JSON.stringify(dropletId) );
needle.get("https://api.digitalocean.com/v2/droplets/"+dropletId, {headers:headers,json:true}, onResponse );
},
deleteDroplet: function(dropletId,onResponse){
console.log("Attempting to delete: dropletId="+ JSON.stringify(dropletId) );
needle.delete("https://api.digitalocean.com/v2/droplets/"+dropletId,null,{headers:headers,json:true}, onResponse )
}
};
// #############################################
// #1 Print out a list of available regions
// Comment out when completed.
// https://developers.digitalocean.com/#list-all-regions
// use 'slug' property
client.listRegions(function(error, response)
{
var data = response.body;
// console.log( JSON.stringify(response.body) );
for(var i=0; i<data.regions.length; i++)
{
var region =data.regions[i];
console.log(region.slug);
}
console.log(response.headers);
});
// #############################################
// #2 Extend the client object to have a listImages method
// Comment out when completed.
// https://developers.digitalocean.com/#images
// - Print out a list of available system images, that are AVAILABLE in a specified region.
// - use 'slug' property
// client.listImages(function(error,response){
// var data=response.body;
// for(var i=0;i<data.images.length;i++){
// var image= data.images[i];
// console.log(image.slug);
// }
// console.log(response.headers);
// });
// #############################################
// #3 Create an droplet with the specified name, region, and image
// Comment out when completed. ONLY RUN ONCE!!!!!
// Write down/copy droplet id.
//id=3793268
// var name = "sxu11"+os.hostname();
// var region = "nyc3"; // Fill one in from #1
// var image = "ubuntu-12-04-x64"; // Fill one in from #2
// client.createDroplet(name, region, image, function(err, resp, body)
// {
// // StatusCode 202 - Means server accepted request.
// if(!err && resp.statusCode == 202)
// {
// console.log( JSON.stringify( body, null, 3 ) );
// }
// });
// #############################################
// #4 Extend the client to retrieve information about a specified droplet.
// Comment out when done.
// https://developers.digitalocean.com/#retrieve-an-existing-droplet-by-id
// REMEMBER POST != GET
// Most importantly, print out IP address!
var dropletId = "3793268";
// client.retrieveDroplet(dropletId, function(err, resp, body)
// {
// // StatusCode 202 - Means server accepted request.
// if(!err && resp.statusCode == 202)
// {
// console.log( JSON.stringify( body, null, 3 ) );
// }
// // console.log(resp.body);
// console.log("-----------------------");
// console.log(resp.body.droplet.networks);
// });
// { v4:
// [ { ip_address: '104.236.65.51',
// netmask: '255.255.192.0',
// gateway: '104.236.64.1',
// type: 'public' } ],
// v6:
// [ { ip_address: '2604:A880:0800:0010:0000:0000:050C:7001',
// netmask: 64,
// gateway: '2604:A880:0800:0010:0000:0000:0000:0001',
// type: 'public' } ] }
// #############################################
// #5 In the command line, ping your server, make sure it is alive!
// ping xx.xx.xx.xx
// #############################################
// #6 Extend the client to DESTROY the specified droplet.
// Comment out when done.
// https://developers.digitalocean.com/#delete-a-droplet
// HINT, use the DELETE verb.
// HINT #2, needle.delete(url, data, options, callback), data needs passed as null.
// No response body will be sent back, but the response code will indicate success.
// Specifically, the response code will be a 204, which means that the action was successful with no returned body data.
// if(!err && resp.statusCode == 204)
// {
// console.log("Deleted!");
// }
// client.deleteDroplet(dropletId,function(err,resp,body){
// if(!err && resp.statusCode == 204)
// {
// console.log("Deleted!");
// }
// })
// #############################################
// #7 In the command line, ping your server, make sure it is dead!
// ping xx.xx.xx.xx
// It could be possible that digitalocean reallocated your IP address to another server, so don't fret it is still pinging.