wakame-vdc-agents 10.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/LICENSE +202 -0
  2. data/NOTICE +1 -0
  3. data/Rakefile +142 -0
  4. data/bin/hva +972 -0
  5. data/bin/nsa +147 -0
  6. data/bin/sta +182 -0
  7. data/config/hva.conf.example +10 -0
  8. data/config/initializers/isono.rb +43 -0
  9. data/config/initializers/passenger.rb +6 -0
  10. data/config/initializers/sequel.rb +21 -0
  11. data/config/nsa.conf.example +9 -0
  12. data/config/path_resolver.rb +12 -0
  13. data/lib/dcmgr.rb +115 -0
  14. data/lib/dcmgr/endpoints/core_api.rb +1004 -0
  15. data/lib/dcmgr/endpoints/core_api_mock.rb +816 -0
  16. data/lib/dcmgr/endpoints/errors.rb +55 -0
  17. data/lib/dcmgr/endpoints/metadata.rb +129 -0
  18. data/lib/dcmgr/logger.rb +44 -0
  19. data/lib/dcmgr/models/account.rb +104 -0
  20. data/lib/dcmgr/models/account_resource.rb +16 -0
  21. data/lib/dcmgr/models/base.rb +69 -0
  22. data/lib/dcmgr/models/base_new.rb +371 -0
  23. data/lib/dcmgr/models/frontend_system.rb +38 -0
  24. data/lib/dcmgr/models/host_pool.rb +102 -0
  25. data/lib/dcmgr/models/image.rb +46 -0
  26. data/lib/dcmgr/models/instance.rb +255 -0
  27. data/lib/dcmgr/models/instance_netfilter_group.rb +16 -0
  28. data/lib/dcmgr/models/instance_nic.rb +68 -0
  29. data/lib/dcmgr/models/instance_spec.rb +21 -0
  30. data/lib/dcmgr/models/ip_lease.rb +42 -0
  31. data/lib/dcmgr/models/netfilter_group.rb +88 -0
  32. data/lib/dcmgr/models/netfilter_rule.rb +21 -0
  33. data/lib/dcmgr/models/network.rb +32 -0
  34. data/lib/dcmgr/models/physical_host.rb +67 -0
  35. data/lib/dcmgr/models/request_log.rb +25 -0
  36. data/lib/dcmgr/models/ssh_key_pair.rb +55 -0
  37. data/lib/dcmgr/models/storage_pool.rb +134 -0
  38. data/lib/dcmgr/models/tag.rb +126 -0
  39. data/lib/dcmgr/models/tag_mapping.rb +28 -0
  40. data/lib/dcmgr/models/volume.rb +130 -0
  41. data/lib/dcmgr/models/volume_snapshot.rb +47 -0
  42. data/lib/dcmgr/node_modules/hva_collector.rb +134 -0
  43. data/lib/dcmgr/node_modules/sta_collector.rb +72 -0
  44. data/lib/dcmgr/scheduler.rb +12 -0
  45. data/lib/dcmgr/scheduler/find_last.rb +16 -0
  46. data/lib/dcmgr/scheduler/find_random.rb +16 -0
  47. data/lib/dcmgr/stm/instance.rb +25 -0
  48. data/lib/dcmgr/stm/snapshot_context.rb +33 -0
  49. data/lib/dcmgr/stm/volume_context.rb +65 -0
  50. data/lib/dcmgr/web/base.rb +21 -0
  51. data/lib/sinatra/accept_media_types.rb +128 -0
  52. data/lib/sinatra/lazy_auth.rb +56 -0
  53. data/lib/sinatra/rabbit.rb +278 -0
  54. data/lib/sinatra/respond_to.rb +272 -0
  55. data/lib/sinatra/sequel_transaction.rb +27 -0
  56. data/lib/sinatra/static_assets.rb +83 -0
  57. data/lib/sinatra/url_for.rb +44 -0
  58. metadata +270 -0
@@ -0,0 +1,816 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'sinatra/base'
4
+ require 'sinatra/rabbit'
5
+
6
+ require 'json'
7
+ require 'extlib/hash'
8
+
9
+ require 'dcmgr/endpoints/errors'
10
+
11
+ module Dcmgr
12
+ module Endpoints
13
+ class Mock
14
+ def self.loadfile(path, ext='json')
15
+ root_path = File.expand_path('../../')
16
+ controller,action = path.split('/')
17
+ file = action + '.' + ext
18
+ dir_path = File.join(root_path,'fixtures','mock',controller)
19
+ readfile = File.join(dir_path,file)
20
+ data = ''
21
+ open(readfile) {|f| data = f.read }
22
+ data
23
+ end
24
+ end
25
+
26
+ class CoreAPI < Sinatra::Base
27
+ register Sinatra::Rabbit
28
+
29
+ disable :sessions
30
+ disable :show_exceptions
31
+
32
+ before do
33
+ @params = parsed_request_body if request.post?
34
+ request.env['dcmgr.frotend_system.id'] = 1
35
+ request.env['HTTP_X_VDC_REQUESTER_TOKEN']='u-xxxxxx'
36
+ request.env['HTTP_X_VDC_ACCOUNT_UUID']='a-00000000'
37
+ end
38
+
39
+ before do
40
+ @account = Models::Account[request.env['HTTP_X_VDC_ACCOUNT_UUID']]
41
+ @requester_token = request.env['HTTP_X_VDC_REQUESTER_TOKEN']
42
+ #@frontend = Models::FrontendSystem[request.env['dcmgr.frotend_system.id']]
43
+
44
+ #raise InvalidRequestCredentials if !(@account && @frontend)
45
+ raise DisabledAccount if @account.disable?
46
+ end
47
+
48
+ def find_by_uuid(model_class, uuid)
49
+ if model_class.is_a?(Symbol)
50
+ model_class = Models.const_get(model_class)
51
+ end
52
+ model_class[uuid] || raise(UnknownUUIDResource, uuid.to_s)
53
+ end
54
+
55
+ def find_account(account_uuid)
56
+ find_by_uuid(:Account, account_uuid)
57
+ end
58
+
59
+ def find_user(user_uuid)
60
+ find_by_uuid(:User, user_uuid)
61
+ end
62
+
63
+ # Returns deserialized hash from HTTP body. Serialization fromat
64
+ # is guessed from content type header. The query string params
65
+ # is returned if none of content type header is in HTTP headers.
66
+ # This method is called only when the request method is POST.
67
+ def parsed_request_body
68
+ # @mime_types should be defined by sinatra/respond_to.rb plugin.
69
+ if @mime_types.nil?
70
+ # use query string as requested params if Content-Type
71
+ # header was not sent.
72
+ # ActiveResource library tells the one level nested hash which has
73
+ # {'something key'=>real_params} so that dummy key is assinged here.
74
+ hash = {:dummy=>@params}
75
+ else
76
+ mime = @mime_types.first
77
+ case mime.to_s
78
+ when 'application/json', 'text/json'
79
+ require 'json'
80
+ hash = JSON.load(request.body)
81
+ hash = hash.to_mash
82
+ when 'application/yaml', 'text/yaml'
83
+ require 'yaml'
84
+ hash = YAML.load(request.body)
85
+ hash = hash.to_mash
86
+ else
87
+ raise "Unsupported body document type: #{mime.to_s}"
88
+ end
89
+ end
90
+ return hash.values.first
91
+ end
92
+
93
+ # I am not going to use error(ex, &blk) hook since it works only
94
+ # when matches the Exception class exactly. I expect to match
95
+ # whole subclasses of APIError so that override handle_exception!().
96
+ def handle_exception!(boom)
97
+ if boom.kind_of?(APIError)
98
+ @env['sinatra.error'] = boom
99
+ error(boom.status_code, boom.class.to_s)
100
+ else
101
+ super
102
+ end
103
+ end
104
+
105
+ def pagenate(data,start,limit)
106
+ return data unless data.kind_of?(Array)
107
+ if !start.nil? && !limit.nil?
108
+ start = start.to_i
109
+ limit = limit.to_i
110
+ from = start
111
+ to = (from + limit -1)
112
+ data = data[from..to]
113
+ end
114
+ data
115
+ end
116
+
117
+ collection :accounts do
118
+ operation :index do
119
+ control do
120
+ end
121
+ end
122
+
123
+ operation :show do
124
+ control do
125
+ a = find_account(params[:id])
126
+ respond_to { |f|
127
+ f.json { a.to_hash_document.to_json }
128
+ }
129
+ end
130
+ end
131
+
132
+ operation :create do
133
+ description 'Register a new account'
134
+ control do
135
+ a = Models::Account.create()
136
+ respond_to { |f|
137
+ f.json { a.to_hash_document.to_json }
138
+ }
139
+ end
140
+ end
141
+
142
+ operation :destroy do
143
+ description 'Unregister the account.'
144
+ # Associated resources all have to be destroied prior to
145
+ # removing the account.
146
+ #param :id, :string, :required
147
+ control do
148
+ a = find_account(params[:id])
149
+ a.destroy
150
+
151
+ respond_to { |f|
152
+ f.json { {} }
153
+ }
154
+ end
155
+ end
156
+
157
+ operation :enable, :method=>:get, :member=>true do
158
+ description 'Enable the account for all operations'
159
+ control do
160
+ a = find_account(params[:id])
161
+ a.enabled = Models::Account::ENABLED
162
+ a.save
163
+
164
+ respond_to { |f|
165
+ f.json { {} }
166
+ }
167
+ end
168
+ end
169
+
170
+ operation :disable, :method=>:get, :member=>true do
171
+ description 'Disable the account for all operations'
172
+ control do
173
+ a = find_account(params[:id])
174
+ a.enabled = Models::Account::DISABLED
175
+ a.save
176
+
177
+ respond_to { |f|
178
+ f.json { {} }
179
+ }
180
+ end
181
+ end
182
+
183
+ operation :add_tag, :method=>:get, :member=>true do
184
+ description 'Add a tag belongs to the account'
185
+ #param :tag_name, :string, :required
186
+ control do
187
+ a = find_account(params[:id])
188
+
189
+ tag_class = Models::Tags.find_tag_class(params[:tag_name])
190
+ raise "UnknownTagClass: #{params[:tag_name]}" if tag_class.nil?
191
+
192
+ a.add_tag(tag_class.new(:name=>params[:name]))
193
+ end
194
+ end
195
+
196
+ operation :remove_tag, :method=>:get, :member=>true do
197
+ description 'Unlink the associated tag of the account'
198
+ #param :tag_id, :string, :required
199
+ control do
200
+ a = find_account(params[:id])
201
+ t = a.tags_dataset.filter(:uuid=>params[:tag_id]).first
202
+ if t
203
+ a.remove_tag(t)
204
+ else
205
+ raise "Unknown or disassociated tag for #{a.cuuid}: #{params[:tag_id]}"
206
+ end
207
+ end
208
+ end
209
+ end
210
+
211
+ collection :tags do
212
+ operation :create do
213
+ description 'Register new tag to the account'
214
+ #param :tag_name, :string, :required
215
+ #param :type_id, :fixnum, :optional
216
+ #param :account_id, :string, :optional
217
+ control do
218
+ tag_class = Models::Tag.find_tag_class(params[:tag_name])
219
+
220
+ tag_class.create
221
+
222
+ end
223
+ end
224
+
225
+ operation :show do
226
+ #param :account_id, :string, :optional
227
+ control do
228
+ end
229
+ end
230
+
231
+ operation :destroy do
232
+ description 'Create a new user'
233
+ control do
234
+ end
235
+ end
236
+
237
+ operation :update do
238
+ control do
239
+ end
240
+ end
241
+ end
242
+
243
+ collection :instances do
244
+ operation :create do
245
+ description 'Runs a new instance'
246
+ # param :image_id, :required
247
+ # param :host_pool_id :required
248
+ # param :instance_spec_id, :required
249
+ control do
250
+ i = {
251
+ :memory_size => 256,
252
+ :image_id => "wmi-640cbf3r",
253
+ :created_at => "Wed Oct 27 16:58:24 +0900 2010",
254
+ :network => {"ipaddr"=>"192.168.1.241"},
255
+ :id => "i-umwcbev3",
256
+ :volume => {},
257
+ :host_pool_id => "hp-hb4f6f84",
258
+ :cpu_cores => 1,
259
+ :status => "init",
260
+ :state => "init"
261
+ }
262
+
263
+ respond_to { |f|
264
+ f.json { i.to_json }
265
+ }
266
+ end
267
+ end
268
+
269
+ operation :show do
270
+ #param :account_id, :string, :optional
271
+ control do
272
+ i = Modles::Instance[params[:id]]
273
+ respond_to { |f|
274
+ f.json { i.to_hash_document.to_json }
275
+ }
276
+ end
277
+ end
278
+
279
+ operation :destroy do
280
+ description 'Shutdown the instance'
281
+ control do
282
+ i = find_by_uuid(:Instance, params[:id])
283
+
284
+ respond_to { |f|
285
+ f.json { i.to_hash_document.to_json }
286
+ }
287
+ end
288
+ end
289
+
290
+ operation :update do
291
+ description 'Change vcpu cores or memory size on the instance'
292
+ control do
293
+ i = find_by_uuid(:Instance, params[:id])
294
+ end
295
+ end
296
+
297
+ operation :reboot, :method=>:get, :member=>true do
298
+ description 'Reboots the instance'
299
+ control do
300
+ i = find_by_uuid(:Instance, params[:id])
301
+ end
302
+ end
303
+
304
+ operation :resume, :method=>:get, :member=>true do
305
+ description 'Resume the suspending instance'
306
+ control do
307
+ i = find_by_uuid(:Instance, params[:id])
308
+ end
309
+ end
310
+
311
+ operation :suspend, :method=>:get, :member=>true do
312
+ description 'Suspend the instance'
313
+ control do
314
+ i = find_by_uuid(:Instance, params[:id])
315
+ end
316
+ end
317
+ end
318
+
319
+ collection :host_pools do
320
+ operation :create do
321
+ description 'Register a new physical host'
322
+ # param :
323
+ control do
324
+ raise OperationNotPermitted unless @account.is_a?(Models::Account::SystemAccount::DatacenterAccount)
325
+ input = parsed_request_body
326
+
327
+ hp = Models::HostPool.create(:cpu_cores=>input[:cpu_cores],
328
+ :memory_size=>input[:memory_size],
329
+ :arch=>input[:arch],
330
+ :hypervisor=>input[:hypervisor]
331
+ )
332
+ respond_to { |f|
333
+ f.json{ hp.to_hash_document.to_json }
334
+ }
335
+ end
336
+ end
337
+
338
+ operation :show do
339
+ description 'Show status of the host'
340
+ #param :account_id, :string, :optional
341
+ control do
342
+ raise OperationNotPermitted unless @account.is_a?(Models::Account::SystemAccount::DatacenterAccount)
343
+
344
+ hp = find_by_uuid(:HostPool, params[:id])
345
+ respond_to { |f|
346
+ f.json { hp.to_hash_document.to_json }
347
+ }
348
+ end
349
+ end
350
+
351
+ operation :destroy do
352
+ description 'Unregister the existing host'
353
+ control do
354
+ raise OperationNotPermitted unless @account.is_a?(Models::Account::SystemAccount::DatacenterAccount)
355
+
356
+ hp = find_by_uuid(:HostPool, params[:id])
357
+ if hp.depend_resources?
358
+ raise ""
359
+ end
360
+ hp.destroy
361
+ end
362
+ end
363
+
364
+ operation :update do
365
+ description 'Update parameters for the host'
366
+ # param :cpu_cores, :optional
367
+ # param :memory_size, :optional
368
+ control do
369
+ raise OperationNotPermitted unless @account.is_a?(Models::Account::SystemAccount::DatacenterAccount)
370
+
371
+ hp = find_by_uuid(:HostPool, params[:id])
372
+ if params[:cpu_cores]
373
+ hp.offering_cpu_cores = params[:cpu_cores].to_i
374
+ end
375
+ if params[:memory_size]
376
+ hp.offering_memory_size = params[:memory_size].to_i
377
+ end
378
+
379
+ hp.save
380
+ end
381
+ end
382
+
383
+ end
384
+
385
+ collection :images do
386
+ operation :index do
387
+ description 'Show list of machine images'
388
+ control do
389
+ start = params[:start].to_i
390
+ start = start < 1 ? 0 : start
391
+ limit = params[:limit].to_i
392
+ limit = limit < 1 ? 10 : limit
393
+
394
+ partial_ds = (1..30).collect { |i| {
395
+ :created_at => "Mon Oct 18 18:33:58 +0900 2010",
396
+ :updated_at => "Mon Oct 18 18:33:58 +0900 2010",
397
+ :uuid => "wmi-640cbf"+sprintf('%02d',i),
398
+ :arch => "x86",
399
+ :account_id => "a-00000000",
400
+ :id => "wmi-640cbf"+sprintf('%02d',i),
401
+ :boot_dev_type => 2,
402
+ :description => "",
403
+ :source => {
404
+ :uri => "http://localhost/vdc/tmpmz0N86.qcow2",
405
+ :type => "http"
406
+ },
407
+ :state => "init"
408
+ }}
409
+
410
+ total = partial_ds.count
411
+ partial_ds = pagenate(partial_ds,params[:start],params[:limit])
412
+
413
+ res = [{
414
+ :owner_total => total,
415
+ :start => start,
416
+ :limit => limit,
417
+ :results=> partial_ds
418
+ }]
419
+
420
+ respond_to { |f|
421
+ f.json {res.to_json}
422
+ }
423
+ end
424
+ end
425
+ end
426
+
427
+ collection :volumes do
428
+ operation :index do
429
+ description 'Show lists of the volume'
430
+ # param start, fixnum, optional
431
+ # param limit, fixnum, optional
432
+ control do
433
+ start = params[:start].to_i
434
+ start = start < 1 ? 0 : start
435
+ limit = params[:limit].to_i
436
+ limit = limit < 1 ? 10 : limit
437
+
438
+ json = Mock.loadfile('volumes/list')
439
+ vl = JSON.load(json)
440
+ total = vl.count
441
+ vl = pagenate(vl,start,limit)
442
+
443
+ res = [{
444
+ :owner_total => total,
445
+ :start => start,
446
+ :limit => limit,
447
+ :results => vl
448
+ }]
449
+ respond_to { |f|
450
+ f.json {res.to_json}
451
+ }
452
+ end
453
+ end
454
+
455
+ operation :show do
456
+ description 'Show the volume status'
457
+ # param id, string, required
458
+ control do
459
+ volume_id = params[:id]
460
+ raise UndefinedVolumeID if volume_id.nil?
461
+ json = Mock.loadfile('volumes/details')
462
+ vl = JSON.load(json)
463
+ vl = vl[volume_id]
464
+ respond_to { |f|
465
+ f.json { vl.to_json}
466
+ }
467
+ end
468
+ end
469
+
470
+ operation :create do
471
+ description 'Create the new volume'
472
+ # param volume_size, string, required
473
+ # param snapshot_id, string, optional
474
+ # param private_pool_id, string, optional
475
+ control do
476
+ vl = { :status => 'creating', :messages => 'creating the new volume vol-xxxxxxx'}
477
+ # vl = Models::Volume.create(:size=> params[:volume_size])
478
+ # vl.state_machine.on_create
479
+ respond_to { |f|
480
+ f.json { vl.to_json}
481
+ }
482
+ end
483
+ end
484
+
485
+ operation :destroy do
486
+ description 'Delete the volume'
487
+ # param volume_id, string, required
488
+ control do
489
+ vl = { :status => 'deleting', :messages => 'deleting the volume vol-xxxxxxx'}
490
+ respond_to { |f|
491
+ f.json { vl.to_json}
492
+ }
493
+ end
494
+ end
495
+
496
+ operation :attach, :method =>:put, :member =>true do
497
+ description 'Attachd the volume'
498
+ # param volume_id, string, required
499
+ # param instance_id, string, required
500
+ control do
501
+ vl = { :status => 'attaching', :message => 'attaching the volume of vol-xxxxxx to instance_id'}
502
+ respond_to { |f|
503
+ f.json { vl.to_json}
504
+ }
505
+ end
506
+ end
507
+
508
+ operation :detach, :method =>:put, :member =>true do
509
+ description 'Detachd the volume'
510
+ # param volume_id, string, required
511
+ control do
512
+ vl = { :status => 'detaching', :message => 'detaching the volume of instance_id to vol-xxxxxx'}
513
+ respond_to { |f|
514
+ f.json { vl.to_json}
515
+ }
516
+ end
517
+ end
518
+
519
+ operation :status, :method =>:get, :member =>true do
520
+ description 'Show the status'
521
+ control do
522
+ vl = [{ :id => 1, :uuid => 'vol-xxxxxxx', :status => 1 },
523
+ { :id => 2, :uuid => 'vol-xxxxxxx', :status => 0 },
524
+ { :id => 3, :uuid => 'vol-xxxxxxx', :status => 3 },
525
+ { :id => 4, :uuid => 'vol-xxxxxxx', :status => 2 },
526
+ { :id => 5, :uuid => 'vol-xxxxxxx', :status => 4 }]
527
+ respond_to {|f|
528
+ f.json { vl.to_json}
529
+ }
530
+ end
531
+ end
532
+ end
533
+
534
+ collection :volume_snapshots do
535
+ operation :index do
536
+ description 'Show lists of the volume_snapshots'
537
+ # param start, Fixnum, optional
538
+ # param limit, Fixnum, optional
539
+ control do
540
+ start = params[:start].to_i
541
+ start = start < 1 ? 0 : start
542
+ limit = params[:limit].to_i
543
+ limit = limit < 1 ? 10 : limit
544
+
545
+ json = Mock.loadfile('volume_snapshots/list')
546
+ vs = JSON.load(json)
547
+ total = vs.count
548
+ vs = pagenate(vs,start,limit)
549
+
550
+ res = [{
551
+ :owner_total => total,
552
+ :start => start,
553
+ :limit => limit,
554
+ :results => vs
555
+ }]
556
+ respond_to { |f|
557
+ f.json {res.to_json}
558
+ }
559
+ end
560
+ end
561
+
562
+ operation :show do
563
+ description 'Show the volume status'
564
+ # param id, string, required
565
+ control do
566
+ snapshot_id = params[:id]
567
+ raise UndefinedVolumeSnapshotID if snapshot_id.nil?
568
+ json = Mock.loadfile('volume_snapshots/details')
569
+ vs = JSON.load(json)
570
+ vs = vs[snapshot_id]
571
+ respond_to { |f|
572
+ f.json { vs.to_json}
573
+ }
574
+ end
575
+ end
576
+
577
+ operation :create do
578
+ description 'Create a new volume snapshot'
579
+ # param volume_id, string, required
580
+ # param pool_id, string, optional
581
+ control do
582
+ vs = { :status => 'creating', :message => 'creating the new snapshot'}
583
+ respond_to { |f|
584
+ f.json { vs.to_json }
585
+ }
586
+ end
587
+ end
588
+
589
+ operation :destroy do
590
+ description 'Delete the volume snapshot'
591
+ # param snapshot_id, string, required
592
+ control do
593
+ vs = { :status => 'deleting', :message => 'deleting the snapshot'}
594
+ respond_to { |f|
595
+ f.json { vs.to_json }
596
+ }
597
+ end
598
+ end
599
+
600
+ operation :status, :method =>:get, :member =>true do
601
+ description 'Show the status'
602
+ control do
603
+ vs = [{ :id => 1, :uuid => 'snap-xxxxxxx', :status => 1 },
604
+ { :id => 2, :uuid => 'snap-xxxxxxx', :status => 0 },
605
+ { :id => 3, :uuid => 'snap-xxxxxxx', :status => 3 },
606
+ { :id => 4, :uuid => 'snap-xxxxxxx', :status => 2 },
607
+ { :id => 5, :uuid => 'snap-xxxxxxx', :status => 4 }]
608
+ respond_to {|f|
609
+ f.json { vs.to_json}
610
+ }
611
+ end
612
+ end
613
+ end
614
+
615
+ collection :netfilter_groups do
616
+ operation :index do
617
+ control do
618
+ start = params[:start].to_i
619
+ start = start < 1 ? 0 : start
620
+ limit = params[:limit].to_i
621
+ limit = limit < 1 ? 10 : limit
622
+
623
+ g = (1..30).collect { |i|
624
+ {
625
+ :id => i,
626
+ :name => "group_#{i}",
627
+ :description => "desc_group_#{i}",
628
+ :rule => "\ntcp:22,22,ip4:0.0.0.0\ntcp:80,80,ip4:0.0.0.0\n#tcp:443,443,ip4:0.0.0.0\nudp:53,53,ip4:0.0.0.0\nicmp:-1,-1,ip4:0.0.0.0\n",
629
+ :account_id => "a-00000000",
630
+ :created_at => "Fri Oct 22 10:50:09 +0900 2010",
631
+ :updated_at => "Fri Oct 22 10:50:09 +0900 2010",
632
+ }
633
+ }
634
+ total = g.count
635
+ g = pagenate(g,params[:start],params[:limit])
636
+
637
+ res = [{
638
+ :owner_total => total,
639
+ :start => start,
640
+ :limit => limit,
641
+ :results => g
642
+ }]
643
+
644
+ respond_to { |f|
645
+ f.json {res.to_json}
646
+ }
647
+ end
648
+ end
649
+
650
+ operation :show do
651
+ description 'Show lists of the netfilter_groups'
652
+ control do
653
+ @name = params[:id]
654
+ g = {
655
+ :id => 1,
656
+ :name => @name,
657
+ :description => "desc_#{@name}",
658
+ :rule => "\ntcp:22,22,ip4:0.0.0.0\ntcp:80,80,ip4:0.0.0.0\n#tcp:443,443,ip4:0.0.0.0\nudp:53,53,ip4:0.0.0.0\nicmp:-1,-1,ip4:0.0.0.0\n",
659
+ :account_id => "a-00000000",
660
+ :created_at => "Fri Oct 22 10:50:09 +0900 2010",
661
+ :updated_at => "Fri Oct 22 10:50:09 +0900 2010",
662
+ }
663
+ respond_to { |f|
664
+ f.json { g.to_json }
665
+ }
666
+ end
667
+ end
668
+
669
+ operation :create do
670
+ description 'Register a new netfilter_group'
671
+ # params name, string
672
+ # params description, string
673
+ # params rule, string
674
+ control do
675
+ raise UndefinedNetfilterGroup if params[:name].nil?
676
+
677
+ @name = params[:name]
678
+ @description = if params[:description]
679
+ params[:description]
680
+ else
681
+ "desc_#{@name}"
682
+ end
683
+ @rule = if params[:rule]
684
+ params[:rule]
685
+ else
686
+ "\ntcp:22,22,ip4:0.0.0.0\ntcp:80,80,ip4:0.0.0.0\n#tcp:443,443,ip4:0.0.0.0\nudp:53,53,ip4:0.0.0.0\nicmp:-1,-1,ip4:0.0.0.0\n"
687
+ end
688
+
689
+ g = {
690
+ :id => 1,
691
+ :name => @name,
692
+ :description => @description,
693
+ :rule => @rule,
694
+ :account_id => "a-00000000",
695
+ :created_at => "Fri Oct 22 10:50:09 +0900 2010",
696
+ :updated_at => "Fri Oct 22 10:50:09 +0900 2010",
697
+ }
698
+ respond_to { |f|
699
+ f.json { g.to_json }
700
+ }
701
+ end
702
+ end
703
+
704
+ operation :update do
705
+ description "Update parameters for the netfilter group"
706
+ # params description, string
707
+ # params rule, string
708
+ control do
709
+ @name = params[:id]
710
+ @description = nil
711
+ @rule = nil
712
+
713
+ if params[:description]
714
+ @description = params[:description]
715
+ else
716
+ @description = "desc_#{@name}"
717
+ end
718
+
719
+ if params[:rule]
720
+ @rule = params[:rule]
721
+ else
722
+ @rule = "\ntcp:22,22,ip4:0.0.0.0\ntcp:80,80,ip4:0.0.0.0\n#tcp:443,443,ip4:0.0.0.0\nudp:53,53,ip4:0.0.0.0\nicmp:-1,-1,ip4:0.0.0.0\n"
723
+ end
724
+
725
+ g = {
726
+ :id => 1,
727
+ :name => @name,
728
+ :description => @description,
729
+ :rule => @rule,
730
+ :account_id => "a-00000000",
731
+ :created_at => "Fri Oct 22 10:50:09 +0900 2010",
732
+ :updated_at => "Fri Oct 22 10:50:09 +0900 2010",
733
+ }
734
+ respond_to { |f|
735
+ f.json { g.to_json }
736
+ }
737
+ end
738
+ end
739
+
740
+ operation :destroy do
741
+ description "Delete the netfilter group"
742
+
743
+ control do
744
+ @name = params[:id]
745
+ g = {
746
+ :id => 1,
747
+ :name => @name,
748
+ :description => "desc_#{@name}",
749
+ :rule => "\ntcp:22,22,ip4:0.0.0.0\ntcp:80,80,ip4:0.0.0.0\n#tcp:443,443,ip4:0.0.0.0\nudp:53,53,ip4:0.0.0.0\nicmp:-1,-1,ip4:0.0.0.0\n",
750
+ :account_id => "a-00000000",
751
+ :created_at => "Fri Oct 22 10:50:09 +0900 2010",
752
+ :updated_at => "Fri Oct 22 10:50:09 +0900 2010",
753
+ }
754
+ respond_to { |f|
755
+ f.json { g.to_json }
756
+ }
757
+ end
758
+ end
759
+
760
+ end
761
+
762
+ collection :netfilter_rules do
763
+ operation :index do
764
+ control do
765
+ end
766
+ end
767
+
768
+ operation :show do
769
+ description 'Show lists of the netfilter_rules'
770
+ control do
771
+ g = [
772
+ {:id => 1, :netfilter_group_id => 1, :permission => "tcp:22,22,ip4:0.0.0.0", :created_at => "Fri Oct 22 11:15:10 +0900 2010", :updated_at => "Fri Oct 22 11:15:10 +0900 2010",},
773
+ {:id => 2, :netfilter_group_id => 1, :permission => "tcp:80,80,ip4:0.0.0.0", :created_at => "Fri Oct 22 11:15:10 +0900 2010", :updated_at => "Fri Oct 22 11:15:10 +0900 2010",},
774
+ {:id => 3, :netfilter_group_id => 1, :permission => "udp:53,53,ip4:0.0.0.0", :created_at => "Fri Oct 22 11:15:10 +0900 2010", :updated_at => "Fri Oct 22 11:15:10 +0900 2010",},
775
+ {:id => 4, :netfilter_group_id => 1, :permission => "icmp:-1,-1,ip4:0.0.0.0", :created_at => "Fri Oct 22 11:15:10 +0900 2010", :updated_at => "Fri Oct 22 11:15:10 +0900 2010",},
776
+ ]
777
+ respond_to { |f|
778
+ f.json { g.to_json }
779
+ }
780
+ end
781
+ end
782
+ end
783
+
784
+ collection :private_pools do
785
+ operation :show do
786
+ description 'Show lists of the private_pools'
787
+ control do
788
+ pp = [{
789
+ :id => 1,
790
+ :account_id => 'u-xxxxxxx',
791
+ :storage_pool_id => 1,
792
+ :created_at => 'Fri Sep 10 14:50:11 +0900 2010',
793
+ :updated_at => 'Fri Sep 10 14:50:11 +0900 2010'
794
+ },{
795
+ :id => 2,
796
+ :account_id => 'u-xxxxxxx',
797
+ :storage_pool_id => 23,
798
+ :created_at => 'Fri Sep 10 14:50:11 +0900 2010',
799
+ :updated_at => 'Fri Sep 10 14:50:11 +0900 2010'
800
+ },{
801
+ :id => 2,
802
+ :account_id => 'u-xxxxxxx',
803
+ :storage_pool_id => 150,
804
+ :created_at => 'Fri Sep 10 14:50:11 +0900 2010',
805
+ :updated_at => 'Fri Sep 10 14:50:11 +0900 2010'
806
+ }]
807
+ respond_to { |f|
808
+ f.json { pp.to_json}
809
+ }
810
+ end
811
+ end
812
+ end
813
+
814
+ end
815
+ end
816
+ end