Created
May 7, 2015 12:33
-
-
Save Cinderhaze/36039d8521c22e5965ff to your computer and use it in GitHub Desktop.
My first 'successful' attempt at getting the info I need
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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