Revision e3b9eb63 src/cloud/occi/lib/OCCIServer.rb

View differences:

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)

Also available in: Unified diff