21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/puppet/network/http/api/master/v3/environment.rb', line 21
def build_environment_graph(catalog)
env_graph = {:environment => catalog.environment, :applications => {}, :code_id => catalog.code_id}
applications = catalog.resources.select do |res|
type = res.resource_type
type.is_a?(Puppet::Resource::Type) && type.application?
end
applications.each do |app|
file, line = app.file, app.line
nodes = app['nodes']
required_components = catalog.direct_dependents_of(app).map {|comp| comp.ref}
mapped_components = nodes.values.flatten.map {|comp| comp.ref}
nonexistent_components = mapped_components - required_components
if nonexistent_components.any?
raise Puppet::ParseError.new("Application #{app} assigns nodes to non-existent components: #{nonexistent_components.join(', ')}", file, line)
end
missing_components = required_components - mapped_components
if missing_components.any?
raise Puppet::ParseError.new("Application #{app} has components without assigned nodes: #{missing_components.join(', ')}", file, line)
end
node_mapping = {}
nodes.each do |node, comps|
comps = [comps] unless comps.is_a?(Array)
comps.each do |comp|
raise Puppet::ParseError.new(_("Application %{app} assigns multiple nodes to component %{comp}") % { app: app, comp: comp }, file, line) if node_mapping.include?(comp.ref)
node_mapping[comp.ref] = node.title
end
end
app_components = {}
catalog.direct_dependents_of(app).each do |comp|
app_components[comp.ref] = {
:produces => comp.export.map(&:ref),
:consumes => prerequisites(comp).map(&:ref),
:node => node_mapping[comp.ref]
}
end
env_graph[:applications][app.ref] = app_components
end
env_graph
end
|