Class: Puppet::HTTP::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/http/session.rb

Defined Under Namespace

Classes: Route

Constant Summary collapse

ROUTES =
{
  ca: Route.new(Puppet::HTTP::Service::Ca, '/puppet-ca/v1', :ca_server, :ca_port),
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(client, resolvers) ⇒ Session


8
9
10
11
12
# File 'lib/puppet/http/session.rb', line 8

def initialize(client, resolvers)
  @client = client
  @resolvers = resolvers
  @resolved_services = {}
end

Instance Method Details

#create_service(name, server = nil, port = nil) ⇒ Object

Raises:

  • (ArgumentError)

43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/puppet/http/session.rb', line 43

def create_service(name, server = nil, port = nil)
  route = ROUTES[name]
  raise ArgumentError, "Unknown service #{name}" unless route

  server ||= Puppet[route.server_setting]
  port   ||= Puppet[route.port_setting]
  url = URI::HTTPS.build(host: server,
                         port: port,
                         path: route.api
                        ).freeze
  route.service_class.new(@client, url)
end

#route_to(name, ssl_context: nil) ⇒ Object

Raises:

  • (ArgumentError)

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
# File 'lib/puppet/http/session.rb', line 14

def route_to(name, ssl_context: nil)
  route = ROUTES[name]
  raise ArgumentError, "Unknown service #{name}" unless route

  cached = @resolved_services[name]
  return cached if cached

  errors = []

  @resolvers.each do |resolver|
    Puppet.debug("Resolving service '#{name}' using #{resolver.class}")
    resolver.resolve(self, name) do |service|
      begin
        service.connect(ssl_context: ssl_context)
        @resolved_services[name] = service
        Puppet.debug("Resolved service '#{name}' to #{service.url}")
        return service
      rescue Puppet::HTTP::ConnectionError => e
        errors << e
        Puppet.debug("Connection to #{service.url} failed, trying next route: #{e.message}")
      end
    end
  end

  errors.each { |e| Puppet.log_exception(e) }

  raise Puppet::HTTP::RouteError, "No more routes to #{name}"
end