webee 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -98,3 +98,7 @@ gem install webee
98
98
  # FIXME: Not supported in Abiquo right now
99
99
  #
100
100
  #rack.delete
101
+
102
+
103
+ More examples can be found in the examples folder
104
+
data/examples/basics.rb CHANGED
@@ -4,46 +4,72 @@ require 'md5'
4
4
 
5
5
  WeBee::Api.user = ENV['user'] || 'admin'
6
6
  WeBee::Api.password = ENV['pass'] || 'xabiquo'
7
- WeBee::Api.url = 'http://abiquo/api'
7
+ WeBee::Api.url = "http://#{ENV['host'] || 'abiquo'}/api"
8
+
9
+ #
10
+ # Add a license
11
+ #
12
+ license_file = ARGV[0]
13
+ if license_file.nil? or (not File.exist?(license_file))
14
+ $stderr.puts "Invalid license file"
15
+ exit 1
16
+ end
17
+ WeBee::Config.add_license File.read(license_file).strip.chomp
18
+
19
+ #
20
+ # Create a new datacenter
21
+ #
22
+ # This assumes the Remote Services are located in the same server
23
+ #
24
+ dc = WeBee::Datacenter.create :name => 'BCN'
25
+
26
+ #
27
+ # Advanced Datacenter Creation:
28
+ #
29
+ # If you want to add a Datacenter with remote services located in another host
30
+ # use this.
31
+ #
32
+ # remote_services= WeBee::RemoteService.create_for_host("10.60.1.24")
33
+ # WeBee::Datacenter.create :name => 'foo'
34
+ # :remote_services => remote_services
35
+
8
36
 
9
37
  #
10
38
  # List all datacenters available
11
39
  #
12
40
  WeBee::Datacenter.all.each do |dc|
13
- puts dc.name
41
+ # do something like printing dc.name
14
42
  end
15
43
 
16
-
17
44
  #
18
45
  # Return the first datacenter whose name matches BCN (matched via Regexp)
19
46
  #
20
47
  dc = WeBee::Datacenter.find_by_name('BCN').first
21
- puts dc.name
48
+ # puts dc.name
22
49
 
23
50
  #
24
- # List Datacenter Racks
51
+ # Create an enterprise
25
52
  #
26
- dc.racks.each do |r|
27
- puts r.name
28
- end
53
+ ent = WeBee::Enterprise.create :name => 'QA'
29
54
 
30
55
  #
31
56
  # List all the enterprises
32
57
  #
33
58
  WeBee::Enterprise.all.each do |e|
34
- puts e.name
59
+ # do something like printing e.name
35
60
  end
36
61
 
37
62
  #
38
63
  # Find an enterprise named QA
39
64
  #
40
- qa_ent = WeBee::Enterprise.find_by_name('QA')
65
+ qa_ent = WeBee::Enterprise.find_by_name('QA').first
41
66
 
42
67
  #
43
68
  # List enterprise 'QA' users
44
69
  #
45
70
  qa_ent.users.each do |u|
46
- puts u.name
71
+ # print user name
72
+ # puts u.name
47
73
  end
48
74
 
49
75
 
@@ -60,35 +86,84 @@ user = qa_ent.create_user :name => 'Sergio Rubio',
60
86
  :active => 'true' #optional
61
87
 
62
88
  # delete the user
63
- user.delete
89
+ # user.delete
64
90
 
65
91
  #
66
- # Create a new datacenter
67
- # FIXME: not implemented in WeBee
92
+ # List WeBee::OVFPackage available in enterprise QA
68
93
  #
69
- # WeBee::Datacenter.create :name => 'Test DC'
94
+ qa_ent.ovf_packages.each do |pkg|
95
+ # print package info
96
+ # puts "#{pkg.product_name} (#{pkg.category_name})"
97
+ end
70
98
 
71
99
  #
72
- # List WeBee::OVFPackage available in enterprise QA
100
+ # List Datacenter Racks
73
101
  #
74
- qa_ent.ovf_packages.each do |pkg|
75
- puts "#{pkg.product_name} (#{pkg.category_name})"
102
+ dc.racks.each do |r|
103
+ # print rack name
104
+ # puts r.name
76
105
  end
77
106
 
78
107
  #
79
108
  # Create a new Rack
80
109
  #
81
- dc = WeBee::Datacenter.find_by_name('BCN').first
82
- rack = WeBee::Rack.create dc.datacenter_id,
83
- :name => 'rack_1',
84
- :ha_enabled => false,
85
- :vlan_min_id => 100,
86
- :vlan_max_id => 2048,
87
- :vlan_per_vdc_expected => 6,
88
- :nrsq => 80
110
+ rack = dc.add_rack :name => 'rack_1',
111
+ :ha_enabled => false,
112
+ :vlan_min_id => 100,
113
+ :vlan_max_id => 2048,
114
+ :vlan_per_vdc_expected => 6,
115
+ :nrsq => 80
89
116
 
90
117
  #
118
+ # Hypervisor (Machine) stuff
119
+ #
120
+ machine = dc.discover_machine :ip => '10.60.1.24', :hypervisortype => 'kvm'
121
+ # Use the first virtual switch we find
122
+ machine.virtual_switch = machine.virtual_switches.first
123
+ # Enable the first datastore found
124
+ machine.datastores.first.enabled = true
125
+ # Add the hypervisor to the rack
126
+ rack.add_machine(machine)
91
127
  # Delete the rack
92
128
  # FIXME: Not supported in Abiquo right now
93
- #
94
129
  #rack.delete
130
+
131
+ #
132
+ # Create a Virtual Datacenter with default limits and network
133
+ #
134
+ qa = WeBee::Enterprise.find_by_name('QA').first
135
+ # We need to set limits for the enterprise to be able to use a datacenter
136
+ qa.set_limits_for_datacenter dc
137
+ #vdc = qa.create_vdc :name => 'kvm-vdc', :hypervisortype => 'KVM', :datacenter => dc
138
+
139
+ #
140
+ # Advanced VDC Creation
141
+ #
142
+ vdc = qa.create_vdc :datacenter => dc,
143
+ :name => 'kvm-vdc',
144
+ :hypervisortype => 'KVM',
145
+ :ramSoft => 19456,
146
+ :ramHard => 20480,
147
+ :cpuSoft => 18,
148
+ :cpuHard => 20,
149
+ :storageSoft => 28991029248,
150
+ :storageHard => 32212254720,
151
+ :publicIpsSoft => 2,
152
+ :publicIpsHard => 4,
153
+ :hdSoft => 27917287424,
154
+ :hdHard => 32212254720,
155
+ :vlanSoft => 5,
156
+ :vlanHard => 10
157
+
158
+ # Find VDC by name
159
+ vdc = WeBee::VDC.find_by_name 'kvm-vdc'
160
+
161
+ #
162
+ # List all VDCs for my Enterprise
163
+ WeBee::VDC.all
164
+
165
+ #
166
+ # List all VDCs from a specific Enterprise/Datacenter
167
+ # Needs Cloud operator privileges
168
+ #
169
+ WeBee::VDC.all :datacenter_id => WeBee::Datacenter.all.first.datacenter_id, :enterprise_id => 3
data/examples/test.rb ADDED
@@ -0,0 +1,20 @@
1
+ $: << '../lib'
2
+ require 'webee'
3
+ require 'md5'
4
+ require 'active_support'
5
+
6
+ WeBee::Api.user = ENV['user'] || 'admin'
7
+ WeBee::Api.password = ENV['pass'] || 'xabiquo'
8
+ WeBee::Api.url = 'http://10.60.1.24/api'
9
+
10
+ #rs = WeBee::RemoteService.create_for_host("10.60.1.24")
11
+
12
+ WeBee::Datacenter.create :name => 'BCN'
13
+ # :remote_services => rs
14
+ #
15
+ WeBee::Enterprise.create :name => 'QA'
16
+
17
+ WeBee::
18
+
19
+
20
+
data/examples/test2.rb ADDED
@@ -0,0 +1,12 @@
1
+ $: << '../lib'
2
+ require 'webee'
3
+ require 'md5'
4
+ require 'active_support'
5
+ include WeBee
6
+
7
+ WeBee::Api.user = ENV['user'] || 'admin'
8
+ WeBee::Api.password = ENV['pass'] || 'xabiquo'
9
+ WeBee::Api.url = 'http://10.60.1.24/api'
10
+
11
+ require 'pp'
12
+ pp VDC.all :datacenter_id => 2, :enterprise_id => 3
data/lib/webee.rb CHANGED
@@ -4,10 +4,32 @@ require 'sax-machine'
4
4
  require 'nokogiri'
5
5
  require 'active_support/core_ext/hash'
6
6
  require 'uri'
