oca 3.2.1 → 3.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -42,6 +42,10 @@ require 'OpenNebula/Group'
42
42
  require 'OpenNebula/GroupPool'
43
43
  require 'OpenNebula/Acl'
44
44
  require 'OpenNebula/AclPool'
45
+ require 'OpenNebula/Datastore'
46
+ require 'OpenNebula/DatastorePool'
47
+ require 'OpenNebula/Cluster'
48
+ require 'OpenNebula/ClusterPool'
45
49
 
46
50
  module OpenNebula
47
51
 
@@ -79,6 +83,30 @@ module OpenNebula
79
83
  value.class==OpenNebula::Error
80
84
  end
81
85
 
86
+
87
+ if OpenNebula::NOKOGIRI
88
+ class NokogiriStreamParser < XMLRPC::XMLParser::AbstractStreamParser
89
+ def initialize
90
+ @parser_class = NokogiriParser
91
+ end
92
+
93
+ class NokogiriParser < Nokogiri::XML::SAX::Document
94
+ include XMLRPC::XMLParser::StreamParserMixin
95
+
96
+ alias :cdata_block :character
97
+ alias :characters :character
98
+ alias :end_element :endElement
99
+ alias :start_element :startElement
100
+
101
+ def parse(str)
102
+ parser = Nokogiri::XML::SAX::Parser.new(self)
103
+ parser.parse(str)
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+
82
110
  # The client class, represents the connection with the core and handles the
83
111
  # xml-rpc calls.
84
112
  class Client
@@ -113,14 +141,15 @@ module OpenNebula
113
141
  end
114
142
 
115
143
  @server = XMLRPC::Client.new2(@one_endpoint)
116
- end
117
144
 
118
- def call(action, *args)
119
-
120
- if XMLPARSER
145
+ if OpenNebula::NOKOGIRI
146
+ @server.set_parser(NokogiriStreamParser.new)
147
+ elsif XMLPARSER
121
148
  @server.set_parser(XMLRPC::XMLParser::XMLStreamParser.new)
122
149
  end
150
+ end
123
151
 
152
+ def call(action, *args)
124
153
  begin
125
154
  response = @server.call_async("one."+action, @one_auth, *args)
126
155
 
@@ -52,7 +52,9 @@ module OpenNebula
52
52
  "IMAGE" => 0x8000000000,
53
53
  "USER" => 0x10000000000,
54
54
  "TEMPLATE" => 0x20000000000,
55
- "GROUP" => 0x40000000000
55
+ "GROUP" => 0x40000000000,
56
+ "DATASTORE" => 0x100000000000,
57
+ "CLUSTER" => 0x200000000000
56
58
  }
57
59
 
58
60
  RIGHTS =
