Revision e3b9eb63

View differences:

src/cloud/common/CloudServer.rb
31 31
    attr_reader :one_client
32 32

  
33 33
    # Initializes the Cloud server based on a config file
34
    # config_file:: _String_ for the server. MUST include the following 
34
    # config_file:: _String_ for the server. MUST include the following
35 35
    # variables:
36 36
    #   USER
37 37
    #   PASSWORD
......
43 43
        # --- Load the Cloud Server configuration file ---
44 44

  
45 45
        @config = Configuration.new(config_file)
46
        
46

  
47 47
        @instance_types = Hash.new
48 48

  
49 49
        if @config[:vm_type].kind_of?(Array)
......
55 55
        end
56 56

  
57 57
        # --- Start an OpenNebula Session ---
58
        
58

  
59 59
        @one_client = Client.new()
60 60
        @user_pool  = UserPool.new(@one_client)
61 61
    end
......
72 72
        puts "--------------------------------------"
73 73
        puts "      Registered Instance Types       "
74 74
        puts "--------------------------------------"
75
        pp @instance_types 
75
        pp @instance_types
76 76
    end
77 77

  
78 78
    ###########################################################################
......
80 80
    ###########################################################################
81 81

  
82 82
    # Generates an OpenNebula Session for the given user
83
    # user:: _Hash_ the user information  
84
    # [return] an OpenNebula client session 
83
    # user:: _Hash_ the user information
84
    # [return] an OpenNebula client session
85 85
    def one_client_user(user)
86 86
        client = Client.new("dummy:dummy")
87 87
        client.one_auth = "#{user[:name]}:#{user[:password]}"
88
    
88

  
89 89
        return client
90 90
    end
91 91

  
92 92
    # Authenticates a user
93 93
    # name:: _String_ of the user
94 94
    # password:: _String_ of the user
95
    # [return] true if authenticated    
95
    # [return] true if authenticated
96 96
    def authenticate?(name, password)
97 97
        user = get_user(name)
98 98

  
......
104 104
    # [return] _Hash_ with the user data
105 105
    def get_user(name)
106 106
        user = nil
107
    
107

  
108 108
        @user_pool.info
109 109
        @user_pool.each{ |u|
110 110
            if u.name==name
......
117 117
        }
118 118
        return user
119 119
   end
120
   
121
   def xml_to_hash(xml)
122
       begin
123
           hash = Crack::XML.parse(xml)
124
       rescue Exception => e
125
           error = OpenNebula::Error.new(e.message)
126
           return error
127
       end    
128
       
129
       return hash
130
   end
131
   
120

  
132 121
   def get_template_path(instance_type_name)
133
       if instance_type_name.nil?
134
           instance_type=@instance_types.first
135
       end
136
       
122

  
137 123
       instance_type=@instance_types[instance_type_name]
138
       
124

  
139 125
       if !instance_type
140 126
           error = OpenNebula::Error.new("Bad instance type")
141
           return error    
127
           return error
142 128
       end
143
       
144
       return @config[:template_location]+"/#{instance_type['TEMPLATE']}" 
129

  
130
       return @config[:template_location]+"/#{instance_type['TEMPLATE']}"
145 131
   end
146 132

  
147 133
    ###########################################################################
......
162 148
                error = OpenNebula::Error.new(error_msg)
163 149
                return error
164 150
            end
165
        
151

  
166 152
            if !File.exists?(file_path)
167 153
                error_msg = "Image file could not be found, aborting."
168 154
                error = OpenNebula::Error.new(error_msg)
169 155
                return error
170 156
            end
171 157
        end
172
            
158

  
173 159
        template = image.to_one_template
174 160

  
175 161
        rc = image.allocate(template)
......
181 167
        image.info
182 168
        template=image.to_hash
183 169
        template=template['IMAGE']['TEMPLATE']
184
        
170

  
185 171
        if file_path
186 172
            rc = image.copy(file_path, image['SOURCE'])
187 173
            file[:tempfile].unlink
188 174
        elsif template['SIZE'] and template['FSTYPE']
189 175
            rc = image.mk_datablock(
190
                    template['SIZE'], 
191
                    template['FSTYPE'], 
176
                    template['SIZE'],
177
                    template['FSTYPE'],
192 178
                    image['SOURCE'])
193 179
        end
194
        
180

  
195 181
        if OpenNebula.is_error?(rc)
196 182
           image.delete
197 183
           return rc
198 184
        end
199
 
185

  
200 186
        return nil
201 187
    end
202 188

  
src/cloud/occi/lib/OCCIServer.rb
35 35

  
36 36

  
37 37
##############################################################################
38
# The OCCI Server provides an OCCI implementation based on the 
38
# The OCCI Server provides an OCCI implementation based on the
39 39
# OpenNebula Engine
40 40
##############################################################################
41 41
class OCCIServer < CloudServer
42
    
42

  
43 43
    # Server initializer
44 44
    # config_file:: _String_ path of the config file
45
    # template:: _String_ path to the location of the templates 
45
    # template:: _String_ path to the location of the templates
46 46
    def initialize(config_file,template)
47 47
        super(config_file)
48
        
48

  
49 49
        @config.add_configuration_value("TEMPLATE_LOCATION",template)
50
        
50

  
51 51
        if @config[:ssl_server]
52 52
            @base_url=@config[:ssl_server]
53 53
        else
54 54
            @base_url="http://#{@config[:server]}:#{@config[:port]}"
55 55
        end
56
        
56

  
57 57
        print_configuration
58 58
    end
59
    
59

  
60 60
    # Authorization function
61 61
    # requestenv:: _Hash_ Hash containing the environment of the request
62
    # [return] _Boolean_ Whether the user is authorized or not  
62
    # [return] _Boolean_ Whether the user is authorized or not
63 63
    def authenticate?(requestenv)
64 64
      auth ||=  Rack::Auth::Basic::Request.new(requestenv)
65 65

  
66 66
      if !(auth.provided? && auth.basic? && auth.credentials)
67 67
          return false
68
      end 
68
      end
69 69

  
70 70
      user = get_user(requestenv, auth)
71 71

  
......
85 85
        auth =  Rack::Auth::Basic::Request.new(requestenv) if !auth
86 86
        super(auth.credentials.first)
87 87
    end
88
    
88

  
89 89
    # Retrieve a client with the user credentials
90 90
    # requestenv:: _Hash_ Hash containing the environment of the request
91 91
    # [return] _Client_ client with the user credentials
......
93 93
        user = get_user(requestenv)
94 94
        return one_client_user(user)
95 95
    end
96
    
96

  
97 97
    # Prepare the OCCI XML Response
98 98
    # resource:: _Pool_ or _PoolElement_ that represents a OCCI resource
99 99
    # [return] _String_,_Integer_ Resource Representation or error, status code
100 100
    def to_occi_xml(resource)
101 101
        xml_response = resource.to_occi(@base_url)
102 102
        return xml_response, 500 if OpenNebula.is_error?(xml_response)
103
        
104
        return xml_response, 201 
103

  
104
        return xml_response, 201
105 105
    end
106
    
106

  
107 107
    def get_info_hash(body)
108 108
        if body
109
            info = xml_to_hash(body.read)
109
            info = XMLUtilsElement::xml_to_hash(body.read)
110 110
            return info
111 111
        else
112 112
            error_msg = "OCCI XML representation not present"
......
114 114
            return error
115 115
        end
116 116
    end
117
    
117

  
118 118
    ###################################################
119 119
    # Pool Resources methods
120 120
    ###################################################
121
    
121

  
122 122
    # Post a new compute to the COMPUTE pool
123 123
    # request:: _Hash_ hash containing the data of the request
124
    # [return] _String_,_Integer_ COMPUTE Representation or error, status code    
124
    # [return] _String_,_Integer_ COMPUTE Representation or error, status code
125 125
    def post_compute(request)
126 126
        # --- Get client with user credentials ---
127 127
        client = get_client(request.env)
128
        
128

  
129 129
        # --- Check OCCI XML from POST ---
130 130
        vm_info = get_info_hash(request.body)
131 131
        return vm_info, 400 if OpenNebula.is_error?(vm_info)
......
134 134
        if vm_info['COMPUTE']
135 135
            path = get_template_path(vm_info['COMPUTE']['INSTANCE_TYPE'])
136 136
            return path, 500 if OpenNebula.is_error?(path)
137
            
137

  
138 138
            vm_info['TEMPLATE_PATH'] = path
139 139
        end
140 140

  
141 141
        # --- Create the new Instance ---
142 142
        vm = VirtualMachineOCCI.new(
143 143
                    vm_info,
144
                    VirtualMachine.build_xml, 
144
                    VirtualMachine.build_xml,
145 145
                    client)
146
        
146

  
147 147
        # --- Generate the template and Allocate the new Instance ---
148 148
        template = vm.to_one_template
149 149
        return template, 500 if OpenNebula.is_error?(template)
......
155 155
        vm.info
156 156
        return to_occi_xml(vm)
157 157
    end
158
    
158

  
159 159
    # Gets the pool representation of COMPUTES
160 160
    # request:: _Hash_ hash containing the data of the request
161 161
    # [return] _String_,_Integer_ Pool Representation or error, status code
......
164 164
        client = get_client(request.env)
165 165

  
166 166
        # --- Get User's VMs ---
167
        user_flag = -1  
167
        user_flag = -1
168 168
        vmpool = VirtualMachinePoolOCCI.new(client, user_flag)
169
        
169

  
170 170
        # --- Prepare XML Response ---
171
        rc = vmpool.info  
171
        rc = vmpool.info
172 172
        return rc, 404 if OpenNebula.is_error?(rc)
173
        
173

  
174 174
        return to_occi_xml(vmpool)
175 175
    end
176
    
176

  
177 177
    # Post a new network to the NETWORK pool
178 178
    # request:: _Hash_ hash containing the data of the request
179 179
    # [return] _String_,_Integer_ Network Representation or error, status code
180 180
    def post_network(request)
181 181
        # --- Get client with user credentials ---
182 182
        client = get_client(request.env)
183
        
184
        # --- Check OCCI XML from POST ---
185
        network_info = get_info_hash(request.body)
186
        return network_info, 400 if OpenNebula.is_error?(network_info)
187
        
188
        if network_info['NETWORK']
189
            network_info['NETWORK']['BRIDGE'] = @config[:bridge] 
190
        end
191
        
183

  
192 184
        # --- Create the new Instance ---
193 185
        network = VirtualNetworkOCCI.new(
194
                    network_info,
195 186
                    VirtualNetwork.build_xml,
196
                    client)
187
                    client,
188
                    request.body,
189
                    @config[:bridge])
197 190

  
198 191
        # --- Generate the template and Allocate the new Instance ---
199 192
        template = network.to_one_template
......
203 196
        return rc, 500 if OpenNebula.is_error?(rc)
204 197

  
205 198
        # --- Prepare XML Response ---
206
        network.info   
199
        network.info
207 200
        return to_occi_xml(network)
208 201
    end
209
    
202

  
210 203
    # Gets the pool representation of NETWORKS
211 204
    # request:: _Hash_ hash containing the data of the request
212
    # [return] _String_,_Integer_ Network pool representation or error, 
213
    # =>                          status code  
205
    # [return] _String_,_Integer_ Network pool representation or error,
206
    # =>                          status code
214 207
    def get_networks(request)
215 208
        # --- Get client with user credentials ---
216 209
        client = get_client(request.env)
217 210

  
218 211
        # --- Get User's VNETs ---
219
        user_flag = -1  
212
        user_flag = -1
220 213
        network_pool = VirtualNetworkPoolOCCI.new(client, user_flag)
221
        
222
        rc = network_pool.info  
214

  
215
        rc = network_pool.info
223 216
        return rc, 404 if OpenNebula.is_error?(rc)
224
        
217

  
225 218
        # --- Prepare XML Response ---
226 219
        return to_occi_xml(network_pool)
227 220
    end
228
    
221

  
229 222
    # Post a new image to the STORAGE pool
230 223
    # request:: _Hash_ hash containing the data of the request
231
    # [return] _String_,_Integer_ Image representation or error, status code 
224
    # [return] _String_,_Integer_ Image representation or error, status code
232 225
    def post_storage(request)
233 226
        # Get client with user credentials
234 227
        client = get_client(request.env)
235
        
228

  
236 229
        # --- Check OCCI XML from POST ---
237 230
        if request.params['occixml']
238
            image_info = xml_to_hash(request.params['occixml'])
231
            image_info = XMLUtilsElement::xml_to_hash(request.params['occixml'])
239 232
            return image_info, 400 if OpenNebula.is_error?(image_info)
240 233
        else
241 234
            error_msg = "OCCI XML representation of Image" +
......
243 236
            error = OpenNebula::Error.new(error_msg)
244 237
            return error, 400
245 238
        end
246
 
239

  
247 240
        # --- Create and Add the new Image ---
248 241
        image = ImageOCCI.new(image_info, Image.build_xml, client)
249 242

  
250 243
        rc = add_image(image, request.params['file'])
251 244
        return rc, 500 if OpenNebula.is_error?(rc)
252
        
245

  
253 246
        # --- Enable the new Image ---
254 247
        rc = image.enable
255 248
        return rc, 500 if OpenNebula.is_error?(rc)
256
        
249

  
257 250
        # --- Prepare XML Response ---
258 251
        return to_occi_xml(image)
259 252
    end
260
    
253

  
261 254
    # Gets the pool representation of STORAGES
262 255
    # request:: _Hash_ hash containing the data of the request
263
    # [return] _String_,_Integer_ Image pool representation or error, 
264
    #                             status code 
256
    # [return] _String_,_Integer_ Image pool representation or error,
257
    #                             status code
265 258
    def get_storages(request)
266 259
        # --- Get client with user credentials ---
267 260
        client = get_client(request.env)
268 261

  
269 262
        # --- Get User's Images ---
270
        user_flag = -1  
263
        user_flag = -1
271 264
        image_pool = ImagePoolOCCI.new(client, user_flag)
272
        
273
        result = image_pool.info  
265

  
266
        result = image_pool.info
274 267
        return result, 404 if OpenNebula.is_error?(result)
275
        
268

  
276 269
        # --- Prepare XML Response ---
277 270
        return to_occi_xml(image_pool)
278 271
    end
279
    
272

  
280 273
    ###################################################
281 274
    # Entity Resources methods
282 275
    ###################################################
283
    
276

  
284 277
    # Get the representation of a COMPUTE resource
285 278
    # request:: _Hash_ hash containing the data of the request
286
    # [return] _String_,_Integer_ COMPUTE representation or error, 
279
    # [return] _String_,_Integer_ COMPUTE representation or error,
287 280
    #                             status code
288 281
    def get_compute(request, params)
289 282
        # --- Get client with user credentials ---
290 283
        client = get_client(request.env)
291 284

  
292
        # --- Get the VM ---        
285
        # --- Get the VM ---
