diff --git a/src/cloud/occi/lib/ImageOCCI.rb b/src/cloud/occi/lib/ImageOCCI.rb
index acee97b1eb..175743d8b9 100755
--- a/src/cloud/occi/lib/ImageOCCI.rb
+++ b/src/cloud/occi/lib/ImageOCCI.rb
@@ -20,7 +20,7 @@ include OpenNebula
class ImageOCCI < Image
OCCI_IMAGE = %q{
-
+
<%= self.id.to_s %>
<%= self.name %>
<% if template['TYPE'] %>
@@ -32,7 +32,7 @@ class ImageOCCI < Image
<% if size %>
<%= size %>
<% end %>
-
+
}
@@ -60,7 +60,7 @@ class ImageOCCI < Image
end
# Creates the OCCI representation of an Image
- def to_occi()
+ def to_occi(base_url)
image_hash = self.to_hash
return image_hash, 500 if OpenNebula.is_error?(image_hash)
diff --git a/src/cloud/occi/lib/ImagePoolOCCI.rb b/src/cloud/occi/lib/ImagePoolOCCI.rb
index 8087249db4..32b85351fe 100755
--- a/src/cloud/occi/lib/ImagePoolOCCI.rb
+++ b/src/cloud/occi/lib/ImagePoolOCCI.rb
@@ -29,7 +29,8 @@ class ImagePoolOCCI < ImagePool
# Creates the OCCI representation of a Virtual Machine Pool
- def to_occi(base_url)begin
+ def to_occi(base_url)
+ begin
occi = ERB.new(OCCI_IMAGE_POOL)
occi_text = occi.result(binding)
rescue Exception => e
diff --git a/src/cloud/occi/lib/OCCIServer.rb b/src/cloud/occi/lib/OCCIServer.rb
index 8b10e079fc..36e813b76d 100755
--- a/src/cloud/occi/lib/OCCIServer.rb
+++ b/src/cloud/occi/lib/OCCIServer.rb
@@ -94,6 +94,27 @@ class OCCIServer < CloudServer
return one_client_user(user)
end
+ # Prepare the OCCI XML Response
+ # resource:: _Pool_ or _PoolElement_ that represents a OCCI resource
+ # [return] _String_,_Integer_ Resource Representation or error, status code
+ def to_occi_xml(resource)
+ xml_response = resource.to_occi(@base_url)
+ return xml_response, 500 if OpenNebula.is_error?(xml_response)
+
+ return xml_response, 201
+ end
+
+ def get_info_hash(body)
+ if body
+ info = xml_to_hash(body.read)
+ return info
+ else
+ error_msg = "OCCI XML representation not present"
+ error = OpenNebula::Error.new(error_msg)
+ return error
+ end
+ end
+
###################################################
# Pool Resources methods
###################################################
@@ -106,23 +127,16 @@ class OCCIServer < CloudServer
client = get_client(request.env)
# --- Check OCCI XML from POST ---
- if request.body
- vm_info = xml_to_hash(request.body.read)
- return vm_info, 400 if OpenNebula.is_error?(vm_info)
- else
- error_msg = "OCCI XML representation of VM not present"
- error = OpenNebula::Error.new(error_msg)
- return error, 400
- end
+ vm_info = get_info_hash(request.body)
+ return vm_info, 400 if OpenNebula.is_error?(vm_info)
# --- Get Template Path ---
if vm_info['COMPUTE']
- template_path = get_template_path(
- vm_info['COMPUTE']['INSTANCE_TYPE'])
+ path = get_template_path(vm_info['COMPUTE']['INSTANCE_TYPE'])
+ return path, 500 if OpenNebula.is_error?(path)
+
+ vm_info['TEMPLATE_PATH'] = path
end
- return template_path, 500 if OpenNebula.is_error?(template_path)
-
- vm_info['TEMPLATE_PATH'] = template_path
# --- Create the new Instance ---
vm = VirtualMachineOCCI.new(
@@ -138,11 +152,8 @@ class OCCIServer < CloudServer
return rc, 500 if OpenNebula.is_error?(rc)
# --- Prepare XML Response ---
- vm.info
- xml_response = vm.to_occi(@base_url)
- return xml_response, 500 if OpenNebula.is_error?(xml_response)
-
- return xml_response, 201
+ vm.info
+ return to_occi_xml(vm)
end
# Gets the pool representation of COMPUTES
@@ -156,14 +167,11 @@ class OCCIServer < CloudServer
user_flag = -1
vmpool = VirtualMachinePoolOCCI.new(client, user_flag)
+ # --- Prepare XML Response ---
rc = vmpool.info
return rc, 404 if OpenNebula.is_error?(rc)
- # --- Prepare XML Response ---
- xml_response = vmpool.to_occi(@base_url)
- return xml_response, 500 if OpenNebula.is_error?(xml_response)
-
- return xml_response, 201
+ return to_occi_xml(vmpool)
end
# Post a new network to the NETWORK pool
@@ -174,14 +182,8 @@ class OCCIServer < CloudServer
client = get_client(request.env)
# --- Check OCCI XML from POST ---
- if request.body
- network_info = xml_to_hash(request.body.read)
- return network_info, 400 if OpenNebula.is_error?(network_info)
- else
- error_msg = "OCCI XML representation of VNET not present"
- error = OpenNebula::Error.new(error_msg)
- return error, 400
- end
+ network_info = get_info_hash(request.body)
+ return network_info, 400 if OpenNebula.is_error?(network_info)
if network_info['NETWORK']
network_info['NETWORK']['BRIDGE'] = @config[:bridge]
@@ -202,10 +204,7 @@ class OCCIServer < CloudServer
# --- Prepare XML Response ---
network.info
- xml_response = network.to_occi
- return xml_response, 500 if OpenNebula.is_error?(xml_response)
-
- return xml_response, 201
+ return to_occi_xml(network)
end
# Gets the pool representation of NETWORKS
@@ -224,10 +223,7 @@ class OCCIServer < CloudServer
return rc, 404 if OpenNebula.is_error?(rc)
# --- Prepare XML Response ---
- xml_response = network_pool.to_occi(@base_url)
- return xml_response, 500 if OpenNebula.is_error?(xml_response)
-
- return xml_response, 201
+ return to_occi_xml(network_pool)
end
# Post a new image to the STORAGE pool
@@ -258,7 +254,8 @@ class OCCIServer < CloudServer
rc = image.enable
return rc, 500 if OpenNebula.is_error?(rc)
- return image.to_occi, 201
+ # --- Prepare XML Response ---
+ return to_occi_xml(image)
end
# Gets the pool representation of STORAGES
@@ -277,10 +274,7 @@ class OCCIServer < CloudServer
return result, 404 if OpenNebula.is_error?(result)
# --- Prepare XML Response ---
- xml_response = image_pool.to_occi(@base_url)
- return xml_response, 500 if OpenNebula.is_error?(xml_response)
-
- return xml_response, 201
+ return to_occi_xml(image_pool)
end
###################################################
@@ -305,10 +299,7 @@ class OCCIServer < CloudServer
return result, 404 if OpenNebula::is_error?(result)
# --- Prepare XML Response ---
- xml_response = vm.to_occi(@base_url)
- return xml_response, 500 if OpenNebula.is_error?(xml_response)
-
- return xml_response, 201
+ return to_occi_xml(vm)
end
# Deletes a COMPUTE resource
@@ -326,7 +317,6 @@ class OCCIServer < CloudServer
# --- Finalize the VM ---
result = vm.finalize
- pp result
return result, 500 if OpenNebula::is_error?(result)
return "", 204
@@ -341,14 +331,8 @@ class OCCIServer < CloudServer
client = get_client(request.env)
# --- Check OCCI XML from POST ---
- if request.body
- vm_info = xml_to_hash(request.body.read)
- return vm_info, 400 if OpenNebula.is_error?(vm_info)
- else
- error_msg = "OCCI XML representation of VM not present"
- error = OpenNebula::Error.new(error_msg)
- return error, 400
- end
+ vm_info = get_info_hash(request.body)
+ return vm_info, 400 if OpenNebula.is_error?(vm_info)
# --- Get the VM and Action on it ---
if vm_info['COMPUTE'] && vm_info['COMPUTE']['STATE']
@@ -367,10 +351,7 @@ class OCCIServer < CloudServer
# --- Prepare XML Response ---
vm.info
- xml_response = vm.to_occi(@base_url)
- return xml_response, 500 if OpenNebula.is_error?(xml_response)
-
- return xml_response, 202
+ return to_occi_xml(vm)
end
# Retrieves a NETWORK resource
@@ -382,19 +363,16 @@ class OCCIServer < CloudServer
client = get_client(request.env)
# --- Get the VM ---
- vn = VirtualNetworkOCCI.new(
+ network = VirtualNetworkOCCI.new(
nil,
VirtualNetwork.build_xml(params[:id]),
client)
- result = vn.info
+ result = network.info
return result, 404 if OpenNebula::is_error?(result)
# --- Prepare XML Response ---
- xml_response = vn.to_occi
- return xml_response, 500 if OpenNebula.is_error?(xml_response)
-
- return xml_response, 200
+ return to_occi_xml(network)
end
# Deletes a NETWORK resource
@@ -432,10 +410,7 @@ class OCCIServer < CloudServer
return result, 404 if OpenNebula::is_error?(result)
# --- Prepare XML Response ---
- xml_response = image.to_occi
- return xml_response, 500 if OpenNebula.is_error?(xml_response)
-
- return xml_response, 200
+ return to_occi_xml(image)
end
# Deletes a STORAGE resource (Not yet implemented)
diff --git a/src/cloud/occi/lib/VirtualMachineOCCI.rb b/src/cloud/occi/lib/VirtualMachineOCCI.rb
index 379d8a4424..4fb2e2e119 100755
--- a/src/cloud/occi/lib/VirtualMachineOCCI.rb
+++ b/src/cloud/occi/lib/VirtualMachineOCCI.rb
@@ -20,7 +20,7 @@ include OpenNebula
class VirtualMachineOCCI < VirtualMachine
OCCI_VM = %q{
-
+
<%= self.id.to_s%>
<%= self.name%>
<% if template['INSTANCE_TYPE'] %>
diff --git a/src/cloud/occi/lib/VirtualNetworkOCCI.rb b/src/cloud/occi/lib/VirtualNetworkOCCI.rb
index b2a46e415b..1ab7baa1a5 100755
--- a/src/cloud/occi/lib/VirtualNetworkOCCI.rb
+++ b/src/cloud/occi/lib/VirtualNetworkOCCI.rb
@@ -20,7 +20,7 @@ include OpenNebula
class VirtualNetworkOCCI < VirtualNetwork
OCCI_NETWORK = %q{
-
+
<%= self.id.to_s %>
<%= self.name %>
<%= template['NETWORK_ADDRESS'] %>
@@ -46,7 +46,7 @@ class VirtualNetworkOCCI < VirtualNetwork
end
# Creates the OCCI representation of a Virtual Network
- def to_occi()
+ def to_occi(base_url)
vn_hash = self.to_hash
return vn_hash, 500 if OpenNebula.is_error?(vn_hash)