1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-10 00:58:17 +03:00

feature #2917. Add vm information

This commit is contained in:
Ruben S. Montero 2014-07-13 13:17:45 +02:00
parent 9515b8c9db
commit cb11d68b04

View File

@ -131,6 +131,7 @@ get '/vm/:id' do
halt 401, "Not authorized" if client.nil? halt 401, "Not authorized" if client.nil?
vm_id = params[:id].to_i vm_id = params[:id].to_i
vm = VirtualMachine.new_with_id(vm_id, client) vm = VirtualMachine.new_with_id(vm_id, client)
rc = vm.info rc = vm.info
@ -139,73 +140,83 @@ get '/vm/:id' do
halt 404, rc.message halt 404, rc.message
end end
service_id = vm['USER_TEMPLATE/SERVICE_ID'] # Build the vm information
vm_hash = vm.to_hash
if service_id.nil? || !service_id.match(/^\d+$/) response = {
error_msg = "SERVICE_ID invalid or empty" "vm" => {
logger.error {"VMID:#{vm_id} vm.info error: #{error_msg}"} "name" => vm["NAME"],
halt 400, error_msg "user_template" => vm_hash["VM"]["USER_TEMPLATE"]
}
}
# Build the flow information
flow_id = vm['USER_TEMPLATE/SERVICE_ID']
return [200, response.to_json] if flow_id.nil? || !flow_id.match(/^\d+$/)
flow = $flow_client.get("#{RESOURCE_PATH}/#{flow_id}")
if CloudClient::is_error?(flow)
logger.error { "VMID:#{vm_id}, FID:#{flow_id} error: " \
"Service not found: #{flow.message}" }
return [200, response.to_json]
end end
response = $flow_client.get("#{RESOURCE_PATH}/#{service_id}") flow_hash = JSON.parse(flow.body)
roles = flow_hash["DOCUMENT"]["TEMPLATE"]["BODY"]["roles"]
if CloudClient::is_error?(response) return [200, response.to_json] if roles.nil?
error_msg = "Service not found: " + response.message
logger.error {"VMID:#{vm_id} vm.info error: #{error_msg}"}
halt 404, error_msg
end
flow_hash = JSON.parse(response.body) # Build the flow_info hash
roles = flow_hash["DOCUMENT"]["TEMPLATE"]["BODY"]["roles"] flow_info = {}
# Build the flow_info hash while checking that the user has not spoofed the
# Service_ID
flow_info = {}
flow_vm_ids = [] flow_vm_ids = []
begin begin
if roles roles.each do |role|
roles.each do |role| if (nodes = role["nodes"])
if (nodes = role["nodes"])
role_name = role["name"]
role_info = {} role_name = role["name"]
nodes.each do |vm| role_info = {}
vm_deploy_id = vm["deploy_id"]
flow_vm_ids << vm_deploy_id nodes.each do |vm|
vm_deploy_id = vm["deploy_id"]
vm_info = vm["vm_info"]["VM"]
vm_info = vm["vm_info"]["VM"] flow_vm_ids << vm_deploy_id
node_info = {
"name" => vm_info["NAME"], node_info = {
"user_template" => vm_info["USER_TEMPLATE"], "name" => vm_info["NAME"],
"user_template" => vm_info["USER_TEMPLATE"],
}
node_info["nic"] = []
[vm_info["TEMPLATE"]["NIC"]].flatten.each do |nic|
node_info["nic"] << {
"ip" => nic["IP"],
"network" => nic["NETWORK"]
} }
node_info["nic"] = []
[vm_info["TEMPLATE"]["NIC"]].flatten.each do |nic|
node_info["nic"] << {
"ip" => nic["IP"],
"network" => nic["NETWORK"]
}
end
role_info[vm_deploy_id] = node_info
end end
flow_info[role_name] = role_info role_info[vm_deploy_id] = node_info
end end
flow_info[role_name] = role_info
end end
end end
rescue Exception => e rescue Exception => e
error_msg = "Could not parse Flow: #{e.message}" logger.error { "VMID:#{vm_id}, FID:#{flow_id} error: " \
logger.error {"VMID:#{vm_id} vm.info error: #{error_msg}"} "Could not parse Flow: #{e.message}" }
halt 500, error_msg
return [200, response.to_json]
end end
if flow_vm_ids.empty? || !flow_vm_ids.include?(vm_id) # Add flow information if the user has not spoofed the Service_ID
error_msg = "Service ID does not correspond the VM" if !flow_vm_ids.empty? && flow_vm_ids.include?(vm_id)
logger.error {"VMID:#{vm_id} vm.info error: #{error_msg}"} response["flow"] = flow_info
halt 400, error_msg
end end
[200, flow_info.to_json] [200, response.to_json]
end end