293 286
        vm = VirtualMachineOCCI.new(
294 287
                nil,
295 288
                VirtualMachine.build_xml(params[:id]),
......
301 294
        # --- Prepare XML Response ---
302 295
        return to_occi_xml(vm)
303 296
    end
304
    
297

  
305 298
    # Deletes a COMPUTE resource
306 299
    # request:: _Hash_ hash containing the data of the request
307
    # [return] _String_,_Integer_ Delete confirmation msg or error, 
300
    # [return] _String_,_Integer_ Delete confirmation msg or error,
308 301
    #                             status code
309 302
    def delete_compute(request, params)
310 303
        # --- Get client with user credentials ---
311 304
        client = get_client(request.env)
312
        
305

  
313 306
        vm = VirtualMachineOCCI.new(
314 307
                nil,
315 308
                VirtualMachine.build_xml(params[:id]),
316 309
                client)
317
                      
310

  
318 311
        # --- Finalize the VM ---
319 312
        result = vm.finalize
320 313
        return result, 500 if OpenNebula::is_error?(result)
321 314

  
322 315
        return "", 204
323 316
    end
324
    
317

  
325 318
    # Updates a COMPUTE resource
326 319
    # request:: _Hash_ hash containing the data of the request
327
    # [return] _String_,_Integer_ Update confirmation msg or error, 
328
    #                             status code  
320
    # [return] _String_,_Integer_ Update confirmation msg or error,
321
    #                             status code
329 322
    def put_compute(request, params)
330 323
        # --- Get client with user credentials ---
331 324
        client = get_client(request.env)
......
338 331
        if vm_info['COMPUTE'] && vm_info['COMPUTE']['STATE']
339 332
            vm = VirtualMachineOCCI.new(
340 333
                    vm_info,
341
                    VirtualMachine.build_xml(params[:id]), 
334
                    VirtualMachine.build_xml(params[:id]),
342 335
                    client)
343
                    
336

  
344 337
            rc = vm.mk_action(vm_info['COMPUTE']['STATE'])
345 338
            return rc, 400 if OpenNebula.is_error?(rc)
346 339
        else
347
            error_msg = "State not defined in the OCCI XML" 
340
            error_msg = "State not defined in the OCCI XML"
348 341
            error = OpenNebula::Error.new(error_msg)
349
            return error, 400            
342
            return error, 400
350 343
        end
351
        
344

  
352 345
        # --- Prepare XML Response ---
353
        vm.info   
346
        vm.info
354 347
        return to_occi_xml(vm)
355 348
    end
356
    
349

  
357 350
    # Retrieves a NETWORK resource
358 351
    # request:: _Hash_ hash containing the data of the request
359
    # [return] _String_,_Integer_ NETWORK occi representation or error, 
360
    #                             status code 
352
    # [return] _String_,_Integer_ NETWORK occi representation or error,
353
    #                             status code
361 354
    def get_network(request, params)
362 355
        # --- Get client with user credentials ---
363 356
        client = get_client(request.env)
364 357

  
365
        # --- Get the VM ---        
358
        # --- Get the VM ---
366 359
        network = VirtualNetworkOCCI.new(
367
                nil,
368 360
                VirtualNetwork.build_xml(params[:id]),
369 361
                client)
370 362

  
......
374 366
        # --- Prepare XML Response ---
375 367
        return to_occi_xml(network)
376 368
    end
377
    
369

  
378 370
    # Deletes a NETWORK resource
379 371
    # request:: _Hash_ hash containing the data of the request
380
    # [return] _String_,_Integer_ Delete confirmation msg or error, 
381
    #                             status code 
372
    # [return] _String_,_Integer_ Delete confirmation msg or error,
373
    #                             status code
382 374
    def delete_network(request, params)
383 375
        # --- Get client with user credentials ---
384 376
        client = get_client(request.env)
385
        
377

  
386 378
        vn = VirtualNetworkOCCI.new(
387
                nil,
388 379
                VirtualNetwork.build_xml(params[:id]),
389 380
                client)
390
                      
381

  
391 382
        # --- Delete the VNET ---
392 383
        result = vn.delete
393 384
        return result, 500 if OpenNebula::is_error?(result)
394 385

  
395 386
        return "", 204
396 387
    end
397
    
388

  
398 389
    # Get a STORAGE resource
399 390
    # request:: _Hash_ hash containing the data of the request
400
    # [return] _String_,_Integer_ STORAGE occi representation or error, 
401
    #                             status code 
391
    # [return] _String_,_Integer_ STORAGE occi representation or error,
392
    #                             status code
402 393
    def get_storage(request, params)
403 394
        # --- Get client with user credentials ---
404 395
        client = get_client(request.env)
405 396

  
406
        # --- Get the Image ---        
397
        # --- Get the Image ---
407 398
        image = ImageOCCI.new(nil, Image.build_xml(params[:id]), client)
408 399

  
409 400
        result = image.info
......
412 403
        # --- Prepare XML Response ---
413 404
        return to_occi_xml(image)
414 405
    end
415
    
406

  
416 407
    # Deletes a STORAGE resource (Not yet implemented)
417 408
    # request:: _Hash_ hash containing the data of the request
418
    # [return] _String_,_Integer_ Delete confirmation msg or error, 
419
    #                             status code 
409
    # [return] _String_,_Integer_ Delete confirmation msg or error,
410
    #                             status code
420 411
    def delete_storage(request, params)
421 412
        # --- Get client with user credentials ---
422 413
        client = get_client(request.env)
423
        
414

  
424 415
        image = ImageOCCI.new(nil, Image.build_xml(params[:id]), client)
425
                      
416

  
426 417
        # --- Delete the Image ---
427 418
        result = image.delete
428 419
        return result, 500 if OpenNebula::is_error?(result)
src/cloud/occi/lib/VirtualNetworkOCCI.rb
23 23
        <NETWORK href="<%= base_url %>/network/<%= self.id.to_s  %>">
24 24
            <ID><%= self.id.to_s %></ID>
25 25
            <NAME><%= self.name %></NAME>
26
            <ADDRESS><%= template['NETWORK_ADDRESS'] %></ADDRESS>
27
            <% if template['NETWORK_SIZE'] %>
28
            <SIZE><%= template['NETWORK_SIZE'] %></SIZE>
26
            <ADDRESS><%= self['TEMPLATE/NETWORK_ADDRESS'] %></ADDRESS>
27
            <% if self['TEMPLATE/NETWORK_SIZE'] %>
28
            <SIZE><%= self['TEMPLATE/NETWORK_SIZE'] %></SIZE>
29 29
            <% end %>
30 30
        </NETWORK>
31 31
    }
32 32

  
33 33
    ONE_NETWORK = %q{
34
        NAME            = <%= vnet_info['NAME'] %>
34
        NAME            = <%= @vnet_info.elements['NAME'].text %>
35 35
        TYPE            = RANGED
36
        BRIDGE          = <%= vnet_info['BRIDGE'] %>
37
        NETWORK_ADDRESS = <%= vnet_info['ADDRESS'] %>
38
        NETWORK_SIZE    = <%= vnet_info['SIZE'] %>
36
        BRIDGE          = <%= @vnet_info.elements['BRIDGE'].text %>
37
        NETWORK_ADDRESS = <%= @vnet_info.elements['ADDRESS'].text %>
38
        NETWORK_SIZE    = <%= @vnet_info.elements['SIZE'].text %>
39 39
    }.gsub(/^        /, '')
40 40

  
41 41
    # Class constructor
42
    def initialize(vnet_info, xml, client)
42
    def initialize(xml, client, xml_info=nil, bridge=nil)
43 43
        super(xml, client)
44 44

  
45
        @vnet_info = vnet_info
45
        if xml_info != nil and bridge != nil
46
            @vnet_info = REXML::Document.new(xml_info).root
47

  
48
            bridge_element = REXML::Element.new("BRIDGE")
49
            bridge_element.add_text(bridge)
50

  
51
            @vnet_info.add(bridge_element)
52
        else
53
            @vnet_info = nil
54
        end
46 55
    end
47
    
48
    # Creates the OCCI representation of a Virtual Network
49
    def to_occi(base_url)        
50
        vn_hash = self.to_hash
51
        return vn_hash, 500 if OpenNebula.is_error?(vn_hash)
52
        
53
        template = vn_hash['VNET']['TEMPLATE']
54 56

  
57
    # Creates the OCCI representation of a Virtual Network
58
    def to_occi(base_url)
55 59
        begin
56 60
            occi = ERB.new(OCCI_NETWORK)
57
            occi_text = occi.result(binding) 
61
            occi_text = occi.result(binding)
58 62
        rescue Exception => e
59 63
            error = OpenNebula::Error.new(e.message)
60 64
            return error
61
        end    
65
        end
62 66

  
63 67
        return occi_text.gsub(/\n\s*/,'')
64 68
    end
65
    
69

  
66 70
    def to_one_template()
67
        if @vnet_info['NETWORK']
68
            vnet_info = @vnet_info['NETWORK']
69
        else            
70
            error_msg = "Missing STORAGE section in the XML body"
71
        if @vnet_info.name != 'NETWORK'
72
            error_msg = "Missing NETWORK section in the XML body"
71 73
            error = OpenNebula::Error.new(error_msg)
72 74
            return error
73 75
        end
74
            
76

  
75 77
        one = ERB.new(ONE_NETWORK)
76 78
        return one.result(binding)
77 79
    end
src/oca/ruby/OpenNebula/XMLUtils.rb
7 7
    rescue LoadError