@@ -0,0 +1,236 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+
17
+
18
+ require 'OpenNebula/Pool'
19
+
20
+ module OpenNebula
21
+ class Cluster < PoolElement
22
+ #######################################################################
23
+ # Constants and Class Methods
24
+ #######################################################################
25
+
26
+ CLUSTER_METHODS = {
27
+ :info => "cluster.info",
28
+ :allocate => "cluster.allocate",
29
+ :delete => "cluster.delete",
30
+ :addhost => "cluster.addhost",
31
+ :delhost => "cluster.delhost",
32
+ :adddatastore => "cluster.adddatastore",
33
+ :deldatastore => "cluster.deldatastore",
34
+ :addvnet => "cluster.addvnet",
35
+ :delvnet => "cluster.delvnet"
36
+ }
37
+
38
+ # Creates a Cluster description with just its identifier
39
+ # this method should be used to create plain Cluster objects.
40
+ # +id+ the id of the host
41
+ #
42
+ # Example:
43
+ # cluster = Cluster.new(Cluster.build_xml(3),rpc_client)
44
+ #
45
+ def Cluster.build_xml(pe_id=nil)
46
+ if pe_id
47
+ cluster_xml = "<CLUSTER><ID>#{pe_id}</ID></CLUSTER>"
48
+ else
49
+ cluster_xml = "<CLUSTER></CLUSTER>"
50
+ end
51
+
52
+ XMLElement.build_xml(cluster_xml,'CLUSTER')
53
+ end
54
+
55
+ # Class constructor
56
+ def initialize(xml, client)
57
+ super(xml,client)
58
+ end
59
+
60
+ #######################################################################
61
+ # XML-RPC Methods for the Cluster Object
62
+ #######################################################################
63
+
64
+ # Retrieves the information of the given Cluster.
65
+ def info()
66
+ super(CLUSTER_METHODS[:info], 'CLUSTER')
67
+ end
68
+
69
+ # Allocates a new Cluster in OpenNebula
70
+ #
71
+ # +clustername+ A string containing the name of the Cluster.
72
+ def allocate(clustername)
73
+ super(CLUSTER_METHODS[:allocate], clustername)
74
+ end
75
+
76
+ # Deletes the Cluster
77
+ def delete()
78
+ super(CLUSTER_METHODS[:delete])
79
+ end
80
+
81
+ # Adds a Host to this Cluster
82
+ # @param hid [Integer] Host ID
83
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
84
+ # otherwise
85
+ def addhost(hid)
86
+ return Error.new('ID not defined') if !@pe_id
87
+
88
+ rc = @client.call(CLUSTER_METHODS[:addhost], @pe_id, hid)
89
+ rc = nil if !OpenNebula.is_error?(rc)
90
+
91
+ return rc
92
+ end
93
+
94
+ # Deletes a Host from this Cluster
95
+ # @param hid [Integer] Host ID
96
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
97
+ # otherwise
98
+ def delhost(hid)
99
+ return Error.new('ID not defined') if !@pe_id
100
+
101
+ rc = @client.call(CLUSTER_METHODS[:delhost], @pe_id, hid)
102
+ rc = nil if !OpenNebula.is_error?(rc)
103
+
104
+ return rc
105
+ end
106
+
107
+ # Adds a Datastore to this Cluster
108
+ # @param ds_id [Integer] Datastore ID
109
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
110
+ # otherwise
111
+ def adddatastore(ds_id)
112
+ return Error.new('ID not defined') if !@pe_id
113
+
114
+ rc = @client.call(CLUSTER_METHODS[:adddatastore], @pe_id, ds_id)
115
+ rc = nil if !OpenNebula.is_error?(rc)
116
+
117
+ return rc
118
+ end
119
+
120
+ # Deletes a Datastore from this Cluster
121
+ # @param ds_id [Integer] Datastore ID
122
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
123
+ # otherwise
124
+ def deldatastore(ds_id)
125
+ return Error.new('ID not defined') if !@pe_id
126
+
127
+ rc = @client.call(CLUSTER_METHODS[:deldatastore], @pe_id, ds_id)
128
+ rc = nil if !OpenNebula.is_error?(rc)
129
+
130
+ return rc
131
+ end
132
+
133
+ # Adds a VNet to this Cluster
134
+ # @param vnet_id [Integer] VNet ID
135
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
136
+ # otherwise
137
+ def addvnet(vnet_id)
138
+ return Error.new('ID not defined') if !@pe_id
139
+
140
+ rc = @client.call(CLUSTER_METHODS[:addvnet], @pe_id, vnet_id)
141
+ rc = nil if !OpenNebula.is_error?(rc)
142
+
143
+ return rc
144
+ end
145
+
146
+ # Deletes a VNet from this Cluster
147
+ # @param vnet_id [Integer] VNet ID
148
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
149
+ # otherwise
150
+ def delvnet(vnet_id)
151
+ return Error.new('ID not defined') if !@pe_id
152
+
153
+ rc = @client.call(CLUSTER_METHODS[:delvnet], @pe_id, vnet_id)
154
+ rc = nil if !OpenNebula.is_error?(rc)
155
+
156
+ return rc
157
+ end
158
+
159
+ # ---------------------------------------------------------------------
160
+ # Helpers to get information
161
+ # ---------------------------------------------------------------------
162
+
163
+ # Returns whether or not the host with 'id' is part of this cluster
164
+ # @param id [Integer|Array] host ID
165
+ # @return [Boolean] true if found
166
+ def contains_host?(id)
167
+ contains_resource?('HOSTS/ID', id)
168
+ end
169
+
170
+ # Returns an array with the numeric host ids
171
+ # @return [Array<Integer>]
172
+ def host_ids
173
+ array = Array.new
174
+
175
+ self.each("HOSTS/ID") do |id|
176
+ array << id.text.to_i
177
+ end
178
+
179
+ return array
180
+ end
181
+
182
+ # Returns whether or not the datastore with 'id' is part of this cluster
183
+ # @param id [Integer|Array] datastore ID
184
+ # @return [Boolean] true if found
185
+ def contains_datastore?(id)
186
+ contains_resource?('DATASTORES/ID', id)
187
+ end
188
+
189
+ # Returns an array with the numeric datastore ids
190
+ # @return [Array<Integer>]
191
+ def datastore_ids
192
+ array = Array.new
193
+
194
+ self.each("DATASTORES/ID") do |id|
195
+ array << id.text.to_i
196
+ end
197
+
198
+ return array
199
+ end
200
+
201
+ # Returns whether or not the vnet with 'id' is part of this cluster
202
+ # @param id [Integer|Arrray] vnet ID
203
+ # @return [Boolean] true if found
204
+ def contains_vnet?(id)
205
+ contains_resource?('VNETS/ID', id)
206
+ end
207
+
208
+ # Returns an array with the numeric vnet ids
209
+ # @return [Array<Integer>]
210
+ def vnet_ids
211
+ array = Array.new
212
+
213
+ self.each("VNETS/ID") do |id|
214
+ array << id.text.to_i
215
+ end
216
+
217
+ return array
218
+ end
219
+
220
+ private
221
+
222
+ def contains_resource?(xpath, id)
223
+ id_array = retrieve_elements(xpath)
224
+
225
+ return false if id_array.nil?
226
+
227
+ id = [id] if id.class != Array
228
+
229
+ id.each { |i|
230
+ return false if !id_array.include?(i.to_s)
231
+ }
232
+
233
+ return true
234
+ end
235
+ end
236
+ end
@@ -0,0 +1,56 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+
17
+
18
+ require 'OpenNebula/Pool'
19
+
20
+ module OpenNebula
21
+ class ClusterPool < Pool
22
+ #######################################################################
23
+ # Constants and Class attribute accessors
24
+ #######################################################################
25
+
26
+ NONE_CLUSTER_ID = -1
27
+ DEFAULT_CLUSTER_ID = 0
28
+
29
+ CLUSTER_POOL_METHODS = {
30
+ :info => "clusterpool.info"
31
+ }
32
+
33
+ #######################################################################
34
+ # Class constructor & Pool Methods
35
+ #######################################################################
36
+
37
+ # +client+ a Client object that represents a XML-RPC connection
38
+ def initialize(client)
39
+ super('CLUSTER_POOL','CLUSTER',client)
40
+ end
41
+
42
+ # Factory method to create Cluster objects
43
+ def factory(element_xml)
44
+ OpenNebula::Cluster.new(element_xml,@client)
45
+ end
46
+
47
+ #######################################################################
48
+ # XML-RPC Methods for the Cluster Object
49
+ #######################################################################
50
+
51
+ # Retrieves all the Clusters in the pool.
52
+ def info()
53
+ super(CLUSTER_POOL_METHODS[:info])
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,147 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+
17
+
18
+ require 'OpenNebula/Pool'
19
+
20
+ module OpenNebula
21
+ class Datastore < PoolElement
22
+ #######################################################################
23
+ # Constants and Class Methods
24
+ #######################################################################
25
+
26
+ DATASTORE_METHODS = {
27
+ :info => "datastore.info",
28
+ :allocate => "datastore.allocate",
29
+ :delete => "datastore.delete",
30
+ :update => "datastore.update",
31
+ :chown => "datastore.chown",
32
+ :chmod => "datastore.chmod"
33
+ }
34
+
35
+ # Creates a Datastore description with just its identifier
36
+ # this method should be used to create plain Datastore objects.
37
+ # +id+ the id of the user
38
+ #
39
+ # Example:
40
+ # datastore = Datastore.new(Datastore.build_xml(3),rpc_client)
41
+ #
42
+ def Datastore.build_xml(pe_id=nil)
43
+ if pe_id
44
+ datastore_xml = "<DATASTORE><ID>#{pe_id}</ID></DATASTORE>"
45
+ else
46
+ datastore_xml = "<DATASTORE></DATASTORE>"
47
+ end
48
+
49
+ XMLElement.build_xml(datastore_xml,'DATASTORE')
50
+ end
51
+
52
+ # Class constructor
53
+ def initialize(xml, client)
54
+ super(xml,client)
55
+ end
56
+
57
+ #######################################################################
58
+ # XML-RPC Methods for the Datastore Object
59
+ #######################################################################
60
+
61
+ # Retrieves the information of the given Datastore.
62
+ def info()
63
+ super(DATASTORE_METHODS[:info], 'DATASTORE')
64
+ end
65
+
66
+ # Allocates a new Datastore in OpenNebula
67
+ #
68
+ # @param description [String] The template of the Datastore.
69
+ # @param cluster_id [Integer] Id of the cluster
70
+ #
71
+ # @return [Integer, OpenNebula::Error] the new ID in case of
72
+ # success, error otherwise
73
+ def allocate(description, cluster_id=ClusterPool::NONE_CLUSTER_ID)
74
+ super(DATASTORE_METHODS[:allocate], description, cluster_id)
75
+ end
76
+
77
+ # Deletes the Datastore
78
+ def delete()
79
+ super(DATASTORE_METHODS[:delete])
80
+ end
81
+
82
+ # Replaces the template contents
83
+ #
84
+ # @param new_template [String] New template contents
85
+ #
86
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
87
+ # otherwise
88
+ def update(new_template)
89
+ super(DATASTORE_METHODS[:update], new_template)
90
+ end
91
+
92
+ # Changes the owner/group
93
+ #
94
+ # @param uid [Integer] the new owner id. Set to -1 to leave the current one
95
+ # @param gid [Integer] the new group id. Set to -1 to leave the current one
96
+ #
97
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
98
+ # otherwise
99
+ def chown(uid, gid)
100
+ super(DATASTORE_METHODS[:chown], uid, gid)
101
+ end
102
+
103
+ # Changes the datastore permissions.
104
+ #
105
+ # @param octet [String] Permissions octed , e.g. 640
106
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
107
+ # otherwise
108
+ def chmod_octet(octet)
109
+ super(DATASTORE_METHODS[:chmod], octet)
110
+ end
111
+
112
+ # Changes the datastore permissions.
113
+ # Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change
114
+ #
115
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
116
+ # otherwise
117
+ def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
118
+ other_m, other_a)
119
+ super(DATASTORE_METHODS[:chmod], owner_u, owner_m, owner_a, group_u,
120
+ group_m, group_a, other_u, other_m, other_a)
121
+ end
122
+
123
+ # ---------------------------------------------------------------------
124
+ # Helpers to get information
125
+ # ---------------------------------------------------------------------
126
+
127
+ # Returns whether or not the image with id 'id' is part of this datastore
128
+ def contains(id)
129
+ #This doesn't work in ruby 1.8.5
130
+ #return self["DATASTORE/ID[.=#{uid}]"] != nil
131
+
132
+ id_array = retrieve_elements('IMAGES/ID')
133
+ return id_array != nil && id_array.include?(uid.to_s)
134
+ end
135
+
136
+ # Returns an array with the numeric image ids
137
+ def img_ids
138
+ array = Array.new
139
+
140
+ self.each("IMAGES/ID") do |id|
141
+ array << id.text.to_i
142
+ end
143
+
144
+ return array
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,53 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+
17
+
18
+ require 'OpenNebula/Pool'
19
+
20
+ module OpenNebula
21
+ class DatastorePool < Pool
22
+ #######################################################################
23
+ # Constants and Class attribute accessors
24
+ #######################################################################
25
+
26
+ DATASTORE_POOL_METHODS = {
27
+ :info => "datastorepool.info"
28
+ }
29
+
30
+ #######################################################################
31
+ # Class constructor & Pool Methods
32
+ #######################################################################
33
+
34
+ # +client+ a Client object that represents a XML-RPC connection
35
+ def initialize(client)
36
+ super('DATASTORE_POOL','DATASTORE',client)
37
+ end
38
+
39
+ # Factory method to create User objects
40
+ def factory(element_xml)
41
+ OpenNebula::Group.new(element_xml,@client)
42
+ end
43
+
44
+ #######################################################################
45
+ # XML-RPC Methods for the User Object
46
+ #######################################################################
47
+
48
+ # Retrieves all the Groups in the pool.
49
+ def info()
50
+ super(DATASTORE_POOL_METHODS[:info])
51
+ end
52
+ end
53
+ end
@@ -69,7 +69,7 @@ module OpenNebula
69
69
  # Creates ACLs for the group. The ACL rules are described in a file
70
70
  def create_acls(filename = GROUP_DEFAULT)
71
71
  if !File.readable?(filename)
72
- return -1, "Can not read deafult ACL file for group"
72
+ return -1, "Cannot read deafult ACL file for group"
73
73
  end
74
74
 
75
75
  msg = String.new
@@ -79,14 +79,15 @@ module OpenNebula
79
79
  # Allocates a new Host in OpenNebula
80
80
  #
81
81
  # @param hostname [String] Name of the new Host.
82
- # @param im [String] Name of the im_driver
83
- # @param vmm [String] Name of the vmm_driver
84
- # @param tm [String] Name of the tm_driver
82
+ # @param im [String] Name of the im_driver (information/monitoring)
83
+ # @param vmm [String] Name of the vmm_driver (hypervisor)
84
+ # @param tm [String] Name of the vnm_driver (networking)
85
+ # @param cluster_id [String] Id of the cluster
85
86
  #
86
- # @return [Integer, OpenNebula::Error] the new VM ID in case of
87
+ # @return [Integer, OpenNebula::Error] the new ID in case of
87
88
  # success, error otherwise
88
- def allocate(hostname,im,vmm,vnm,tm)
89
- super(HOST_METHODS[:allocate],hostname,im,vmm,vnm,tm)
89
+ def allocate(hostname,im,vmm,vnm,cluster_id=ClusterPool::NONE_CLUSTER_ID)
90
+ super(HOST_METHODS[:allocate],hostname,im,vmm,vnm,cluster_id)
90
91
  end
