Class: Puppet::Rest::Route

Inherits:
Object show all
Defined in:
lib/puppet/rest/route.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api:, server_setting: :server, port_setting: :masterport, srv_service: :puppet) ⇒ Route

Create a Route containing information for querying the given API, hosted at a server determined either by SRV service or by the fallback server on the fallback port.

Parameters:

  • api (String)

    the path leading to the root of the API. Must contain a trailing slash for proper endpoint path construction

  • server_setting (Symbol) (defaults to: :server)

    the setting to check for special server configuration

  • port_setting (Symbol) (defaults to: :masterport)

    the setting to check for speical port configuration

  • srv_service (Symbol) (defaults to: :puppet)

    the name of the service when using SRV records



20
21
22
23
24
25
# File 'lib/puppet/rest/route.rb', line 20

def initialize(api:, server_setting: :server, port_setting: :masterport, srv_service: :puppet)
  @api = api
  @default_server = Puppet::Util::Connection.determine_server(server_setting)
  @default_port = Puppet::Util::Connection.determine_port(port_setting, server_setting)
  @srv_service = srv_service
end

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server.



6
7
8
# File 'lib/puppet/rest/route.rb', line 6

def server
  @server
end

Instance Method Details

#with_base_url(dns_resolver) {|URI| ... } ⇒ Object

Select a server and port to create a base URL for the API specified by this route. If the connection fails and SRV records are in use, the next suitable server will be tried. If SRV records are not in use or no successful connection could be made, fall back to the configured server and port for this API, taking into account failover settings.

Yields:

  • (URI)

    supply a base URL to make a request with

Raises:

  • (Puppet::Error)

    if connection to selected server and port fails, and SRV records are not in use



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
70
71
72
73
74
# File 'lib/puppet/rest/route.rb', line 37

def with_base_url(dns_resolver)
  if @server && @port
    # First try connecting to the previously selected server and port.
    begin
      return yield(base_url)
    rescue SystemCallError => e
      if Puppet[:use_srv_records]
        Puppet.debug "Connection to cached server and port #{@server}:#{@port} failed, reselecting."
      else
        raise Puppet::Error, _("Connection to cached server and port %{server}:%{port} failed: %{message}") %
          { server: @server, port: @port, message: e.message }
      end
    end
  end

  if Puppet[:use_srv_records]
    dns_resolver.each_srv_record(Puppet[:srv_domain], @srv_service) do |srv_server, srv_port|
      # Try each of the servers for this service in weighted order
      # until a working one is found.
      begin
        @server = srv_server
        @port = srv_port
        return yield(base_url)
      rescue SystemCallError
        Puppet.debug "Connection to selected server and port #{@server}:#{@port} failed. Trying next cached SRV record."
        @server = nil
        @port = nil
      end
    end
  end

  # If not using SRV records, fall back to the defaults calculated above
  @server = @default_server
  @port = @default_port

  Puppet.debug "No more servers in SRV record, falling back to #{@server}:#{@port}" if Puppet[:use_srv_records]
  return yield(base_url)
end