8 8
        NOKOGIRI=false
9 9
    end
10
    
10

  
11 11
    # Require crack library if present, otherwise don't bother
12 12
    # This is just for OCCI use
13 13
    begin
......
17 17

  
18 18
    ###########################################################################
19 19
    # The XMLUtilsElement module provides an abstraction of the underlying
20
    # XML parser engine. It provides XML-related methods for the Pool Elements  
20
    # XML parser engine. It provides XML-related methods for the Pool Elements
21 21
    ###########################################################################
22 22
    module XMLUtilsElement
23 23
        # Initialize a XML document for the element
......
34 34

  
35 35
        # Extract an element from the XML description of the PoolElement.
36 36
        # key::_String_ The name of the element
37
        # [return] _String_ the value of the element 
37
        # [return] _String_ the value of the element
38 38
        # Examples:
39 39
        #   ['VID'] # gets VM id
40 40
        #   ['HISTORY/HOSTNAME'] # get the hostname from the history
......
47 47
            else
48 48
                element=@xml.elements[key.to_s.upcase]
49 49
            end
50
            
51
            if element 
50

  
51
            if element
52 52
                element.text
53 53
            end
54 54
        end
55
        
55

  
56 56
        def template_str(indent=true)
57 57
            template_like_str('TEMPLATE', indent)
58 58
        end
59
            
59

  
60 60
        def template_like_str(root_element, indent=true)
61 61
            if NOKOGIRI
62 62
                xml_template=@xml.xpath(root_element).to_s
......
64 64
            else
65 65
                rexml=@xml.elements[root_element]
66 66
            end
67
            
67

  
68 68
            if indent
69 69
                ind_enter="\n"
70 70
                ind_tab='  '
......
72 72
                ind_enter=''
73 73
                ind_tab=' '
74 74
            end
75
            
75

  
76 76
            str=rexml.collect {|n|
77 77
                if n.class==REXML::Element
78 78
                    str_line=""
......
93 93
                    str_line
94 94
                end
95 95
            }.compact.join("\n")
96
            
96

  
97 97
            str
98 98
        end
99
        
100
        def to_hash 
99

  
100
        def to_hash
101 101
            if !@hash && @xml
102 102
                begin
103 103
                   @hash = Crack::XML.parse(to_xml)
......
116 116
                str = ""
117 117
                if pretty
118 118
                    REXML::Formatters::Pretty.new(1).write(@xml,str)
119
                else 
119
                else
120 120
                    REXML::Formatters::Default.new.write(@xml,str)
121 121
                end
122 122
                str
123 123
            end
124 124
        end
125
        
125

  
126
        def XMLUtilsElement.xml_to_hash(xml)
127
            begin
128
                hash = Crack::XML.parse(xml)
129
            rescue Exception => e
130
                error = OpenNebula::Error.new(e.message)
131
                return error
132
            end
133

  
134
            return hash
135
        end
136

  
126 137
    end
127
        
138

  
128 139
    ###########################################################################
129 140
    # The XMLUtilsPool module provides an abstraction of the underlying
130
    # XML parser engine. It provides XML-related methods for the Pools  
141
    # XML parser engine. It provides XML-related methods for the Pools
131 142
    ###########################################################################
132 143
    module XMLUtilsPool
133 144

  
......
141 152
                xml=REXML::Document.new(xml).root
142 153
            end
143 154
        end
144
        
155

  
145 156
        #Executes the given block for each element of the Pool
146
        #block:: _Block_ 
157
        #block:: _Block_
147 158
        def each_element(block)
148 159
            if NOKOGIRI
149 160
                @xml.xpath(
......
165 176
                str = ""
166 177
                if pretty
167 178
                    REXML::Formatters::Pretty.new(1).write(@xml,str)
168
                else 
179
                else
169 180
                    REXML::Formatters::Default.new.write(@xml,str)
170 181
                end
171 182
                str
172 183
            end
173 184
        end
174
        
175
        def to_hash 
185

  
186
        def to_hash
176 187
            if !@hash && @xml
177 188
                @hash=Crack::XML.parse(to_xml)
178 189
            end

Also available in: Unified diff