right_gogrid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,834 @@
1
+ #
2
+ # Copyright (c) 2007-2009 RightScale Inc
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ require 'cgi'
24
+ require 'benchmark'
25
+ require 'md5'
26
+ require 'active_support'
27
+ require 'rubygems'
28
+ require 'right_http_connection'
29
+
30
+ $:.unshift(File.dirname(__FILE__))
31
+ require 'benchmark_fix'
32
+ require 'support'
33
+ require 'gogrid_base'
34
+
35
+
36
+ module RightGogrid
37
+ MAJOR = 0
38
+ MINOR = 1
39
+ TINY = 0
40
+ VERSION = [MAJOR, MINOR, TINY].join('.')
41
+ end
42
+
43
+
44
+ module Rightscale #:nodoc:
45
+
46
+ class Gogrid
47
+ include RightGogridInterface
48
+
49
+ def initialize(gogrid_api_key, gogrid_secret, params={})
50
+ init gogrid_api_key, gogrid_secret, params
51
+ end
52
+
53
+ #--------------
54
+ # Images
55
+ #--------------
56
+
57
+ # GoGrid API: http://wiki.gogrid.com/wiki/index.php/API:grid.image.list
58
+ #
59
+ # Retrieve a list of existing images. Returns array of hashes describing the images or an exception:
60
+ #
61
+ # Required params:
62
+ # (none)
63
+ # Optional params:
64
+ # (none)
65
+ #
66
+ # gogrid = Rightscale::Gogrid.new(key, password)
67
+ # gogrid.list_images #=> [
68
+ # {"name"=>"centos44_32_apache22php5",
69
+ # "friendlyName"=>"CentOS 4.4 (32-bit) w/ Apache 2.2 + PHP5",
70
+ # "id"=>1,
71
+ # "isActive"=>true,
72
+ # "description"=>"CentOS 4.4 (32-bit) w/ Apache 2.2 + PHP5",
73
+ # "isPublic"=>true,
74
+ # "object"=>"serverimage",
75
+ # "location"=>"centos44_32_apache22php5_"},
76
+ # {"name"=>"rhel4_32_apache22php5",
77
+ # "friendlyName"=>"RHEL 4 (32-bit) w/ Apache 2.2 + PHP5",
78
+ # "id"=>2,
79
+ # "isActive"=>true,
80
+ # "description"=>"RHEL 4 (32-bit) w/ Apache 2.2 + PHP5",
81
+ # "isPublic"=>true,
82
+ # "object"=>"serverimage",
83
+ # "location"=>"rhel4_32_apache22php5_"},
84
+ # ...
85
+ # {"name"=>"centos51_64_postgresql",
86
+ # "friendlyName"=>"CentOS 5.1 (64-bit) w/ PostgreSQL 8.1",
87
+ # "id"=>28,
88
+ # "isActive"=>true,
89
+ # "description"=>"CentOS 5.1 (64-bit) w/ PostgreSQL 8.1",
90
+ # "isPublic"=>true,
91
+ # "object"=>"serverimage",
92
+ # "location"=>"centos51_64_postgresql_"}
93
+ # ]
94
+ #
95
+ # This method is cached.
96
+ #
97
+ def list_images
98
+ request_hash = generate_request("grid/image/list")
99
+ request_cache_or_info(:list_images, request_hash, GogridJsonParser)
100
+ rescue
101
+ on_exception
102
+ end
103
+
104
+ #--------------
105
+ # Servers
106
+ #--------------
107
+
108
+ # GoGrid API: http://wiki.gogrid.com/wiki/index.php/API:grid.server.get
109
+ #
110
+ # Retrieves one or many server objects from your list of servers. Returns array of hashes describing the servers or an exception:
111
+ #
112
+ # Required params:
113
+ # (none required...except that at least 1 of the optional ones should be set)
114
+ # Optional params:
115
+ # +:ids+ = (array of strings|int) The id(s) of the server(s) to retrieve. If multiple input id parameters are specified, the API will retrieve the set of servers whose ids match the input parameter values.
116
+ # +:names+ = (array of strings) The name(s) of the server(s) to retrieve. If multiple input name parameters are specified, the API will retrieve the set of servers whose names match the input parameter values.
117
+ # +:servers+ = (array of strings|int) The id(s) or name(s) of the server(s) to retrieve. If multiple input server parameters are specified, the API will retrieve the set of servers whose ids or names match the input parameter values.
118
+ #
119
+ # gogrid.gogrid_get_servers(:names => ["Example Web Server"]) #=> [
120
+ # {"name" => "Example Web Server",
121
+ # "os" => {...},
122
+ # "type" => {"name"=>"Web Server", ...},
123
+ # "id" => 5075,
124
+ # "description" => "Some more info here",
125
+ # "ip" => {...},
126
+ # "ram" => {...},
127
+ # "image" => {...},
128
+ # "object" => "server",
129
+ # "state" => {...}
130
+ # }]
131
+ def gogrid_get_servers(params={})
132
+ param_list = [] # An array with single-key hash entries...
133
+ [:ids, :names, :servers].each do |ptype|
134
+ items = params[ptype].to_a.flatten
135
+ item_name = ptype.to_s.chop.to_sym
136
+ param_list += items.map { |item| { item_name => item} }
137
+ end
138
+ do_request("grid/server/get", {}, param_list)
139
+ rescue
140
+ on_exception
141
+ end
142
+
143
+ # GoGrid API: http://wiki.gogrid.com/wiki/index.php/API:grid.server.list
144
+ #
145
+ # Retrieve a list of existing servers. Returns array of hashes describing the servers or an exception:
146
+ #
147
+ # Required params:
148
+ # (none)
149
+ # Optional params:
150
+ # +server_type+ = (string) name or id of the type of servers to list. With server_type one
151
+ # can filter the results of the list to display just web/app servers or just database servers. (e.g., 1234 or "Web Server")
152
+ # To list possible server.type values, call list_common_lookup with lookup set to 'server.type'
153
+ #
154
+ # gogrid.list_servers #=> [
155
+ # { "name" => "Example Web Server",
156
+ # "os" => {...},
157
+ # "type" => {"name"=>"Web Server", ...},
158
+ # "id" => 5075,
159
+ # "description" => "Some more info here",
160
+ # "ip" => {...},
161
+ # "ram" => {...},
162
+ # "image" => {...},
163
+ # "object" => "server",
164
+ # "state" => {...}
165
+ # }]
166
+ #
167
+ # This method is cached (unless server_type defined).
168
+ #
169
+ def list_servers(server_type=nil)
170
+ opts = {}
171
+ opts["server.type"] = server_type if server_type
172
+ request_hash = generate_request("grid/server/list", opts)
173
+ request_cache_or_info(:list_servers, request_hash, GogridJsonParser, !server_type)
174
+ rescue
175
+ on_exception
176
+ end
177
+
178
+ # Retrieve a list of servers. Returns array of hashes describing the servers or an exception:
179
+ # gogrid.get_servers_by_id #=> [
180
+ # {"name"=>"Example Web Server",
181
+ # "os" => {...},
182
+ # "type" => {"name"=>"Web Server", ...},
183
+ # "id" => 5075,
184
+ # "description" => "Some more info here",
185
+ # "ip" => {...},
186
+ # "ram" => {...},
187
+ # "image" => {...},
188
+ # "object" => "server",
189
+ # "state" => {...}
190
+ # }]
191
+ #
192
+ # If +list+ param is set, then retrieve information about servers with listed ids only
193
+ #
194
+ def get_servers_by_id(*list)
195
+ list.empty? ? list_servers : gogrid_get_servers(:ids => list)
196
+ end
197
+
198
+ # Retrieve a list of servers. Returns array of hashes describing the servers or an exception:
199
+ # gogrid.get_servers_by_name #=> [
200
+ # {"name"=>"Example Web Server",
201
+ # "os" => {...},
202
+ # "type" => {"name"=>"Web Server", ...},
203
+ # "id" => 5075,
204
+ # "description" => "Some more info here",
205
+ # "ip" => {...},
206
+ # "ram" => {...},
207
+ # "image" => {...},
208
+ # "object" => "server",
209
+ # "state" => {...}
210
+ # }]
211
+ #
212
+ # If +list+ param is set, then retrieve information about servers with listed names only
213
+ #
214
+ def get_servers_by_name(*list)
215
+ list.empty? ? list_servers : gogrid_get_servers(:names => list)
216
+ end
217
+
218
+ # GoGrid API: http://wiki.gogrid.com/wiki/index.php/API:grid.server.add
219
+ #
220
+ # Adds a single server to your grid. Returns array with one hash describing the new server or an exception:
221
+ #
222
+ # Required params:
223
+ # +name+ = (string) The friendly name of this server.
224
+ # +image+ = (string) The desired server image's id or name.
225
+ # To list available server images, use list_images.
226
+ # +ram+ = (string) The id or name of the desired ram option for this server.
227
+ # To list ram values, call list_common_lookup with lookup set to server.ram
228
+ # +ip+ = (strings) The initial public ip for this server.
229
+ #
230
+ # Optional params:
231
+ # +description+ = (string) Descriptive text to describe this server.
232
+ #
233
+ # gogrid.add_server(:name => "From API",
234
+ # :image => "rhel51_64_php",
235
+ # :ram => "512MB",
236
+ # :ip => "216.121.60.21",
237
+ # :description => "My first API server" ) #=> [
238
+ # {"name" =>"From API",
239
+ # "os" => { "name"=>"RHEL 5.1 (64-bit)",
240
+ # "id"=>9,
241
+ # "description"=>"RHEL Linux 5.1 (64-bit)",
242
+ # "object"=>"option" },
243
+ # "type" => { "name"=>"Web Server", ...},
244
+ # "id" => 5075,
245
+ # "description" => "My first API server",
246
+ # "ip" => {...},
247
+ # "ram" => {...},
248
+ # "image" => { "name"=>"rhel51_64_php",
249
+ # "id"=>20,
250
+ # "description"=>"RHEL 5.1 (64-bit) w/ Apache 2.2 + PHP 5.1",
251
+ # "object"=>"option" },
252
+ # "object" => "server",
253
+ # "state" => {...}
254
+ # }]
255
+ #
256
+ def add_server(name, image, ram, ip, description='' )
257
+ do_request("grid/server/add", { :name => name,
258
+ :image => image,
259
+ :ram => ram,
260
+ :ip => ip,
261
+ :description => description } )
262
+ rescue
263
+ on_exception
264
+ end
265
+
266
+
267
+ # GoGrid API: http://wiki.gogrid.com/wiki/index.php/API:grid.server.delete
268
+ #
269
+ # Deletes a single server from your grid. Returns array with one hash describing the deleted server or an exception:
270
+ #
271
+ # Required params:
272
+ # (none required...except that at least 1 of the optional ones should be set)
273
+ # Optional params:
274
+ # +:id+ = (string|int) The id of the server to delete.
275
+ # +:name+ = (string) The name of the server to delete.
276
+ # +:server+ (string|int) The id or name of the server to delete.
277
+ #
278
+ # gogrid.gogrid_delete_server(:name => ["From API"]) #=> [
279
+ # {"name" => "From API",
280
+ # "os" => {...},
281
+ # "type" => {"name"=>"Web Server", ...},
282
+ # "id" => 5075,
283
+ # "description" => "My first API server",
284
+ # "ip" => {...},
285
+ # "ram" => {...},
286
+ # "image" => {},
287
+ # "object" => "server",
288
+ # "state" => {...}
289
+ # }]
290
+ def gogrid_delete_server(params)
291
+ #TODO: ensure at least 1 arg is set? (or handle the response appropriately)
292
+ do_request("grid/server/delete", params)
293
+ rescue
294
+ on_exception
295
+ end
296
+
297
+ # Deletes server with given id from your grid. Returns array with one hash describing the deleted server or an exception:
298
+ #
299
+ # gogrid.delete_server(5075) #=> [
300
+ # {"name" => "From API",
301
+ # "os" => {...},
302
+ # "type" => {"name"=>"Web Server", ...},
303
+ # "id" => 5075,
304
+ # "description" => "My first API server",
305
+ # "ip" => {...},
306
+ # "ram" => {...},
307
+ # "image" => {},
308
+ # "object" => "server",
309
+ # "state" => {...}
310
+ # }]
311
+ def delete_server(id)
312
+ gogrid_delete_server(:id => id)
313
+ end
314
+
315
+ # Deletes server with given name from your grid. Returns array with one hash describing the deleted server or an exception:
316
+ # Note: generates an error if one or more servers share non-unique name.
317
+ #
318
+ # gogrid.delete_server_by_name('From API') #=> [
319
+ # {"name" => "From API",
320
+ # "os" => {...},
321
+ # "type" => {"name"=>"Web Server", ...},
322
+ # "id" => 5075,
323
+ # "description" => "My first API server",
324
+ # "ip" => {...},
325
+ # "ram" => {...},
326
+ # "image" => {},
327
+ # "object" => "server",
328
+ # "state" => {...}
329
+ # }]
330
+ def delete_server_by_name(name)
331
+ gogrid_delete_server(:name => name)
332
+ end
333
+
334
+ # GoGrid API: http://wiki.gogrid.com/wiki/index.php/API:grid.server.power
335
+ #
336
+ # Issues a power command to a single server in your grid or returns an exception:
337
+ #
338
+ # Required params:
339
+ # +:power+ = (string|symbol) Type of power operation to invoke. Supported types:
340
+ # :on | :start - To start a server
341
+ # :off| :stop - To stop (shutdown) a server
342
+ # :cycle | :restart - To restart a server
343
+ #
344
+ # Optional params: NOTE that while they're "optional" there needs to be at least 1 set
345
+ # +:id+ = (string|int) The id of the server to which the power opperation will be performed.
346
+ # +:name+ = (string) The name of the server to which the power opperation will be performed.
347
+ # +:server+ (string|int) The id or name of the server to which the power opperation will be performed.
348
+ #
349
+ # gogrid.gogrid_power_server(:power => "start", :name => ["From API"]) #=> [
350
+ # {"name"=>"From API",
351
+ # "os" => {...},
352
+ # "type" => {"name"=>"Web Server", ...},
353
+ # "id" => 5075,
354
+ # "description" => "My first API server",
355
+ # "ip" => {...},
356
+ # "ram" => {...},
357
+ # "image" => {},
358
+ # "object" =>"server",
359
+ # "state" => {...}
360
+ # }]
361
+ def gogrid_power_server(params)
362
+ do_request("grid/server/power", params)
363
+ rescue
364
+ on_exception
365
+ end
366
+
367
+ # Issues power command to server with given id or returns an exception:
368
+ #
369
+ # +:power+ = (string|symbol) Type of power operation to invoke. Supported types:
370
+ # :on | :start - To start a server
371
+ # :off| :stop - To stop (shutdown) a server
372
+ # :cycle | :restart - To restart a server
373
+ #
374
+ # gogrid.power_server(5075, :cycle) #=> [
375
+ # {"name"=>"From API",
376
+ # "os" => {...},
377
+ # "type" => {"name"=>"Web Server", ...},
378
+ # "id" => 5075,
379
+ # "description" => "My first API server",
380
+ # "ip" => {...},
381
+ # "ram" => {...},
382
+ # "image" => {},
383
+ # "object" =>"server",
384
+ # "state" => {...}
385
+ # }]
386
+ def power_server(id, power=:on)
387
+ gogrid_power_server(:id => id, :power => power)
388
+ end
389
+
390
+ # Issues power command to server with given name or returns an exception:
391
+ #
392
+ # +:power+ = (string|symbol) Type of power operation to invoke. Supported types:
393
+ # :on | :start - To start a server
394
+ # :off| :stop - To stop (shutdown) a server
395
+ # :cycle | :restart - To restart a server
396
+ #
397
+ # gogrid.power_server_by_name('From API', :cycle) #=> [
398
+ # {"name"=>"From API",
399
+ # "os" => {...},
400
+ # "type" => {"name"=>"Web Server", ...},
401
+ # "id" => 5075,
402
+ # "description" => "My first API server",
403
+ # "ip" => {...},
404
+ # "ram" => {...},
405
+ # "image" => {},
406
+ # "object" =>"server",
407
+ # "state" => {...}
408
+ # }]
409
+ def power_server_by_name(name, power=:on)
410
+ gogrid_power_server(:name => name, :power => power)
411
+ end
412
+
413
+ #--------------
414
+ # IPs
415
+ #--------------
416
+
417
+ # GoGrid API: http://wiki.gogrid.com/wiki/index.php/API:grid.ip.list
418
+ #
419
+ # Returns a (possibly filtered) list of available IPs in your grid or an exception.
420
+ #
421
+ # Required params:
422
+ # (none)
423
+ # Optional params:
424
+ # +:state+ = (string) Filtering parameter to limit the returned ips based on state
425
+ # e.g., "Assigned", "Unassigned"
426
+ # To list ip state values, call list_common_lookup with lookup set to ip.state
427
+ # +:type+ = (string) Filtering parameter to limit the returned ips based on type
428
+ # e.g., "Public","Private"
429
+ # To list ip type values, call list_common_lookup with lookup set to ip.type
430
+ #
431
+ # gogrid.list_ips(:type => "Public", :state => "Assigned") #=>
432
+ # [{"public"=>true,
433
+ # "id"=>138273,
434
+ # "ip"=>"216.121.60.16",
435
+ # "subnet"=>"216.121.60.16/255.255.255.240",
436
+ # "object"=>"ip"},
437
+ # ...
438
+ # {"public"=>true,
439
+ # "id"=>138288,
440
+ # "ip"=>"216.121.60.31",
441
+ # "subnet"=>"216.121.60.16/255.255.255.240",
442
+ # "object"=>"ip"
443
+ # }]
444
+ #
445
+ # This method is cached (unless state and type defined).
446
+ #
447
+ def list_ips(state=nil, type=nil)
448
+ #In this one we'll convert keys to string since they require a "." in it (not supported by symbols)
449
+ opts = {}
450
+ opts["ip.state"] = state if state
451
+ opts["ip.type"] = type if type
452
+ request_hash = generate_request("grid/ip/list", opts)
453
+ request_cache_or_info(:list_ips, request_hash, GogridJsonParser, !(state || type))
454
+ rescue
455
+ on_exception
456
+ end
457
+
458
+ #--------------
459
+ # Misc
460
+ #--------------
461
+
462
+ # GoGrid API: http://wiki.gogrid.com/wiki/index.php/API:support.password.list
463
+ #
464
+ # Returns the list all the passwords registered in the system or an exception
465
+ #
466
+ # Required params:
467
+ # (none)
468
+ # Optional params:
469
+ # (none)
470
+ #
471
+ # gogrid.list_support_passwords #=>
472
+ # [{"username"=>"root",
473
+ # "id"=>5415,
474
+ # "server"=>
475
+ # {"name"=>"From API",
476
+ # ...
477
+ # },
478
+ # "object"=>"password",
479
+ # "password"=>"abcdefghi",
480
+ # "applicationtype"=>"os"},
481
+ # {"username"=>"root",
482
+ # "id"=>5252,
483
+ # "server"=>
484
+ # {"name"=>"Example Server",
485
+ # ...
486
+ # },
487
+ # "object"=>"password",
488
+ # "password"=>"abcdefghi",
489
+ # "applicationtype"=>"os"}]
490
+ #
491
+ def list_support_passwords
492
+ request_hash = generate_request("support/password/list")
493
+ request_cache_or_info(:list_support_passwords, request_hash, GogridJsonParser)
494
+ rescue
495
+ on_exception
496
+ end
497
+
498
+ # GoGrid API: http://wiki.gogrid.com/wiki/index.php/API:support.password.get
499
+ #
500
+ # Returns a single password registered in the system or an exception
501
+ #
502
+ # Required params:
503
+ # +id+ = (string) The id of the password to retrieve
504
+ #
505
+ # gogrid.get_support_password (:id => 5415) #=>
506
+ # [{"username"=>"root",
507
+ # "id"=>5415,
508
+ # "server"=>
509
+ # {"name"=>"From API",
510
+ # ...
511
+ # },
512
+ # "object"=>"password",
513
+ # "password"=>"abcdefghi",
514
+ # "applicationtype"=>"os"}]
515
+ #
516
+ def get_support_password(id)
517
+ do_request("support/password/get", :id => id)
518
+ rescue
519
+ on_exception
520
+ end
521
+
522
+ # GoGrid API: http://wiki.gogrid.com/wiki/index.php/API:common.lookup.list
523
+ #
524
+ # Returns the list of options for a given lookup or an exception.
525
+ # To list all the available lookups, set the parameter lookup to lookups.
526
+ #
527
+ # Required params:
528
+ # +lookup+ = (string) the type of lookup
529
+ # If set to "lookups" the call returns all the available lookups
530
+ # Optional params:
531
+ # +sort+ = (string|symbol) the sort field [:id | :name | :description]
532
+ # +asc+ = (bool) if ordering in ascending mode [ :true | :false ]
533
+ #
534
+ # gogrid.list_common_lookup(:lookup => 'server.type', :sort => :name) #=>
535
+ # [{"name"=>"Database Server",
536
+ # "id"=>2,
537
+ # "description"=>
538
+ # "This server does not have a public connection to the Internet.",
539
+ # "object"=>"option"},
540
+ # {"name"=>"Web Server",
541
+ # "id"=>1,
542
+ # "description"=>"This server has a public connection to the Internet.",
543
+ # "object"=>"option"}]
544
+ #
545
+ # This method is cached (unless lookup, sort and asc defined).
546
+ #
547
+ def list_common_lookup(lookup='lookups', sort=nil, asc=nil)
548
+ opts = { :lookup => lookup }
549
+ opts[:sort] = sort if sort
550
+ opts[:asc] = asc if asc
551
+ request_hash = generate_request("common/lookup/list", opts)
552
+ request_cache_or_info(:list_common_lookup, request_hash, GogridJsonParser, lookup=='lookups' && !(sort || asc))
553
+ rescue
554
+ on_exception
555
+ end
556
+
557
+ #--------------
558
+ # Balancers
559
+ #--------------
560
+
561
+ # GoGrid API: http://wiki.gogrid.com/wiki/index.php/API:grid.loadbalancer.get
562
+ #
563
+ # Retrieves one or many of your loadbalancers. Returns array of hashes describing the loadbalancers or an exception:
564
+ #
565
+ # Required params:
566
+ # (none required...except that at least 1 of the optional ones should be set)
567
+ # Optional params:
568
+ # +:ids+ = (array of strings|int) The id(s) of the loadbalancer(s) to retrieve. If multiple input id parameters are specified, the API will retrieve the set of loadbalancers whose ids match the input parameter values.
569
+ # +:names+ = (array of strings) The name(s) of the loadbalancer(s) to retrieve. If multiple input name parameters are specified, the API will retrieve the set of loadbalancers whose names match the input parameter values.
570
+ # +:loadbalancers+ = (array of strings|int) The id(s) or name(s) of the loadbalancer(s) to retrieve. If multiple input loadbalancer parameters are specified, the API will retrieve the set of loadbalancers whose ids or names match the input parameter values.
571
+ #
572
+ # gogrid.gogrid_get_loadbalancers(:names => ["API LB"]) #=> [
573
+ # [{ "name" => "API LB",
574
+ # "id" => 1,
575
+ # "realiplist" => [{"port"=>443,"ip"=> {...},
576
+ # {"port"=>8080,"ip"=> {...}],
577
+ # "os" => {"name"=>"F5", ...},
578
+ # "type" => {"name"=>"Round Robin", ...},
579
+ # "virtualip" => {"port"=>80,"ip"=> {...}},
580
+ # "persistence" => {"name"=>"None", ...},
581
+ # "object" => "loadbalancer",
582
+ # "state" => {"name"=>"On", ...}
583
+ # }]
584
+ #
585
+ def gogrid_get_loadbalancers(params={})
586
+ param_list = [] # An array with single-key hash entries...
587
+ [:ids, :names, :loadbalancers].each do |ptype|
588
+ items = params[ptype].to_a.flatten
589
+ item_name = ptype.to_s.chop.to_sym
590
+ param_list += items.map { |item| { item_name => item} }
591
+ end
592
+ do_request("grid/loadbalancer/get", {}, param_list)
593
+ rescue
594
+ on_exception
595
+ end
596
+
597
+ # GoGrid API: http://wiki.gogrid.com/wiki/index.php/API:grid.loadbalancer.list
598
+ #
599
+ # Returns the list of all loadbalancers in the system or an exception
600
+ #
601
+ # Required params:
602
+ # (none)
603
+ # Optional params:
604
+ # (none)
605
+ #
606
+ # gogrid.list_loadbalancers #=>
607
+ # [{ "name" => "API LB",
608
+ # "id" => 1,
609
+ # "realiplist" => [{"port"=>443,"ip"=> {...},
610
+ # {"port"=>8080,"ip"=> {...}],
611
+ # "os" => {"name"=>"F5", ...},
612
+ # "type" => {"name"=>"Round Robin", ...},
613
+ # "virtualip" => {"port"=>80,"ip"=> {...}},
614
+ # "persistence" => {"name"=>"None", ...},
615
+ # "object" => "loadbalancer",
616
+ # "state" => {"name"=>"On", ...}
617
+ # }]
618
+ #
619
+ # This method is cached.
620
+ #
621
+ def list_loadbalancers
622
+ request_hash = generate_request("grid/loadbalancer/list")
623
+ request_cache_or_info(:list_loadbalancers, request_hash, GogridJsonParser)
624
+ rescue
625
+ on_exception
626
+ end
627
+
628
+ # Returns list of loadbalancers
629
+ #
630
+ # gogrid.get_loadbalancers_by_id(1) #=>
631
+ # [{ "name" => "API LB",
632
+ # "id" => 1,
633
+ # "realiplist" => [{"port"=>443,"ip"=> {...},
634
+ # {"port"=>8080,"ip"=> {...}],
635
+ # "os" => {"name"=>"F5", ...},
636
+ # "type" => {"name"=>"Round Robin", ...},
637
+ # "virtualip" => {"port"=>80,"ip"=> {...}},
638
+ # "persistence" => {"name"=>"None", ...},
639
+ # "object" => "loadbalancer",
640
+ # "state" => {"name"=>"On", ...}
641
+ # }]
642
+ #
643
+ # If +list+ param is set, then retrieve information about loadbalancers with listed ids only
644
+ #
645
+ def get_loadbalancers_by_id(*list)
646
+ gogrid_get_loadbalancers(:ids => list)
647
+ end
648
+
649
+ # Returns list of loadbalancers
650
+ #
651
+ # gogrid.get_loadbalancers_by_names(['API LB']) #=>
652
+ # [{ "name" => "API LB",
653
+ # "id" => 1,
654
+ # "realiplist" => [{"port"=>443,"ip"=> {...},
655
+ # {"port"=>8080,"ip"=> {...}],
656
+ # "os" => {"name"=>"F5", ...},
657
+ # "type" => {"name"=>"Round Robin", ...},
658
+ # "virtualip" => {"port"=>80,"ip"=> {...}},
659
+ # "persistence" => {"name"=>"None", ...},
660
+ # "object" => "loadbalancer",
661
+ # "state" => {"name"=>"On", ...}
662
+ # }]
663
+ #
664
+ # If +list+ param is set, then retrieve information about loadbalancers with listed names only
665
+ #
666
+ def get_loadbalancers_by_names(*list)
667
+ gogrid_get_loadbalancers(:names => list)
668
+ end
669
+
670
+ # GoGrid API: http://wiki.gogrid.com/wiki/index.php/API:grid.loadbalancer.add
671
+ #
672
+ # Adds a single load balancer to your grid and returns its configuration or an exception.
673
+ #
674
+ # # Required params:
675
+ # +:name+ = (string) The name of this load balancer.
676
+ # +:virtual_ip+ = (hash) The IPv4 and port of the virtual IP for this load balancer. This must be a publicly available IP.
677
+ # :ip => ipv4 (string)
678
+ # :port => port (string|integer)
679
+ # e.g., {:ip => "200.100.50.1", :port => "80}
680
+ # +:real_ips+ (array) The list of IP/port tuples in the real IP list for this load balancer.
681
+ # Each tuple in the array will follow the same structure if the virtual_ip parameter:
682
+ # :ip => ipv4 (string)
683
+ # :port => port (string|integer)
684
+ # e.g., [{:ip => "200.100.50.1", :port => "80} , {:ip => "1.2.3.4", :port => "8080}]
685
+ # Optional params:
686
+ # +:description+ = (string) Descriptive text to describe this load balancer.
687
+ # +:type+ = (string) The load balancer type. This can be an int or string representing the load balancer option's id or name respectively.
688
+ # * Default is none
689
+ # +:persistence+ = (string) The persistence type to use. This can be an int or string representing the load balancer persistence types option's id or name respectively.
690
+ # * Default is round robin.
691
+ # * To list persistence values, call list_common_lookup with lookup set to loadbalancer.persistence
692
+ #
693
+ # gogrid.add_loadbalancer( "API LB", "216.121.60.25", "80",
694
+ # [{:ip => "216.121.60.18", :port => "8080"},
695
+ # {:ip => "216.121.60.19", :port => "443"}]) !=>
696
+ # [{ "name" => "API LB",
697
+ # "id" => 1,
698
+ # "realiplist" => [{"port"=>443,"ip"=> {...},
699
+ # {"port"=>8080,"ip"=> {...}],
700
+ # "os" => {"name"=>"F5", ...},
701
+ # "type" => {"name"=>"Round Robin", ...},
702
+ # "virtualip" => {"port"=>80,"ip"=> {...}},
703
+ # "persistence" => {"name"=>"None", ...},
704
+ # "object" =>"loadbalancer",
705
+ # "state" => {"name"=>"On", ...}
706
+ # }]
707
+ #
708
+ def add_loadbalancer(name, virtual_ip, virtual_port, real_ips, description=nil, type=nil, persistence="None")
709
+ # mandatory
710
+ opts = { :name => name,
711
+ "virtualip.ip" => virtual_ip,
712
+ "virtualip.port" => virtual_port}
713
+ # optional
714
+ opts[:description] = description if description
715
+ opts[:type] = type if type
716
+ opts[:persistence] = persistence if persistence
717
+
718
+ real_ip_tuples = real_ips # It's an array of 2 item hashes [{:ip=>"216.121.60.18",:port=>"8080"},{:ip=>"200.100.50.1",:port=>"80"} ]
719
+ extra_opts = []
720
+ if real_ip_tuples && real_ip_tuples.length > 0
721
+ index=-1
722
+ real_ip_tuples.each do |tuple|
723
+ extra_opts << {"realiplist.#{index += 1}.ip" => tuple[:ip]}
724
+ extra_opts << {"realiplist.#{index}.port" => tuple[:port]}
725
+ end
726
+ end
727
+ do_request("grid/loadbalancer/add", opts, extra_opts)
728
+ rescue
729
+ on_exception
730
+ end
731
+
732
+ # GoGrid API: http://wiki.gogrid.com/wiki/index.php/API:grid.loadbalancer.delete
733
+ #
734
+ # Deletes a single loadbalancer from your grid. Returns array with one hash describing the deleted loadbalancer or an exception:
735
+ #
736
+ # Required params:
737
+ # (none required...except that at least 1 of the optional ones should be set)
738
+ # Optional params:
739
+ # +:id+ = (string|int) The id of the loadbalancer to delete.
740
+ # +:name+ = (string) The name of the loadbalancer to delete.
741
+ # +:loadbalancer+ (string|int) The id or name of the loadbalancer to delete.
742
+ #
743
+ # gogrid.gogrid_delete_loadbalancer(:name => ["From API"]) #=> [
744
+ # [{ "name" => "API LB",
745
+ # "id" => 1,
746
+ # "realiplist" => [{"port"=>443,"ip"=> {...},
747
+ # {"port"=>8080,"ip"=> {...}],
748
+ # "os" => {"name"=>"F5", ...},
749
+ # "type" => {"name"=>"Round Robin", ...},
750
+ # "virtualip" => {"port"=>80,"ip"=> {...}},
751
+ # "persistence" => {"name"=>"None", ...},
752
+ # "object" => "loadbalancer",
753
+ # "state" => {"name"=>"On", ...}
754
+ # }]
755
+ #
756
+ def gogrid_delete_loadbalancer(params)
757
+ do_request("grid/loadbalancer/delete",params)
758
+ rescue
759
+ on_exception
760
+ end
761
+
762
+ # Deletes loadbalancer with given name from your grid. Returns array with one hash describing the deleted loadbalancer or an exception:
763
+ #
764
+ # gogrid.delete_loadbalancer(1) #=> [
765
+ # [{ "name" => "API LB",
766
+ # "id" => 1,
767
+ # "realiplist" => [{"port"=>443,"ip"=> {...},
768
+ # {"port"=>8080,"ip"=> {...}],
769
+ # "os" => {"name"=>"F5", ...},
770
+ # "type" => {"name"=>"Round Robin", ...},
771
+ # "virtualip" => {"port"=>80,"ip"=> {...}},
772
+ # "persistence" => {"name"=>"None", ...},
773
+ # "object" => "loadbalancer",
774
+ # "state" => {"name"=>"On", ...}
775
+ # }]
776
+ #
777
+ def delete_loadbalancer(id)
778
+ gogrid_delete_loadbalancer(:id => id)
779
+ end
780
+
781
+ # Deletes loadbalancer with given name from your grid. Returns array with one hash describing the deleted loadbalancer or an exception:
782
+ #
783
+ # gogrid.delete_loadbalancer_by_name('API LB') #=> [
784
+ # [{ "name" => "API LB",
785
+ # "id" => 1,
786
+ # "realiplist" => [{"port"=>443,"ip"=> {...},
787
+ # {"port"=>8080,"ip"=> {...}],
788
+ # "os" => {"name"=>"F5", ...},
789
+ # "type" => {"name"=>"Round Robin", ...},
790
+ # "virtualip" => {"port"=>80,"ip"=> {...}},
791
+ # "persistence" => {"name"=>"None", ...},
792
+ # "object" => "loadbalancer",
793
+ # "state" => {"name"=>"On", ...}
794
+ # }]
795
+ #
796
+ def delete_loadbalancer_by_name(name)
797
+ gogrid_delete_loadbalancer(:name => name)
798
+ end
799
+
800
+ #--------------
801
+ # Billing
802
+ #--------------
803
+
804
+ #
805
+ # GoGrid API: http://wiki.gogrid.com/wiki/index.php/API:myaccount.billing.get
806
+ #
807
+ # Returns single billing summary or an exception
808
+ #
809
+ # Required params:
810
+ # (none)
811
+ # Optional params:
812
+ # (none)
813
+ #
814
+ # gogrid.get_myaccount_billing #=>
815
+ # [{"transferOverage"=>0,
816
+ # "transferOverageCharge"=>0,
817
+ # "memoryAccrued"=>504,
818
+ # "memoryOverage"=>11,
819
+ # "transferAllotment"=>0,
820
+ # "memoryInUse"=>0.5,
821
+ # "startDate"=>nil,
822
+ # "memoryAllotment"=>0,
823
+ # "memoryOverageCharge"=>2.08999997377396,
824
+ # "endDate"=>nil,
825
+ # "object"=>"billingsummary"}]
826
+ #
827
+ def get_myaccount_billing
828
+ do_request("myaccount/billing/get",{})
829
+ rescue
830
+ on_exception
831
+ end
832
+
833
+ end
834
+ end