Skip to content

Instantly share code, notes, and snippets.

@TomyLobo
Last active October 6, 2015 11:56
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 TomyLobo/f659bf9c1ed81de7be3e to your computer and use it in GitHub Desktop.
Save TomyLobo/f659bf9c1ed81de7be3e to your computer and use it in GitHub Desktop.
Puppet catalog to TGF graph format converter
#!/usr/bin/ruby
require 'json'
require 'tgf'
catalog_file_name = 'catalog.json'
graph_file_name = 'foo.tgf'
catalog = JSON.parse(File.read(catalog_file_name))
nodes = catalog['data']['resources']
edges = catalog['data']['edges']
edges.map! do |entry|
from = entry['source'].gsub(' ', '_')
to = entry['target'].gsub(' ', '_')
TGF::Edge.new("#{from} #{to}")
end
nodes.map! do |entry|
label = "#{entry['type']}[#{entry['title']}]"
id = label.gsub(' ', '_')
TGF::Node.new("#{id} #{label}")
end
graph_file = File.open(graph_file_name, 'w')
TGF.write(graph_file, nodes, edges)
source 'https://rubygems.org'
gem 'tgf', '~> 1.1.0'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment