class Puppet::HTTP::Service

Represents an abstract Puppet web service.

@abstract Subclass and implement methods for the service's REST APIs. @api public

Constants

EXCLUDED_FORMATS

@return [Array<Symbol>] format types that are unsupported

SERVICE_NAMES

@return [Array<Symbol>] available services

Attributes

url[R]

@return [URI] the url associated with this service

Public Class Methods

create_service(client, session, name, server = nil, port = nil) click to toggle source

Create a new web service, which contains the URL used to connect to the service. The four services implemented are `:ca`, `:fileserver`, `:puppet`, and `:report`.

The `:ca` and `:report` services handle certs and reports, respectively. The `:fileserver` service handles puppet file metadata and content requests. And the default service, `:puppet`, handles nodes, facts, and catalogs.

@param [Puppet::HTTP::Client] client the owner of the session @param [Puppet::HTTP::Session] session the owner of the service @param [Symbol] name the type of service to create @param [<Type>] server optional, the server to connect to @param [<Type>] port optional, the port to connect to

@return [Puppet::HTTP::Service] an instance of the service type requested

@api private

   # File lib/puppet/http/service.rb
33 def self.create_service(client, session, name, server = nil, port = nil)
34   case name
35   when :ca
36     Puppet::HTTP::Service::Ca.new(client, session, server, port)
37   when :fileserver
38     Puppet::HTTP::Service::FileServer.new(client, session, server, port)
39   when :puppet
40     ::Puppet::HTTP::Service::Compiler.new(client, session, server, port)
41   when :puppetserver
42     ::Puppet::HTTP::Service::Puppetserver.new(client, session, server, port)
43   when :report
44     Puppet::HTTP::Service::Report.new(client, session, server, port)
45   else
46     raise ArgumentError, "Unknown service #{name}"
47   end
48 end
new(client, session, url) click to toggle source

Create a new service. Services should be created by calling `Puppet::HTTP::Session#route_to`.

@param [Puppet::HTTP::Client] client @param [Puppet::HTTP::Session] session @param [URI] url The url to connect to

@api private

   # File lib/puppet/http/service.rb
68 def initialize(client, session, url)
69   @client = client
70   @session = session
71   @url = url
72 end
valid_name?(name) click to toggle source

Check if the service named is included in the list of available services.

@param [Symbol] name

@return [Boolean]

@api private

   # File lib/puppet/http/service.rb
57 def self.valid_name?(name)
58   SERVICE_NAMES.include?(name)
59 end

Public Instance Methods

connect(ssl_context: nil) click to toggle source

Open a connection using the given ssl context.

@param [Puppet::SSL::SSLContext] ssl_context An optional ssl context to connect with @return [void]

@api public

   # File lib/puppet/http/service.rb
93 def connect(ssl_context: nil)
94   @client.connect(@url, options: {ssl_context: ssl_context})
95 end
with_base_url(path) click to toggle source

Return the url with the given path encoded and appended

@param [String] path the string to append to the base url

@return [URI] the URI object containing the encoded path

@api public

   # File lib/puppet/http/service.rb
81 def with_base_url(path)
82   u = @url.dup
83   u.path += Puppet::Util.uri_encode(path)
84   u
85 end

Protected Instance Methods

add_puppet_headers(headers) click to toggle source
    # File lib/puppet/http/service.rb
 99 def add_puppet_headers(headers)
100   modified_headers = headers.dup
101 
102   # Add 'X-Puppet-Profiling' to enable performance profiling if turned on
103   modified_headers['X-Puppet-Profiling'] = 'true' if Puppet[:profile]
104 
105   # Add additional user-defined headers if they are defined
106   Puppet[:http_extra_headers].each do |name, value|
107     if modified_headers.keys.find { |key| key.casecmp(name) == 0 }
108       Puppet.warning(_('Ignoring extra header "%{name}" as it was previously set.') % { name: name })
109     else
110       if value.nil? || value.empty?
111         Puppet.warning(_('Ignoring extra header "%{name}" as it has no value.') % { name: name })
112       else
113         modified_headers[name] = value
114       end
115     end
116   end
117   modified_headers
118 end
build_url(api, server, port) click to toggle source
    # File lib/puppet/http/service.rb
120 def build_url(api, server, port)
121   URI::HTTPS.build(host: server,
122                    port: port,
123                    path: api
124                   ).freeze
125 end
deserialize(response, model) click to toggle source
    # File lib/puppet/http/service.rb
160 def deserialize(response, model)
161   formatter = formatter_for_response(response)
162   begin
163     formatter.intern(model, response.body.to_s)
164   rescue => err
165     raise Puppet::HTTP::SerializationError.new("Failed to deserialize #{model} from #{formatter.name}: #{err.message}", err)
166   end
167 end
deserialize_multiple(response, model) click to toggle source
    # File lib/puppet/http/service.rb
169 def deserialize_multiple(response, model)
170   formatter = formatter_for_response(response)
171   begin
172     formatter.intern_multiple(model, response.body.to_s)
173   rescue => err
174     raise Puppet::HTTP::SerializationError.new("Failed to deserialize multiple #{model} from #{formatter.name}: #{err.message}", err)
175   end
176 end
formatter_for_response(response) click to toggle source
    # File lib/puppet/http/service.rb
132 def formatter_for_response(response)
133   header = response['Content-Type']
134   raise Puppet::HTTP::ProtocolError.new(_("No content type in http response; cannot parse")) unless header
135 
136   header.gsub!(/\s*;.*$/,'') # strip any charset
137 
138   formatter = Puppet::Network::FormatHandler.mime(header)
139   raise Puppet::HTTP::ProtocolError.new("Content-Type is unsupported") if EXCLUDED_FORMATS.include?(formatter.name)
140 
141   formatter
142 end
get_mime_types(model) click to toggle source
    # File lib/puppet/http/service.rb
127 def get_mime_types(model)
128   network_formats = model.supported_formats - EXCLUDED_FORMATS
129   network_formats.map { |f| model.get_format(f).mime }
130 end
process_response(response) click to toggle source
    # File lib/puppet/http/service.rb
178 def process_response(response)
179   @session.process_response(response)
180 
181   raise Puppet::HTTP::ResponseError.new(response) unless response.success?
182 end
serialize(formatter, object) click to toggle source
    # File lib/puppet/http/service.rb
144 def serialize(formatter, object)
145   begin
146     formatter.render(object)
147   rescue => err
148     raise Puppet::HTTP::SerializationError.new("Failed to serialize #{object.class} to #{formatter.name}: #{err.message}", err)
149   end
150 end
serialize_multiple(formatter, object) click to toggle source
    # File lib/puppet/http/service.rb
152 def serialize_multiple(formatter, object)
153   begin
154     formatter.render_multiple(object)
155   rescue => err
156     raise Puppet::HTTP::SerializationError.new("Failed to serialize multiple #{object.class} to #{formatter.name}: #{err.message}", err)
157   end
158 end