91
92
 
92
93
  # Deletes the Host
@@ -91,9 +91,13 @@ module OpenNebula
91
91
 
92
92
  # Allocates a new Image in OpenNebula
93
93
  #
94
- # +description+ A string containing the template of the Image.
95
- def allocate(description)
96
- super(IMAGE_METHODS[:allocate],description)
94
+ # @param description [String] A string containing the template of the Image.
95
+ # @param ds_id [Integer] the target datastore ID
96
+ #
97
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
98
+ # otherwise
99
+ def allocate(description, ds_id)
100
+ super(IMAGE_METHODS[:allocate],description, ds_id)
97
101
  end
98
102
 
99
103
  # Replaces the template contents
@@ -178,6 +178,8 @@ module OpenNebula
178
178
  def update(xml_method, new_template)
179
179
  return Error.new('ID not defined') if !@pe_id
180
180
 
181
+ new_template ||= template_xml
182
+
181
183
  rc = @client.call(xml_method,@pe_id, new_template)
182
184
  rc = nil if !OpenNebula.is_error?(rc)
183
185
 
@@ -70,9 +70,12 @@ module OpenNebula
70
70
 
71
71
  # Allocates a new Template in OpenNebula
72
72
  #
73
- # +templatename+ A string containing the name of the Template.
74
- def allocate(templatename)
75
- super(TEMPLATE_METHODS[:allocate], templatename)
73
+ # @param description [String] The contents of the Template.
74
+ #
75
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
76
+ # otherwise
77
+ def allocate(description)
78
+ super(TEMPLATE_METHODS[:allocate], description)
76
79
  end
77
80
 
78
81
  # Deletes the Template
@@ -78,15 +78,19 @@ module OpenNebula
78
78
 
79
79
  # Allocates a new VirtualNetwork in OpenNebula
80
80
  #
81
- # +description+ A string containing the template of the VirtualNetwork.
82
- def allocate(description)
83
- super(VN_METHODS[:allocate],description)
81
+ # @param description [String] The template of the VirtualNetwork.
82
+ # @param cluster_id [Integer] Id of the cluster
83
+ #
84
+ # @return [Integer, OpenNebula::Error] the new ID in case of
85
+ # success, error otherwise
86
+ def allocate(description,cluster_id=ClusterPool::NONE_CLUSTER_ID)
87
+ super(VN_METHODS[:allocate], description, cluster_id)
84
88
  end
85
89
 
86
90
  # Replaces the template contents
87
91
  #
88
92
  # +new_template+ New template contents
89
- def update(new_template)
93
+ def update(new_template=nil)
90
94
  super(VN_METHODS[:update], new_template)
91
95
  end
92
96
 
@@ -158,9 +162,12 @@ module OpenNebula
158
162
  end
159
163
 
160
164
  # Changes the owner/group
161
- # uid:: _Integer_ the new owner id. Set to -1 to leave the current one
162
- # gid:: _Integer_ the new group id. Set to -1 to leave the current one
163
- # [return] nil in case of success or an Error object
165
+ #
166
+ # @param uid [Integer] the new owner id. Set to -1 to leave the current one
167
+ # @param gid [Integer] the new group id. Set to -1 to leave the current one
168
+ #
169
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
170
+ # otherwise
164
171
  def chown(uid, gid)
165
172
  super(VN_METHODS[:chown], uid, gid)
166
173
  end
@@ -101,6 +101,44 @@ module OpenNebula
101
101
  end
102
102
  end
103
103
 
104
+ def delete_element(xpath)
105
+ if NOKOGIRI
106
+ @xml.xpath(xpath.to_s).remove
107
+ else
108
+ @xml.delete_element(xpath.to_s)
109
+ end
110
+ end
111
+
112
+ def add_element(xpath, elems)
113
+ elems.each { |key, value|
114
+ if value.instance_of?(Hash)
115
+ if NOKOGIRI
116
+ elem = Nokogiri::XML::Node.new key, @xml.document
117
+ value.each { |k2, v2|
118
+ child = Nokogiri::XML::Node.new k2, elem
119
+ child.content = v2
120
+ elem.add_child(child)
121
+ }
122
+ @xml.xpath(xpath.to_s).first.add_child(elem)
123
+ else
124
+ elem = REXML::Element.new(key)
125
+ value.each { |k2, v2|
126
+ elem.add_element(k2).text = v2
127
+ }
128
+ @xml.elements[xpath].add_element(elem)
129
+ end
130
+ else
131
+ if NOKOGIRI
132
+ elem = Nokogiri::XML::Node.new key, @xml.document
133
+ elem.content = value
134
+ @xml.xpath(xpath.to_s).first.add_child(elem)
135
+ else
136
+ @xml.elements[xpath].add_element(key).text = value
137
+ end
138
+ end
139
+ }
140
+ end
141
+
104
142
  # Gets an array of text from elemenets extracted
105
143
  # using the XPATH expression passed as filter
106
144
  def retrieve_elements(filter)
@@ -187,6 +225,8 @@ module OpenNebula
187
225
  end
188
226
  end
189
227
 
228
+ # Returns wheter there are elements for a given XPath
229
+ # xpath_str:: _String_ XPath expression to locate the element
190
230
  def has_elements?(xpath_str)
191
231
  if NOKOGIRI
192
232
  element = @xml.xpath(xpath_str.to_s.upcase)
@@ -197,51 +237,69 @@ module OpenNebula
197
237
  end
198
238
  end
199
239
 
240
+ # Returns the <TEMPLATE> element in text form
241
+ # indent:: _Boolean_ indents the resulting string, default true
200
242
  def template_str(indent=true)
201
243
  template_like_str('TEMPLATE', indent)
202
244
  end
203
245
 
246
+ # Returns the <TEMPLATE> element in XML form
247
+ def template_xml
248
+ if NOKOGIRI
249
+ @xml.xpath('TEMPLATE').to_s
250
+ else
251
+ @xml.elements['TEMPLATE'].to_s
252
+ end
253
+ end
254
+
255
+ # Returns elements in text form
256
+ # root_element:: _String_ base element
257
+ # indent:: _Boolean_ indents the resulting string, default true
258
+ # xpath_exp:: _String_ filter elements with a XPath
204
259
  def template_like_str(root_element, indent=true, xpath_exp=nil)
