Class: Puppet::HTTP::Session Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The session is the mechanism by which services may be connected to and accessed.

Constant Summary collapse

CAP_LOCALES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

capabilities for a site

'locales'.freeze
CAP_JSON =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

'json'.freeze
SUPPORTED_LOCALES_MOUNT_AGENT_VERSION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

puppet version where locales mount was added

Gem::Version.new("5.3.4")
SUPPORTED_JSON_DEFAULT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

puppet version where JSON was enabled by default

Gem::Version.new("5.0.0")

Instance Method Summary collapse

Constructor Details

#initialize(client, resolvers) ⇒ Session

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new HTTP session. The session is the mechanism by which services may be connected to and accessed.

Parameters:



27
28
29
30
31
32
# File 'lib/puppet/http/session.rb', line 27

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

Instance Method Details

#process_response(response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Collect per-site server versions. This will allow us to modify future requests based on the version of puppetserver we are talking to.

Parameters:



92
93
94
95
96
97
98
# File 'lib/puppet/http/session.rb', line 92

def process_response(response)
  version = response[Puppet::HTTP::HEADER_PUPPET_VERSION]
  if version
    site = Puppet::Network::HTTP::Site.from_uri(response.url)
    @server_versions[site] = version
  end
end

#route_to(name, url: nil, ssl_context: nil) ⇒ Puppet::HTTP::Service

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

If an explicit server and port are specified on the command line or configuration file, this method always returns a Service with that host and port. Otherwise, we walk the list of resolvers in priority order:

- DNS SRV
- Server List
- Puppet server/port settings

If a given resolver fails to connect, it tries the next available resolver until a successful connection is found and returned. The successful service is cached and returned if ‘route_to` is called again.

Parameters:

  • name (Symbol)

    the service to resolve

  • url (URI) (defaults to: nil)

    (nil) optional explicit url to use, if it is already known

  • ssl_context (Puppet::SSL::SSLContext) (defaults to: nil)

    ssl_context ssl context to be used for connections

Returns:

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/puppet/http/session.rb', line 54

def route_to(name, url: nil, ssl_context: nil)
  raise ArgumentError, "Unknown service #{name}" unless Puppet::HTTP::Service.valid_name?(name)

  # short circuit if explicit URL host & port given
  if url && url.host != nil && !url.host.empty?
    service = Puppet::HTTP::Service.create_service(@client, self, name, url.host, url.port)
    service.connect(ssl_context: ssl_context)
    return service
  end

  cached = @resolved_services[name]
  return cached if cached

  resolution_exceptions = []
  error_handler = proc { |e| resolution_exceptions << e }

  @resolvers.each do |resolver|
    Puppet.debug("Resolving service '#{name}' using #{resolver.class}")
    service = resolver.resolve(self, name, ssl_context: ssl_context, error_handler: error_handler)
    if service
      @resolved_services[name] = service
      Puppet.debug("Resolved service '#{name}' to #{service.url}")
      return service
    end
  end

  resolution_exceptions.each { |e| Puppet.log_exception(e) }
  raise Puppet::HTTP::RouteError, "No more routes to #{name}"
end

#supports?(name, capability) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine if a session supports a capability. Depending on the server version we are talking to, we know certain features are available or not. These specifications are defined here so we can modify our requests appropriately.

Parameters:

  • name (Symbol)

    name of the service to check

  • capability (String)

    the capability, ie ‘locales` or `json`

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/puppet/http/session.rb', line 112

def supports?(name, capability)
  raise ArgumentError, "Unknown service #{name}" unless Puppet::HTTP::Service.valid_name?(name)

  service = @resolved_services[name]
  return false unless service

  site = Puppet::Network::HTTP::Site.from_uri(service.url)
  server_version = @server_versions[site]

  case capability
  when CAP_LOCALES
    !server_version.nil? && Gem::Version.new(server_version) >= SUPPORTED_LOCALES_MOUNT_AGENT_VERSION
  when CAP_JSON
    server_version.nil? || Gem::Version.new(server_version) >= SUPPORTED_JSON_DEFAULT
  else
    false
  end
end