7
+ require 'json'
8
+ require 'builder'
9
+
10
+ #
11
+ # Monkeypatch SAXMachine to keep the raw XMK
12
+ #
13
+ # Ugly but fun, isn't it? This may blow up at some point
14
+ #
15
+ module SAXMachine
16
+
17
+ alias_method :old_parse, :parse
18
+
19
+ def parse(xml, on_error = nil, on_warning = nil)
20
+ obj = old_parse(xml, on_error, on_warning)
21
+ obj.instance_variable_set :@raw, xml
22
+ obj.class.send :define_method, 'raw' do
23
+ @raw
24
+ end
25
+ obj
26
+ end
27
+ end
7
28
 
8
29
  module WeBee
9
30
 
10
- VERSION = '0.1'
31
+ VERSION = '0.2'
32
+
11
33
 
12
34
  module RestResource
13
35
 
@@ -46,10 +68,16 @@ module WeBee
46
68
 
47
69
  end
48
70
 
71
+ class Config
72
+ def self.add_license(hash)
73
+ RestClient.post Api.url + "/config/licenses" , "<license><code>#{hash}</code></license>", :content_type => :xml
74
+ end
75
+ end
76
+
49
77
 
50
78
  class Api
51
79
  class << self
52
- attr_accessor :user, :password, :url, :port
80
+ attr_accessor :user, :password, :url, :port, :host
53
81
 
54
82
  def url=(url)
55
83
  @url = build_url(url)
@@ -59,6 +87,7 @@ module WeBee
59
87
  def build_url(url)
60
88
  port ||= 80
61
89
  uri = URI.parse(url)
90
+ @host = uri.host
62
91
  "http://#{user}:#{password}@#{uri.host}:#{uri.port}#{uri.path}"
63
92
  end
64
93
  end
@@ -120,25 +149,54 @@ module WeBee
120
149
 
121
150
  element :id, :as => :datacenter_id
122
151
  element :name
123
- element :ramSoft
124
- element :ramHard
125
- element :cpuSoft
126
- element :cpuHard
127
- element :storageSoft
128
- element :storageHard
129
- element :repositorySoft
130
- element :repositoryHard
131
- element :publicIPSoft
132
- element :publicIPHard
133
- element :hdSoft
134
- element :hdHard
135
- element :vlanSoft
136
- element :vlanHard
152
+ element :ramSoft, :as => :ram_soft
153
+ element :ramHard, :as => :ram_hard
154
+ element :cpuSoft, :as => :cpu_soft
155
+ element :cpuHard, :as => :cpu_hard
156
+ element :storageSoft, :as => :storage_soft
157
+ element :storageHard, :as => :storage_hard
158
+ element :repositorySoft, :as => :repository_soft
159
+ element :repositoryHard, :as => :repository_hard
160
+ element :publicIpsSoft, :as => :public_ip_soft
161
+ element :publicIpsHard, :as => :public_ip_hard
162
+ element :hdSoft, :as => :hd_soft
163
+ element :hdHard, :as => :hd_hard
164
+ element :vlanSoft, :as => :vlan_soft
165
+ element :vlanHard, :as => :vlan_hard
137
166
  element :location
138
167
 
139
168
  def self.create(attributes)
140
- xml = attributes.to_xml(:root => 'datacenter')
141
- res = RestClient.post(Api.url + '/admin/datacenters', xml, :content_type => :xml, :accept => :xml)
169
+ if attributes[:remote_services].nil?
170
+ attributes[:remote_services] = WeBee::RemoteService.create_for_host(Api.host)
171
+ end
172
+ xm = Builder::XmlMarkup.new
173
+ xm.datacenter {
174
+ xm.name attributes[:name]
175
+ xm.location(attributes[:location] || 'California, USA')
176
+ xm.cpuSoft(attributes[:cpu_soft] || "0")
177
+ xm.cpuHard(attributes[:cpu_hard] || "0")
178
+ xm.vlanSoft(attributes[:vlan_soft] || "0")
179
+ xm.vlanHard(attributes[:vlan_hard] || "0")
180
+ xm.ramSoft(attributes[:ram_soft] || "0")
181
+ xm.ramHard(attributes[:ram_hard] || "0")
182
+ xm.repositorySoft(attributes[:repository_soft] || "0")
183
+ xm.repositoryHard(attributes[:repository_hard] || "0")
184
+ xm.publicIpsSoft(attributes[:public_ip_soft] || "0" )
185
+ xm.publicIpsHard(attributes[:public_ip_hard] || "0" )
186
+ xm.hdSoft(attributes[:hd_soft] || "0")
187
+ xm.hdHard(attributes[:hd_hard] || "0")
188
+ xm.storageSoft(attributes[:storage_soft] || "0")
189
+ xm.storageHard(attributes[:storage_hard] || "0")
190
+ xm.remoteServices {
191
+ attributes[:remote_services].each do |rs|
192
+ xm.remoteService {
193
+ xm.uri rs.uri
194
+ xm.type rs.rs_type
195
+ }
196
+ end
197
+ }
198
+ }
199
+ res = RestClient.post(Api.url + '/admin/datacenters', xm.target!, :content_type => :xml)
142
200
  Datacenter.parse(res)
143
201
  end
144
202
 
@@ -154,11 +212,83 @@ module WeBee
154
212
  doc = Nokogiri.parse(RestClient.get(Api.url + "/admin/datacenters/#{@datacenter_id}/racks", :accept => :xml))
155
213
  doc.search
156
214
  doc.search('//rack').each do |node|
157
- items << Rack.parse(node.to_s)
215
+ rack = Rack.parse(node.to_s)
216
+ rack.datacenter_id = @datacenter_id
217
+ items << rack
158
218
  end
159
219
  items
160
220
  end
161
221
 
222
+ def add_rack(params)
223
+ Rack.create datacenter_id, params
224
+ end
225
+
226
+ def discover_machine(params)
227
+ p = {}
228
+ p[:ip] = params[:ip]
229
+ p[:hypervisortype] = params[:hypervisortype] || 'kvm'
230
+ p[:user] = params[:user] || 'user'
231
+ p[:password] = params[:secret] || 'secret'
232
+ p[:port] = params[:port] || '8889'
233
+ p[:virtual_switch] = params[:virtual_switch]
234
+ res = RestClient.get Api.url + "/admin/datacenters/#{datacenter_id}/action/discover", :params => p, :content_type => :xml
235
+ machine = Machine.parse res
236
+ end
237
+
238
+ end
239
+
240
+ class RemoteService
241
+ attr_reader :attributes
242
+
243
+ def initialize(attributes)
244
+ @attributes = attributes
245
+ end
246
+
247
+ def uri
248
+ @attributes[:uri]
249
+ end
250
+
251
+ def rs_type
252
+ @attributes[:rs_type]
253
+ end
254
+
255
+ def self.from_type(address, type, use_ssl = false)
256
+ rs_type_map = {
257
+ 'STORAGE_SYSTEM_MONITOR' => 'ssm',
258
+ 'VIRTUAL_FACTORY' => 'virtualfactory',
259
+ 'VIRTUAL_SYSTEM_MONITOR' => 'vsm',
260
+ 'NODE_COLLECTOR' => 'nodecollector',
261
+ 'APPLIANCE_MANAGER' => 'am',
262
+ }
263
+ case type
264
+ when RemoteServiceType::BPM_SERVICE
265
+ RemoteService.new :uri => "tcp://#{address}:61616", :rs_type => type
266
+ when RemoteServiceType::DHCP_SERVICE
267
+ RemoteService.new :uri => "omapi://#{address}:7911", :rs_type => type
268
+ else
269
+ if use_ssl
270
+ RemoteService.new :uri => "https://#{address}:443", :rs_type => type
271
+ else
272
+ RemoteService.new :uri => "http://#{address}:80/#{rs_type_map[type]}", :rs_type => type
273
+ end
274
+ end
275
+ end
276
+
277
+ def self.create_for_host(address, use_ssl = false)
278
+ items = []
279
+ %w(
280
+ STORAGE_SYSTEM_MONITOR
281
+ VIRTUAL_FACTORY
282
+ VIRTUAL_SYSTEM_MONITOR
283
+ NODE_COLLECTOR
284
+ APPLIANCE_MANAGER
285
+ DHCP_SERVICE
286
+ BPM_SERVICE
287
+ ).each do |t|
288
+ items << RemoteService.from_type(address, t, use_ssl)
289
+ end
290
+ items
291
+ end
162
292
  end
163
293
 
164
294
  class Rack
@@ -189,6 +319,268 @@ module WeBee
189
319
  RestClient.delete(Api.url + "/admin/datacenters/#{datacenter_id}/racks/#{rack_id}", :content_type => :xml)
190
320
  end
191
321
 
322
+ def add_machine(machine)
323
+ res = RestClient.post Api.url + "/admin/datacenters/#{datacenter_id}/racks/#{rack_id}/machines", machine.to_xml, :content_type => :xml
324
+ end
325
+
326
+ def machines
327
+ u = []
328
+ doc = Nokogiri.parse(RestClient.get(Api.url + "/admin/datacenters/#{datacenter_id}/racks/#{rack_id}/machines", :content_type => :xml))
329
+ doc.search('//machine').each do |node|
330
+ u << Machine.parse(node.to_s)
331
+ end
332
+ u
333
+ end
334
+
335
+ end
336
+
337
+ class Datastore
338
+ include SAXMachine
339
+
340
+ element :id, :as => :datastore_id
341
+ element :directory
342
+ element :rootPath, :as => :root_path
343
+ element :enabled
344
+ element :name
345
+ element :size
346
+ element :usedSize, :as => :used_size
347
+
348
+ def to_xml
349
+ xm = Builder::XmlMarkup.new
350
+ xm.datastore {
351
+ xm.name name
352
+ xm.directory directory
353
+ xm.rootPath root_path
354
+ xm.enabled enabled
355
+ xm.size size
356
+ xm.usedSize used_size
357
+ }
358
+ end
359
+
360
+ end
361
+
362
+ class Machine
363
+ include SAXMachine
364
+
365
+ attr_accessor :raw
366
+ attr_accessor :datacenter_id
367
+
368
+ element :id, :as => :machine_id
369
+ element :description
370
+ element :ip
371
+ element :ipService, :as => :ip_service
372
+ element :name
373
+ element :password
374
+ element :user
375
+ element :realCpu, :as => :real_cpu
376
+ element :realHd, :as => :real_hd
377
+ element :realRam, :as => :real_ram
378
+ element :state
379
+ element :type, :as => :hypervisortype
380
+ element :cpu
381
+ element :cpuRatio, :as => :cpu_ratio
382
+ element :cpuUsed, :as => :cpu_used
383
+ element :hd
384
+ element :hdUsed, :as => :hd_used
385
+ element :ram
386
+ element :ramUsed, :as => :ram_used
387
+ element :virtualSwitch, :as => :virtual_switch
388
+ elements :datastore, :as => :datastores, :class => Datastore
389
+
390
+ def virtual_switches
391
+ virtual_switch.split('/')
392
+ end
393
+
394
+ def to_xml
395
+ xm = Builder::XmlMarkup.new
396
+ xm.machine {
397
+ xm.name name
398
+ xm.description description
399
+ xm.ip ip
400
+ xm.ipService ip_service
401
+ xm.user user
402
+ xm.password password
403
+ xm.realCpu real_cpu
404
+ xm.realHd real_hd
405
+ xm.realRam real_ram
406
+ xm.state state
407
+ xm.type hypervisortype
408
+ xm.cpu cpu
409
+ xm.cpuRatio cpu_ratio
410
+ xm.cpuUsed cpu_used
411
+ xm.hd hd
412
+ xm.hdUsed hd_used
413
+ xm.ram ram
414
+ xm.ramUsed ram_used
415
+ xm.virtualSwitch virtual_switch
416
+ xm.datastores {
417
+ datastores.each do |ds|
418
+ xm.datastore {
419
+ xm.name ds.name
420
+ xm.directory ds.directory
421
+ xm.rootPath ds.root_path
422
+ xm.enabled ds.enabled
423
+ xm.size ds.size
424
+ xm.usedSize ds.used_size
425
+ }
426
+ end
427
+ }
428
+ }
429
+ xm.target!
430
+ end
431
+
432
+ end
433
+
434
+ class RemoteServiceType
435
+ VIRTUAL_FACTORY = 'VIRTUAL_FACTORY'
436
+ STORAGE_SYSTEM_MONITOR = 'STORAGE_SYSTEM_MONITOR'
437
+ VIRTUAL_SYSTEM_MONITOR = 'VIRTUAL_SYSTEM_MONITOR'
438
+ NODE_COLLECTOR = 'NODE_COLLECTOR'
439
+ APPLIANCE_MANAGER = 'APPLIANCE_MANAGER'
440
+ DHCP_SERVICE = 'DHCP_SERVICE'
441
+ BPM_SERVICE = 'BPM_SERVICE'
442
+ end
443
+
444
+ #
445
+ # A Virtual Datacenter Network
446
+ # FIXME: Unimplemented
447
+ #
448
+ class VDCNetwork
449
+ include SAXMachine
450
+ element :id, :as => :vdc_id
451
+ element :name
452
+ element :gateway
453
+ element :address
454
+ element :mask
455
+ element :defaultNetwork, :as => :default_network
456
+
457
+ def self.create(attributes)
458
+ xml = attributes.to_xml(:root => 'network')
459
+ VDCNetwork.parse(xml)
460
+ end
461
+
462
+ def to_xml
463
+ end
464
+ end
465
+
466
+ #
467
+ # A virtual datacenter
468
+ # FIXME: Unimplemented
469
+ class VDC
470
+ include SAXMachine
471
+ element :id, :as => :vdc_id
472
+ element :name
473
+ element :ramSoft, :as => :ram_soft
474
+ element :ramHard, :as => :ram_hard
475
+ element :cpuSoft, :as => :cpu_soft
476
+ element :cpuHard, :as => :cpu_hard
477
+ element :storageSoft, :as => :storage_soft
478
+ element :storageHard, :as => :storage_hard
479
+ element :publicIpsSoft, :as => :public_ip_soft
480
+ element :publicIpsHard, :as => :public_ip_hard
481
+ element :hdSoft, :as => :hd_soft
482
+ element :hdHard, :as => :hd_hard
483
+ element :vlanSoft, :as => :vlan_soft
484
+ element :vlanHard, :as => :vlan_hard
485
+ element :hypervisorType, :as => :hypervisortype
486
+ element :network, :class => VDCNetwork
487
+
488
+ def self.create(attributes)
489
+ datacenter = attributes[:datacenter].datacenter_id
490
+ enterprise = attributes[:enterprise].resource_id
491
+ if attributes[:network].nil?
492
+ net = VDCNetwork.new
493
+ net.name = 'defaultNetwork'
494
+ net.gateway = '192.168.1.1'
495
+ net.address = '192.168.1.0'
496
+ net.mask = '24'
497
+ net.default_network = true
498
+ attributes[:network] = net
499
+ end
500
+ xm = Builder::XmlMarkup.new
501
+ xm.virtualDatacenter {
502
+ xm.name attributes[:name]
503
+ xm.cpuSoft(attributes[:cpu_soft] || "0")
504
+ xm.cpuHard(attributes[:cpu_hard] || "0")
505
+ xm.vlanSoft(attributes[:vlan_soft] || "0")
506
+ xm.vlanHard(attributes[:vlan_hard] || "0")
507
+ xm.ramSoft(attributes[:ram_soft] || "0")
508
+ xm.ramHard(attributes[:ram_hard] || "0")
509
+ xm.publicIpsSoft(attributes[:public_ip_soft] || "0" )
510
+ xm.publicIpsHard(attributes[:public_ip_hard] || "0")
511
+ xm.hdSoft(attributes[:hd_soft] || "0")
512
+ xm.hdHard(attributes[:hd_hard] || "0" )
513
+ xm.storageSoft(attributes[:storage_soft] || "0")
514
+ xm.storageHard(attributes[:storage_hard] || "0")
515
+ xm.hypervisorType attributes[:hypervisortype]
516
+ xm.network {
517
+ xm.name(attributes[:network].name || 'defaultNetwork')
518
+ xm.gateway(attributes[:network].gateway || '192.168.1.1')
519
+ xm.address(attributes[:network].address || '192.168.1.0')
520
+ xm.mask(attributes[:network].mask || '24')
521
+ xm.defaultNetwork(attributes[:network].default_network || true)
522
+ }
523
+ }
524
+ res = RestClient.post(Api.url + "/cloud/virtualdatacenters/?datacenter=#{datacenter}&enterprise=#{enterprise}", xm.target!, :content_type => :xml)
525
+ VDC.parse(res)
526
+ end
527
+
528
+ def to_xml
529
+ xm = Builder::XmlMarkup.new
530
+ xm.virtualDatacenter {
531
+ xm.name name
532
+ xm.cpuSoft(cpu_soft || "0")
533
+ xm.cpuHard(cpu_hard || "0")
534
+ xm.vlanSoft(vlan_soft || "0")
535
+ xm.vlanHard(vlan_hard || "0")
536
+ xm.ramSoft(ram_soft || "0")
537
+ xm.ramHard(ram_hard || "0")
538
+ xm.repositorySoft(repository_soft || "0")
539
+ xm.repositoryHard(repository_hard || "0")
540
+ xm.publicIpsSoft(public_ip_soft || "0" )
541
+ xm.publicIpsHard(public_ip_hard || "0" )
542
+ xm.hdSoft(hd_soft || "0")
543
+ xm.hdHard(hd_hard || "0")
544
+ xm.storageSoft(storage_soft || "0")
545
+ xm.storageHard(storage_hard || "0")
546
+ xm.network {
547
+ xm.name(network.name || 'defaultNetwork')
548
+ xm.gateway(network.gateway || '192.168.1.1')
549
+ xm.address(network.address || '192.168.1.0')
550
+ xm.mask(network.mask || '24')
551
+ xm.defaultNetwork(netowork.default_network || true)
552
+ }
553
+ }
554
+ xm.target!
555
+ end
556
+
557
+ def self.all(params = {})
558
+ items = []
559
+ if params.empty?
560
+ doc = Nokogiri.parse(RestClient.get(Api.url + "/cloud/virtualdatacenters", :accept => :xml))
561
+ doc.search('//virtualDatacenter').each do |node|
562
+ items << VDC.parse(node.to_s)
563
+ end
564
+ else
565
+ extra = []
566
+ if params[:enterprise_id]
567
+ extra << "enterprise=#{params[:enterprise_id]}"
568
+ end
569
+ if params[:datacenter_id]
570
+ extra << "datacenter=#{params[:datacenter_id]}"
571
+ end
572
+ doc = Nokogiri.parse(RestClient.get(Api.url + "/cloud/virtualdatacenters?#{extra.join('&')}", :accept => :xml))
573
+ doc.search('//virtualDatacenter').each do |node|
574
+ items << VDC.parse(node.to_s)
575
+ end
576
+ end
577
+ items
578
+ end
579
+
580
+ def self.find_by_name(name, options = {})
581
+ VDC.all(options).find_all { |vdc| vdc.name =~ /#{name}/ }
582
+ end
583
+
192
584
  end
193
585
 
194
586
  class Enterprise
@@ -200,6 +592,22 @@ module WeBee
200
592
 
201
593
  element :id, :as => :resource_id
202
594
  element :name
595
+ element :ramSoft, :as => :ram_soft
596
+ element :ramHard, :as => :ram_hard
597
+ element :cpuSoft, :as => :cpu_soft
598
+ element :cpuHard, :as => :cpu_hard
599
+ element :storageSoft, :as => :storage_soft
600
+ element :storageHard, :as => :storage_hard
601
+ element :repositorySoft, :as => :repository_soft
602
+ element :repositoryHard, :as => :repository_hard
603
+ element :publicIpsSoft, :as => :public_ip_soft
604
+ element :publicIpsHard, :as => :public_ip_hard
605
+ element :hdSoft, :as => :hd_soft
606
+ element :hdHard, :as => :hd_hard
607
+ element :vlanSoft, :as => :vlan_soft
608
+ element :vlanHard, :as => :vlan_hard
609
+ element :isReservationRestricted, :as => :is_reservation_restricted
610
+
203
611
 
204
612
  def delete
205
613
  RestClient.delete(Api.url + "/admin/enterprises/#{resource_id}")
@@ -209,13 +617,30 @@ module WeBee
209
617
  # may raise Exception if recuest is not successful
210
618
  #
211
619
  def self.create(attributes = {})
212
- xml = attributes.to_xml(:root => 'enterprise')
213
- res = RestClient.post(Api.url + '/admin/enterprises', xml, :content_type => :xml, :accept => :xml)
620
+ xm = Builder::XmlMarkup.new
621
+ xm.enterprise {
622
+ xm.name attributes[:name]
623
+ xm.cpuSoft(attributes[:cpu_soft] || "0")
624
+ xm.cpuHard(attributes[:cpu_hard] || "0")
625
+ xm.vlanSoft(attributes[:vlan_soft] || "0")
626
+ xm.vlanHard(attributes[:vlan_hard] || "0")
627
+ xm.ramSoft(attributes[:ram_soft] || "0")
628
+ xm.ramHard(attributes[:ram_hard] || "0")
629
+ xm.repositorySoft(attributes[:repository_soft] || "0")
630
+ xm.repositoryHard(attributes[:repository_hard] || "0")
631
+ xm.publicIpsSoft(attributes[:public_ip_soft] || "0" )
632
+ xm.publicIpsHard(attributes[:public_ip_hard] || "0" )
633
+ xm.hdSoft(attributes[:hd_soft] || "0")
634
+ xm.hdHard(attributes[:hd_hard] || "0")
635
+ xm.storageSoft(attributes[:storage_soft] || "0")
636
+ xm.storageHard(attributes[:storage_hard] || "0")
637
+ }
638
+ res = RestClient.post(Api.url + '/admin/enterprises', xm.target!, :content_type => :xml)
214
639
  Enterprise.parse(res)
215
640
  end
216
641
 
217
642
  def self.find_by_name(name, options = {})
218
- Enterprise.all(options).find { |e| e.name =~ /#{name}/ }
643
+ Enterprise.all(options).find_all { |e| e.name =~ /#{name}/ }
219
644
  end
220
645
 
221
646
  def ovf_packages
@@ -231,9 +656,44 @@ module WeBee
231
656
  col
232
657
  end
233
658
 
234
- def create_user(attributes)
235
- attributes[:enterprise] = self
236
- User.create attributes
659
+ def create_user(params)
660
+ params[:enterprise] = self
661
+ User.create params
662
+ end
663
+
664
+ def create_vdc(params)
665
+ params[:enterprise] = self
666
+ VDC.create(params)
667
+ end
668
+
669
+ def set_limits_for_datacenter(dc, params = {})
670
+ datacenter = dc.datacenter_id
671
+ xm = Builder::XmlMarkup.new
672
+ xm.limit {
673
+ xm.cpuSoft(params[:cpu_soft] || "0")
674
+ xm.cpuHard(params[:cpu_hard] || "0")
675
+ xm.vlanSoft(params[:vlan_soft] || "0")
676
+ xm.vlanHard(params[:vlan_hard] || "0")
677
+ xm.ramSoft(params[:ram_soft] || "0")
678
+ xm.ramHard(params[:ram_hard] || "0")
679
+ xm.repositorySoft(params[:repository_soft] || "0")
680
+ xm.repositoryHard(params[:repository_hard] || "0")
681
+ xm.publicIpsSoft(params[:public_ip_soft] || "0" )
682
+ xm.publicIpsHard(params[:public_ip_hard] || "0" )
683
+ xm.hdSoft(params[:hd_soft] || "0")
684
+ xm.hdHard(params[:hd_hard] || "0")
685
+ xm.storageSoft(params[:storage_soft] || "0")
686
+ xm.storageHard(params[:storage_hard] || "0")
687
+ }
688
+ res = RestClient.post(Api.url + "/admin/enterprises/#{resource_id}/limits?datacenter=#{datacenter}", xm.target!, :content_type => :xml)
689
+ end
690
+
691
+ def vdcs
692
+ VDC.all(:enterprise_id => resource_id)
693
+ end
694
+
695
+ def enterprise_id
696
+ resource_id
237
697
  end
238
698
 
239
699
  end
@@ -253,14 +713,14 @@ module WeBee
253
713
  #
254
714
  # May raise exception if request is not successful
255
715
  #
256
- def self.create(attributes)
257
- if attributes[:role]
258
- role = "<link rel='role' href='#{attributes[:role]}'/>"
259
- attributes.delete :role
716
+ def self.create(params)
717
+ if params[:role]
718
+ role = "<link rel='role' href='#{params[:role]}'/>"
719
+ params.delete :role
260
720
  end
261
- eid = attributes[:enterprise].resource_id
262
- attributes.delete :enterprise
263
- xml = attributes.to_xml(:root => 'user')
721
+ eid = params[:enterprise].resource_id
722
+ params.delete :enterprise
723
+ xml = params.to_xml(:root => 'user')
264
724
  xml = xml.gsub('</user>', "#{role}</user>")
265
725
  res = RestClient.post(Api.url + "/admin/enterprises/#{eid}/users", xml, :content_type => :xml, :accept => :xml)
266
726
  user = User.parse(res)
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestWebee < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webee
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- version: "0.1"
8
+ - 2
9
+ version: "0.2"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Sergio Rubio
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-07-29 00:00:00 Z
17
+ date: 2011-09-08 00:00:00 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: sax-machine
@@ -95,6 +95,9 @@ files:
95
95
  - lib/webee/commands/user.rb
96
96
  - test/helper.rb
97
97
  - test/test_webee.rb
98
+ - examples/test.rb
99
+ - examples/test2.rb
100
+ - test/test_appliance_manager.rb
98
101
  homepage: http://github.com/rubiojr/webee
99
102
  licenses:
100
103
  - MIT
@@ -124,9 +127,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
127
  requirements: []
125
128
 
126
129
  rubyforge_project:
127
- rubygems_version: 1.8.5
130
+ rubygems_version: 1.7.2
128
131
  signing_key:
129
132
  specification_version: 3
130
133
  summary: Abiquo API Ruby Implementation
131
- test_files: []
132
-
134
+ test_files:
135
+ - examples/basics.rb
136
+ - examples/test.rb
137
+ - examples/test2.rb
138
+ - test/helper.rb
139
+ - test/test_appliance_manager.rb
140
+ - test/test_webee.rb