rbovirt 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rbovirt might be problematic. Click here for more details.

data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 0.0.8
@@ -0,0 +1,29 @@
1
+ module OVIRT
2
+ class Client
3
+ def cluster_version?(cluster_id, major)
4
+ c = cluster(cluster_id)
5
+ c.version.split('.')[0] == major
6
+ end
7
+
8
+ def clusters(opts={})
9
+ headers = {:accept => "application/xml; detail=datacenters"}
10
+ search= opts[:search] || ("datacenter=%s" % current_datacenter.name)
11
+ http_get("/clusters?search=%s" % CGI.escape(search), headers).xpath('/clusters/cluster').collect do |cl|
12
+ OVIRT::Cluster.new(self, cl)
13
+ end
14
+ end
15
+
16
+ def cluster(cluster_id)
17
+ headers = {:accept => "application/xml; detail=datacenters"}
18
+ cluster_xml = http_get("/clusters/%s" % cluster_id, headers)
19
+ OVIRT::Cluster.new(self, cluster_xml.root)
20
+ end
21
+
22
+ def networks(opts)
23
+ cluster_id = opts[:cluster_id] || current_cluster.id
24
+ http_get("/clusters/%s/networks" % cluster_id, http_headers).xpath('/networks/network').collect do |cl|
25
+ OVIRT::Network.new(self, cl)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ module OVIRT
2
+ class Client
3
+ def datacenter(datacenter_id)
4
+ begin
5
+ datacenter = http_get("/datacenters/%s" % datacenter_id)
6
+ OVIRT::DataCenter::new(self, datacenter.root)
7
+ rescue
8
+ handle_fault $!
9
+ end
10
+ end
11
+
12
+ def datacenters(opts={})
13
+ search = opts[:search] ||""
14
+ datacenters = http_get("/datacenters?search=%s" % CGI.escape(search))
15
+ datacenters.xpath('/data_centers/data_center').collect do |dc|
16
+ OVIRT::DataCenter::new(self, dc)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ module OVIRT
2
+ class Client
3
+ def host(host_id, opts={})
4
+ xml_response = http_get("/hosts/%s" % host_id)
5
+ OVIRT::Host::new(self, xml_response.root)
6
+ end
7
+
8
+ def hosts(opts={})
9
+ search= opts[:search] || ("datacenter=%s" % current_datacenter.name)
10
+ http_get("/hosts?search=%s" % CGI.escape(search)).xpath('/hosts/host').collect do |h|
11
+ OVIRT::Host::new(self, h)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module OVIRT
2
+ class Client
3
+ def storagedomain(sd_id)
4
+ sd = http_get("/storagedomains/%s" % sd_id)
5
+ OVIRT::StorageDomain::new(self, sd.root)
6
+ end
7
+
8
+ def storagedomains(opts={})
9
+ search= opts[:search] ||''
10
+ http_get("/storagedomains?search=%s" % CGI.escape(search)).xpath('/storage_domains/storage_domain').collect do |sd|
11
+ OVIRT::StorageDomain::new(self, sd)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,37 @@
1
+ module OVIRT
2
+ class Client
3
+ def templates(opts={})
4
+ search= opts[:search] || ("datacenter=%s" % current_datacenter.name)
5
+ http_get("/templates?search=%s" % CGI.escape(search)).xpath('/templates/template').collect do |t|
6
+ OVIRT::Template::new(self, t)
7
+ end.compact
8
+ end
9
+
10
+ def template(template_id, opts={})
11
+ results = http_get("/templates/%s" % template_id)
12
+ template = OVIRT::Template::new(self, results.root)
13
+ template
14
+ end
15
+
16
+ def create_template(opts)
17
+ template = http_post("/templates", Template.to_xml(opts))
18
+ OVIRT::Template::new(self, template.root)
19
+ end
20
+
21
+ def destroy_template(id)
22
+ http_delete("/templates/%s" % id)
23
+ end
24
+
25
+ def template_interfaces template_id
26
+ http_get("/templates/%s/nics" % template_id, http_headers).xpath('/nics/nic').collect do |nic|
27
+ OVIRT::Interface::new(self, nic)
28
+ end
29
+ end
30
+
31
+ def template_volumes template_id
32
+ http_get("/templates/%s/disks" % template_id, http_headers).xpath('/disks/disk').collect do |disk|
33
+ OVIRT::Volume::new(self, disk)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,65 @@
1
+ module OVIRT
2
+ class Client
3
+ def vm(vm_id, opts={})
4
+ headers = {:accept => "application/xml; detail=disks; detail=nics; detail=hosts"}
5
+ OVIRT::VM::new(self, http_get("/vms/%s" % vm_id, headers).root)
6
+ end
7
+
8
+ def vms(opts={})
9
+ headers = {:accept => "application/xml; detail=disks; detail=nics; detail=hosts"}
10
+ search= opts[:search] || ("datacenter=%s" % current_datacenter.name)
11
+ http_get("/vms?search=%s" % CGI.escape(search), headers).xpath('/vms/vm').collect do |vm|
12
+ OVIRT::VM::new(self, vm)
13
+ end
14
+ end
15
+
16
+ def create_vm(opts)
17
+ opts[:cluster_name] ||= clusters.first.name
18
+ OVIRT::VM::new(self, http_post("/vms",OVIRT::VM.to_xml(opts)).root)
19
+ end
20
+
21
+ def vm_interfaces vm_id
22
+ http_get("/vms/%s/nics" % vm_id, http_headers).xpath('/nics/nic').collect do |nic|
23
+ OVIRT::Interface::new(self, nic)
24
+ end
25
+ end
26
+
27
+ def destroy_interface(vm_id, interface_id)
28
+ http_delete("/vms/%s/nics/%s" % [vm_id, interface_id])
29
+ end
30
+
31
+ def add_interface(vm_id, opts={})
32
+ http_post("/vms/%s/nics" % vm_id, OVIRT::Interface.to_xml( opts))
33
+ end
34
+
35
+ def update_interface(vm_id, interface_id, opts={})
36
+ http_put("/vms/%s/nics/%s" % [vm_id, interface_id], OVIRT::Interface.to_xml( opts))
37
+ end
38
+
39
+ def vm_volumes vm_id
40
+ http_get("/vms/%s/disks" % vm_id, http_headers).xpath('/disks/disk').collect do |disk|
41
+ OVIRT::Volume::new(self, disk)
42
+ end
43
+ end
44
+
45
+ def add_volume(vm_id, opts={})
46
+ storage_domain_id = opts[:storage_domain] || storagedomains.first.id
47
+ http_post("/vms/%s/disks" % vm_id, OVIRT::Volume.to_xml(storage_domain_id, opts))
48
+ end
49
+
50
+ def vm_action(id, action, opts={})
51
+ xml_response = http_post("/vms/%s/%s" % [id, action],'<action/>', opts)
52
+ return (xml_response/'action/status').first.text.strip.upcase=="COMPLETE"
53
+ end
54
+
55
+ def destroy_vm(id)
56
+ http_delete("/vms/%s" % id)
57
+ end
58
+
59
+ def update_vm(opts)
60
+ opts[:cluster_name] ||= clusters.first.name
61
+ result_xml = http_put("/vms/%s" % opts[:id], OVIRT::VM.to_xml(opts))
62
+ OVIRT::VM::new(self, result_xml.root)
63
+ end
64
+ end
65
+ end
@@ -3,30 +3,42 @@ module OVIRT
3
3
  class Interface < BaseObject
