Skip to content

Instantly share code, notes, and snippets.

@Cinderhaze
Created May 7, 2015 12:33
Show Gist options
  • Save Cinderhaze/36039d8521c22e5965ff to your computer and use it in GitHub Desktop.
Save Cinderhaze/36039d8521c22e5965ff to your computer and use it in GitHub Desktop.
My first 'successful' attempt at getting the info I need
# JSON Parsing example
require "rubygems"
require "json"
string = '{"desc":{"someKey":"someValue","anotherKey":"value"},"main_item":{"stats":{"a":8,"b":12,"c":10}}}'
parsed = JSON.parse(string) # returns a hash
p parsed["desc"]["someKey"]
p parsed["main_item"]["stats"]["a"]
# Read JSON from a file, iterate over objects
file = open("ec2s.json")
json = file.read
parsed = JSON.parse(json)
#p parsed.keys
parsed["Instances"].each do |inst|
ip=nil
tags=Hash.new
is_running=false
inst.each do |prop|
case prop[0]
when "PrivateIpAddress"
p prop[1]
ip = prop[1]
when "State"
is_running = prop[1]["Name"]=="running"
when "Tags"
prop[1].each do |tag|
p tag["Value"] if tag["Key"] == "Name"
tags[tag["Key"]]=tag["Value"]
end
end
end
p 'Machine '+ ip+ ' is running' if is_running
ret1 = [tags["Name"], ip] if is_running and tags["cost.centre"] == "1234"
p ret1
ret2 = {:name => tags["Name"], :ip => ip, :cost_centre=> tags["cost.centre"]}
p ret2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment