Class: Puppet::Network::HTTP::API::Master::V3::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/network/http/api/master/v3/environment.rb

Instance Method Summary collapse

Instance Method Details

#call(request, response) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
# File 'lib/puppet/network/http/api/master/v3/environment.rb', line 5

def call(request, response)
  env_name = request.routing_path.split('/').last
  env = Puppet.lookup(:environments).get(env_name)

  if env.nil?
    raise Puppet::Network::HTTP::Error::HTTPNotFoundError.new("#{env_name} is not a known environment", Puppet::Network::HTTP::Issues::RESOURCE_NOT_FOUND)
  end

  catalog = Puppet::Parser::EnvironmentCompiler.compile(env).to_resource

  env_graph = {:environment => env.name, :applications => {}}
  applications = catalog.resources.select do |res|
    type = res.resource_type
    type.is_a?(Puppet::Resource::Type) && type.application?
  end
  applications.each do |app|
    app_components = {}
    # Turn the 'nodes' hash into a map component ref => node name
    node_mapping = {}
    app['nodes'].each do |node, comps|
      comps = [comps] unless comps.is_a?(Array)
      comps.each do |comp|
        raise Puppet::ParseError, "Application #{app} maps component #{comp} to multiple nodes" if node_mapping.include?(comp.ref)
        node_mapping[comp.ref] = node.title
      end
    end

    catalog.direct_dependents_of(app).each do |comp|
      mapped_node = node_mapping[comp.ref]
      if mapped_node.nil?
        raise Puppet::ParseError, "Component #{comp} is not mapped to any node"
      end
      app_components[comp.ref] = {
        :produces => comp.export.map(&:ref),
        :consumes => prerequisites(comp).map(&:ref),
        :node => mapped_node
      }
    end
    env_graph[:applications][app.ref] = app_components
  end
  response.respond_with(200, "application/json", JSON.dump(env_graph))
end