Skip to content

Instantly share code, notes, and snippets.

@np422
Created August 24, 2016 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save np422/953bf3b3fa88b2223102646002d10793 to your computer and use it in GitHub Desktop.
Save np422/953bf3b3fa88b2223102646002d10793 to your computer and use it in GitHub Desktop.
Small script to parse xml-output from vcloud api
#!/usr/bin/env ruby
require "json"
require "crack"
myXML = Crack::XML.parse(File.read( ARGV[0] ))
myJSON = myXML.to_json
vAPP = JSON.parse(myJSON)
def print_ovf_info(m)
m["ovf:Item"].each do |item|
if item["rasd:Description"] == "Hard disk"
puts item["rasd:ElementName"]
puts item["rasd:HostResource"]["vcloud:capacity"]
end
if item["rasd:Description"] == "Number of Virtual CPUs"
puts item["rasd:ElementName"]
end
if item["rasd:Description"] == "Memory Size"
puts item["rasd:ElementName"]
end
end
end
vAPP["VApp"]["Children"]["Vm"].each do |machine|
if machine.is_a? Array
machine.each do |m|
if m.is_a? Hash
if !m.keys.grep("ovf:System").empty?
puts "System name = " + m["ovf:System"]["vssd:VirtualSystemIdentifier"]
print_ovf_info(m)
puts ","
end
end
end
else
puts "System name = " + machine["GuestCustomizationSection"]["ComputerName"] if
print_ovf_info(machine["ovf:VirtualHardwareSection"])
puts ","
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment