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
@return [URI] the url associated with this service
Public Class Methods
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 32 def self.create_service(client, session, name, server = nil, port = nil) 33 case name 34 when :ca 35 Puppet::HTTP::Service::Ca.new(client, session, server, port) 36 when :fileserver 37 Puppet::HTTP::Service::FileServer.new(client, session, server, port) 38 when :puppet 39 ::Puppet::HTTP::Service::Compiler.new(client, session, server, port) 40 when :puppetserver 41 ::Puppet::HTTP::Service::Puppetserver.new(client, session, server, port) 42 when :report 43 Puppet::HTTP::Service::Report.new(client, session, server, port) 44 else 45 raise ArgumentError, "Unknown service #{name}" 46 end 47 end
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 67 def initialize(client, session, url) 68 @client = client 69 @session = session 70 @url = url 71 end
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 56 def self.valid_name?(name) 57 SERVICE_NAMES.include?(name) 58 end
Public Instance Methods
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 92 def connect(ssl_context: nil) 93 @client.connect(@url, options: {ssl_context: ssl_context}) 94 end
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 80 def with_base_url(path) 81 u = @url.dup 82 u.path += Puppet::Util.uri_encode(path) 83 u 84 end
Protected Instance Methods
# File lib/puppet/http/service.rb 98 def add_puppet_headers(headers) 99 modified_headers = headers.dup 100 101 # Add 'X-Puppet-Profiling' to enable performance profiling if turned on 102 modified_headers['X-Puppet-Profiling'] = 'true' if Puppet[:profile] 103 104 # Add additional user-defined headers if they are defined 105 Puppet[:http_extra_headers].each do |name, value| 106 if modified_headers.keys.find { |key| key.casecmp(name) == 0 } 107 Puppet.warning(_('Ignoring extra header "%{name}" as it was previously set.') % { name: name }) 108 else 109 if value.nil? || value.empty? 110 Puppet.warning(_('Ignoring extra header "%{name}" as it has no value.') % { name: name }) 111 else 112 modified_headers[name] = value 113 end 114 end 115 end 116 modified_headers 117 end
# File lib/puppet/http/service.rb 119 def build_url(api, server, port) 120 URI::HTTPS.build(host: server, 121 port: port, 122 path: api 123 ).freeze 124 end
# File lib/puppet/http/service.rb 159 def deserialize(response, model) 160 formatter = formatter_for_response(response) 161 begin 162 formatter.intern(model, response.body.to_s) 163 rescue => err 164 raise Puppet::HTTP::SerializationError.new("Failed to deserialize #{model} from #{formatter.name}: #{err.message}", err) 165 end 166 end
# File lib/puppet/http/service.rb 168 def deserialize_multiple(response, model) 169 formatter = formatter_for_response(response) 170 begin 171 formatter.intern_multiple(model, response.body.to_s) 172 rescue => err 173 raise Puppet::HTTP::SerializationError.new("Failed to deserialize multiple #{model} from #{formatter.name}: #{err.message}", err) 174 end 175 end
# File lib/puppet/http/service.rb 131 def formatter_for_response(response) 132 header = response['Content-Type'] 133 raise Puppet::HTTP::ProtocolError.new(_("No content type in http response; cannot parse")) unless header 134 135 header.gsub!(/\s*;.*$/,'') # strip any charset 136 137 formatter = Puppet::Network::FormatHandler.mime(header) 138 raise Puppet::HTTP::ProtocolError.new("Content-Type is unsupported") if EXCLUDED_FORMATS.include?(formatter.name) 139 140 formatter 141 end
# File lib/puppet/http/service.rb 126 def get_mime_types(model) 127 network_formats = model.supported_formats - EXCLUDED_FORMATS 128 network_formats.map { |f| model.get_format(f).mime } 129 end
# File lib/puppet/http/service.rb 177 def process_response(response) 178 @session.process_response(response) 179 180 raise Puppet::HTTP::ResponseError.new(response) unless response.success? 181 end
# File lib/puppet/http/service.rb 143 def serialize(formatter, object) 144 begin 145 formatter.render(object) 146 rescue => err 147 raise Puppet::HTTP::SerializationError.new("Failed to serialize #{object.class} to #{formatter.name}: #{err.message}", err) 148 end 149 end
# File lib/puppet/http/service.rb 151 def serialize_multiple(formatter, object) 152 begin 153 formatter.render_multiple(object) 154 rescue => err 155 raise Puppet::HTTP::SerializationError.new("Failed to serialize multiple #{object.class} to #{formatter.name}: #{err.message}", err) 156 end 157 end