def synthesize_edges(hash, catalog)
profile("Synthesize edges",
[:puppetdb, :edges, :synthesize]) do
aliases = map_aliases_to_title(hash)
resource_table = {}
profile("Build up resource_table",
[:puppetdb, :edges, :synthesize, :resource_table, :build]) do
hash['resources'].each do |resource|
resource_table[ [resource['type'], resource['title']] ] = resource
end
end
profile("Primary synthesis",
[:puppetdb, :edges, :synthesize, :primary_synthesis])do
hash['resources'].each do |resource|
if real_resource = catalog.resource(resource['type'], resource['title'])
next if real_resource.virtual?
end
Relationships.each do |param,relation|
if value = resource['parameters'][param]
[value].flatten.each do |other_ref|
edge = {'relationship' => relation[:relationship]}
resource_hash = {'type' => resource['type'], 'title' => resource['title']}
other_hash = resource_ref_to_hash(other_ref)
unless other_ref =~ /^[A-Z][a-z0-9_-]*(::[A-Z][a-z0-9_-]*)*\[.*\]/m
rel = edge_to_s(resource_hash_to_ref(resource_hash), other_ref, param)
raise Puppet::Error, "Invalid relationship: #{rel}, because " +
"#{other_ref} doesn't seem to be in the correct format. " +
"Resource references should be formatted as: " +
"Classname['title'] or Modulename::Classname['title'] (take " +
"careful note of the capitalization)."
end
if other_hash['type'] == 'File' and other_hash['title'] =~ /\/$/
other_hash['title'] = other_hash['title'].sub(/\/+$/, '')
end
other_array = [other_hash['type'], other_hash['title']]
other_resource = resource_table[other_array]
if other_resource.nil? and alias_hash = aliases[other_array]
other_resource = resource_table[ alias_hash.values_at('type', 'title') ]
end
raise Puppet::Error, "Invalid relationship: #{edge_to_s(resource_hash_to_ref(resource_hash), other_ref, param)}, because #{other_ref} doesn't seem to be in the catalog" unless other_resource
if other_real_resource = catalog.resource(other_resource['type'], other_resource['title'])
if other_real_resource.virtual?
raise Puppet::Error, "Invalid relationship: #{edge_to_s(resource_hash_to_ref(resource_hash), other_ref, param)}, because #{other_ref} is exported but not collected"
end
end
other_hash['title'] = other_resource['title']
if relation[:direction] == :forward
edge.merge!('source' => resource_hash, 'target' => other_hash)
else
edge.merge!('source' => other_hash, 'target' => resource_hash)
end
hash['edges'] << edge
end
end
end
end
end
profile("Make edges unique",
[:puppetdb, :edges, :synthesize, :make_unique]) do
hash['edges'].uniq!
end
hash
end
end