4
4
  attr_reader :name, :mac, :interface, :network, :vm
5
5
 
6
- def initialize(client, xml)
7
- super(client, xml[:id], xml[:href], (xml/'name').first.text)
8
- parse_xml_attributes!(xml)
6
+ def initialize(client=nil, xml={})
7
+ if xml.is_a?(Hash)
8
+ super(client, xml[:id], xml[:href], xml[:name])
9
+ @network = xml[:network]
10
+ @persisted = xml[:persisted]
11
+ else
12
+ super(client, xml[:id], xml[:href], (xml/'name').first.text)
13
+ parse_xml_attributes!(xml)
14
+ end
9
15
  self
10
16
  end
11
17
 
12
18
  def self.to_xml(opts={})
13
19
  builder = Nokogiri::XML::Builder.new do
14
20
  nic{
15
- name_(opts[:name] || 'eth0')
16
- network{
17
- name_(opts[:network] || 'ovirtmgmt')
18
- }
21
+ name_(opts[:name] || "nic-#{Time.now.to_i}")
22
+ if opts[:network]
23
+ network :id => opts[:network]
24
+ else
25
+ network{ name_(opts[:network_name] || 'ovirtmgmt') }
26
+ end
19
27
  }
20
28
  end
21
29
  Nokogiri::XML(builder.to_xml).root.to_s
22
30
  end
23
31
 
32
+ def persisted?
33
+ @persisted || !!id
34
+ end
35
+
24
36
  def parse_xml_attributes!(xml)
25
- @name = (xml/'name').first.text
26
- @mac = (xml/'mac').first[:address]
37
+ @mac = (xml/'mac').first[:address] rescue nil #template interfaces doesn't have MAC address.
27
38
  @interface = (xml/'interface').first.text
28
39
  @network = (xml/'network').first[:id]
29
- @vm = Link::new(@client, (xml/'vm').first[:id], (xml/'vm').first[:href])
40
+ @vm = Link::new(@client, (xml/'vm').first[:id], (xml/'vm').first[:href]) if (xml/'vm') rescue nil
41
+ @template = Link::new(@client, (xml/'template').first[:id], (xml/'template').first[:href]) rescue nil
30
42
  end
31
43
 
32
44
  end
@@ -19,10 +19,16 @@ module OVIRT
19
19
  Nokogiri::XML(builder.to_xml).root.to_s
20
20
  end
21
21
 
22
- private
22
+ def interfaces
23
+ @interfaces ||= @client.template_interfaces(id)
24
+ end
23
25
 
24
- def parse_xml_attributes!(xml)
26
+ def volumes
27
+ @volumes ||= @client.send(:volumes, "/templates/%s/disks" % id)
28
+ end
25
29
 
30
+ private
31
+ def parse_xml_attributes!(xml)
26
32
  @description = ((xml/'description').first.text rescue '')
27
33
  @status = ((xml/'status').first.text rescue 'unknown')
28
34
  @memory = (xml/'memory').first.text
data/lib/ovirt/vm.rb CHANGED
@@ -18,11 +18,11 @@ module OVIRT
18
18
  end
19
19
 
20
20
  def interfaces
21
- @interfaces ||= @client.interfaces(id)
21
+ @interfaces ||= @client.vm_interfaces(id)
22
22
  end
23
23
 
24
24
  def volumes
25
- @volumes ||= @client.disks(id)
25
+ @volumes ||= @client.vm_volumes(id)
26
26
  end
27
27
 
28
28
  def self.to_xml( opts={})
data/lib/rbovirt.rb CHANGED
@@ -9,6 +9,13 @@ require "ovirt/volume"
9
9
  require "ovirt/interface"
10
10
  require "ovirt/network"
11
11
 
12
+ require "client/vm_api"
13
+ require "client/template_api"
14
+ require "client/cluster_api"
15
+ require "client/host_api"
16
+ require "client/datacenter_api"
17
+ require "client/storage_domain_api"
18
+
12
19
  require "nokogiri"
13
20
  require "rest_client"
14
21
 
@@ -37,44 +44,6 @@ module OVIRT
37
44
  @api_entrypoint = api_entrypoint
38
45
  end
39
46
 
40
- def vm(vm_id, opts={})
41
- headers = {:accept => "application/xml; detail=disks; detail=nics; detail=hosts"}
42
- vm = OVIRT::VM::new(self, http_get("/vms/%s" % vm_id, headers).root)
43
- # optional eager loading
44
- vm.interfaces = interfaces(vm_id) if opts[:include] && opts[:include].include?(:interfaces)
45
- vm.volumes = volumes(vm_id) if opts[:include] && opts[:include].include?(:volumes)
46
- vm
47
- end
48
-
49
- def interfaces vm_id
50
- http_get("/vms/%s/nics" % vm_id, http_headers).xpath('/nics/nic').collect do |nic|
51
- OVIRT::Interface::new(self, nic)
52
- end
53
- end
54
-
55
- def volumes vm_id
56
- http_get("/vms/%s/disks" % vm_id, http_headers).xpath('/disks/disk').collect do |disk|
57
- OVIRT::Volume::new(self, disk)
58
- end
59
- end
60
-
61
- def vms(opts={})
62
- headers = {:accept => "application/xml; detail=disks; detail=nics; detail=hosts"}
63
- search= opts[:search] || ("datacenter=%s" % current_datacenter.name)
64
- http_get("/vms?search=%s" % CGI.escape(search), headers).xpath('/vms/vm').collect do |vm|
65
- OVIRT::VM::new(self, vm)
66
- end
67
- end
68
-
69
- def vm_action(id, action, opts={})
70
- xml_response = http_post("/vms/%s/%s" % [id, action],'<action/>', opts)
71
- return (xml_response/'action/status').first.text.strip.upcase=="COMPLETE"
72
- end
73
-
74
- def destroy_vm(id)
75
- http_delete("/vms/%s" % id)
76
- end
77
-
78
47
  def api_version
79
48
  xml = http_get("/")/'/api/product_info/version'
80
49
  (xml/'version').first[:major] +"."+ (xml/'version').first[:minor]
@@ -84,83 +53,7 @@ module OVIRT
84
53
  api_version.split('.')[0] == major
85
54
  end
86
55
 
87
- def cluster_version?(cluster_id, major)
88
- c = cluster(cluster_id)
89
- c.version.split('.')[0] == major
90
- end
91
-
92
- def create_vm(opts)
93
- opts[:cluster_name] ||= clusters.first.name
94
- result_xml = http_post("/vms",OVIRT::VM.to_xml(opts))
95
- OVIRT::VM::new(self, result_xml.root)
96
- end
97
-
98
- def update_vm(opts)
99
- opts[:cluster_name] ||= clusters.first.name
100
- result_xml = http_put("/vms/%s" % opts[:id], OVIRT::VM.to_xml(opts))
101
- OVIRT::VM::new(self, result_xml.root)
102
- end
103
-
104
- def add_volume(vm_id, opts={})
105
- storage_domain_id = opts[:storage_domain] || storagedomains.first.id
106
- result_xml = http_post("/vms/%s/disks" % vm_id, OVIRT::Volume.to_xml(storage_domain_id, opts))
107
- end
108
-
109
-
110
- def add_interface(vm_id, opts={})
111
- http_post("/vms/%s/nics" % vm_id, OVIRT::Interface.to_xml( opts))
112
- end
113
-
114
- def create_template(opts)
115
- template = http_post("/templates", Template.to_xml(opts))
116
- OVIRT::Template::new(self, template.root)
117
- end
118
-
119
- def destroy_template(id)
120
- http_delete("/templates/%s" % id)
121
- end
122
-
123
- def templates(opts={})
124
- search= opts[:search] || ("datacenter=%s" % current_datacenter.name)
125
- http_get("/templates?search=%s" % CGI.escape(search)).xpath('/templates/template').collect do |t|
126
- OVIRT::Template::new(self, t)
127
- end.compact
128
- end
129
-
130
- def template(template_id)
131
- template = http_get("/templates/%s" % template_id)
132
- OVIRT::Template::new(self, template.root)
133
- end
134
-
135
- def datacenters(opts={})
136
- search = opts[:search] ||""
137
- datacenters = http_get("/datacenters?search=%s" % CGI.escape(search))
138
- datacenters.xpath('/data_centers/data_center').collect do |dc|
139
- OVIRT::DataCenter::new(self, dc)
140
- end
141
- end
142
-
143
- def clusters(opts={})
144
- headers = {:accept => "application/xml; detail=datacenters"}
145
- search= opts[:search] || ("datacenter=%s" % current_datacenter.name)
146
- http_get("/clusters?search=%s" % CGI.escape(search), headers).xpath('/clusters/cluster').collect do |cl|
147
- OVIRT::Cluster.new(self, cl)
148
- end
149
- end
150
-
151
- def cluster(cluster_id)
152
- headers = {:accept => "application/xml; detail=datacenters"}
153
- cluster_xml = http_get("/clusters/%s" % cluster_id, headers)
154
- OVIRT::Cluster.new(self, cluster_xml.root)
155
- end
156
-
157
- def networks(opts)
158
- cluster_id = opts[:cluster_id] || current_cluster.id
159
- http_get("/clusters/%s/networks" % cluster_id, http_headers).xpath('/networks/network').collect do |cl|
160
- OVIRT::Network.new(self, cl)
161
- end
162
- end
163
-
56
+ private
164
57
  def current_datacenter
165
58
  @current_datacenter ||= self.datacenter_id ? datacenter(self.datacenter_id) : datacenters.first
166
59
  end
@@ -169,41 +62,6 @@ module OVIRT
169
62
  @current_cluster ||= self.cluster_id ? cluster(self.cluster_id) : clusters.first
170
63
  end
171
64
 
172
- def datacenter(datacenter_id)
173
- begin
174
- datacenter = http_get("/datacenters/%s" % datacenter_id)
175
- OVIRT::DataCenter::new(self, datacenter.root)
176
- rescue
177
- handle_fault $!
178
- end
179
- end
180
-
181
- def host(host_id, opts={})
182
- xml_response = http_get("/hosts/%s" % host_id)
183
- OVIRT::Host::new(self, xml_response.root)
184
- end
185
-
186
- def hosts(opts={})
187
- search= opts[:search] || ("datacenter=%s" % current_datacenter.name)
188
- http_get("/hosts?search=%s" % CGI.escape(search)).xpath('/hosts/host').collect do |h|
189
- OVIRT::Host::new(self, h)
190
- end
191
- end
192
-
193
- def storagedomain(sd_id)
194
- sd = http_get("/storagedomains/%s" % sd_id)
195
- OVIRT::StorageDomain::new(self, sd.root)
196
- end
197
-
198
- def storagedomains(opts={})
199
- search= opts[:search] ||''
200
- http_get("/storagedomains?search=%s" % CGI.escape(search)).xpath('/storage_domains/storage_domain').collect do |sd|
201
- OVIRT::StorageDomain::new(self, sd)
202
- end
203
- end
204
-
205
- private
206
-
207
65
  def http_get(suburl, headers={})
208
66
  begin
209
67
  Nokogiri::XML(RestClient::Resource.new(@api_entrypoint)[suburl].get(http_headers(headers)))
@@ -287,7 +145,5 @@ module OVIRT
287
145
  object_class = ::OVIRT.const_get(xml.root.name.camelize)
288
146
  object_class.new(@client, (xml.root))
289
147
  end
290
-
291
148
  end
292
-
293
149
  end
data/rbovirt.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rbovirt}
8
- s.version = "0.0.7"
8
+ s.version = "0.0.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Amos Benari"]
12
- s.date = %q{2012-02-20}
12
+ s.date = %q{2012-03-12}
13
13
  s.description = %q{A Ruby client for oVirt REST API}
14
14
  s.email = %q{abenari@redhat.com}
15
15
  s.extra_rdoc_files = [
@@ -23,6 +23,12 @@ Gem::Specification.new do |s|
23
23
  "README.rdoc",
24
24
  "Rakefile",
25
25
  "VERSION",
26
+ "lib/client/cluster_api.rb",
27
+ "lib/client/datacenter_api.rb",
28
+ "lib/client/host_api.rb",
29
+ "lib/client/storage_domain_api.rb",
30
+ "lib/client/template_api.rb",
31
+ "lib/client/vm_api.rb",
26
32
  "lib/ovirt/base_object.rb",
27
33
  "lib/ovirt/cluster.rb",
28
34
  "lib/ovirt/datacenter.rb",
@@ -16,7 +16,7 @@ describe OVIRT, "API" do
16
16
  before(:all) do
17
17
  user="admin@internal"
18
18
  password="123123"
19
- hostname = "ovirt.sat.lab.tlv.redhat.com"
19
+ hostname = "covirt.sat.lab.tlv.redhat.com"
20
20
  port = "8080"
21
21
  url = "http://#{hostname}:#{port}/api"
22
22
  @blank_template_id = "00000000-0000-0000-0000-000000000000"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbovirt
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Amos Benari
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-02-20 00:00:00 +02:00
18
+ date: 2012-03-12 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -137,6 +137,12 @@ files:
137
137
  - README.rdoc
138
138
  - Rakefile
139
139
  - VERSION
140
+ - lib/client/cluster_api.rb
141
+ - lib/client/datacenter_api.rb
142
+ - lib/client/host_api.rb
143
+ - lib/client/storage_domain_api.rb
144
+ - lib/client/template_api.rb
145
+ - lib/client/vm_api.rb
140
146
  - lib/ovirt/base_object.rb
141
147
  - lib/ovirt/cluster.rb
142
148
  - lib/ovirt/datacenter.rb