205
260
  if NOKOGIRI
206
- xml_template=@xml.xpath(root_element).to_s
207
- rexml=REXML::Document.new(xml_template).root
261
+ xml_template = @xml.xpath(root_element).to_s
262
+ rexml = REXML::Document.new(xml_template).root
208
263
  else
209
- rexml=@xml.elements[root_element]
264
+ rexml = @xml.elements[root_element]
210
265
  end
211
266
 
212
267
  if indent
213
- ind_enter="\n"
214
- ind_tab=' '
268
+ ind_enter = "\n"
269
+ ind_tab = ' '
215
270
  else
216
- ind_enter=''
217
- ind_tab=' '
271
+ ind_enter = ''
272
+ ind_tab = ' '
218
273
  end
219
274
 
220
- str=rexml.elements.collect(xpath_exp) {|n|
221
- if n.class==REXML::Element
222
- str_line=""
223
- if n.has_elements?
224
- str_line << n.name << "=[" << ind_enter
225
-
226
- str_line << n.collect {|n2|
227
- if n2 && n2.class==REXML::Element
228
- str = ""
229
- str << ind_tab << n2.name << '='
230
- str << attr_to_str(n2.text) if n2.text
231
- str
232
- end
233
- }.compact.join(','+ind_enter)
234
- str_line<<" ]"
235
- else
236
- str_line << n.name << '=' << attr_to_str(n.text.to_s)
237
- end
238
- str_line
275
+ str = rexml.elements.collect(xpath_exp) {|n|
276
+ next if n.class != REXML::Element
277
+
278
+ str_line = ""
279
+
280
+ if n.has_elements?
281
+ str_line << "#{n.name}=[#{ind_enter}" << n.collect { |n2|
282
+
283
+ next if n2.class != REXML::Element or !n2.has_text?
284
+
285
+ str = "#{ind_tab}#{n2.name}=#{attr_to_str(n2.text)}"
286
+
287
+ }.compact.join(",#{ind_enter}") << " ]"
288
+ else
289
+ next if !n.has_text?
290
+
291
+ str_line << "#{n.name}=#{attr_to_str(n.text)}"
239
292
  end
293
+
294
+ str_line
240
295
  }.compact.join("\n")
241
296
 
242
- str
297
+ return str
243
298
  end
244
299
 
300
+ #
301
+ #
302
+ #
245
303
  def to_xml(pretty=false)
246
304
  if NOKOGIRI && pretty
247
305
  str = @xml.to_xml
@@ -259,6 +317,9 @@ module OpenNebula
259
317
  return str
260
318
  end
261
319
 
320
+ #
321
+ #
322
+ #
262
323
  def to_hash(hash={}, element=nil)
263
324
  element ||= @xml.document.root
264
325
 
@@ -298,13 +359,14 @@ module OpenNebula
298
359
  end
299
360
 
300
361
  private
362
+
363
+ #
364
+ #
365
+ #
301
366
  def attr_to_str(attr)
302
367
  attr.gsub!('"',"\\\"")
303
-
304
- if attr.match(/[=,' ']/)
305
- return '"' + attr + '"'
306
- end
307
-
368
+ attr = "\"#{attr}\""
369
+
308
370
  return attr
309
371
  end
310
372
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oca
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.1
4
+ version: 3.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -52,6 +52,10 @@ files:
52
52
  - lib/OpenNebula.rb
53
53
  - lib/OpenNebula/Acl.rb
54
54
  - lib/OpenNebula/AclPool.rb
55
+ - lib/OpenNebula/Cluster.rb
56
+ - lib/OpenNebula/ClusterPool.rb
57
+ - lib/OpenNebula/Datastore.rb
58
+ - lib/OpenNebula/DatastorePool.rb
55
59
  - lib/OpenNebula/Group.rb
56
60
  - lib/OpenNebula/GroupPool.rb
57
61
  - lib/OpenNebula/Host.rb