class Puppet::Indirector::REST

Access objects via REST

Attributes

port_setting[R]
server_setting[R]

Public Instance Methods

add_profiling_header(headers) click to toggle source
# File lib/puppet/indirector/rest.rb, line 49
def add_profiling_header(headers)
  if (Puppet[:profile])
    headers[Puppet::Network::HTTP::HEADER_ENABLE_PROFILING] = "true"
  end
  headers
end
destroy(request) click to toggle source
# File lib/puppet/indirector/rest.rb, line 151
def destroy(request)
  raise ArgumentError, "DELETE does not accept options" unless request.options.empty?

  response = do_request(request) do |request|
    http_delete(request, Puppet::Network::HTTP::API::V1.indirection2uri(request), headers)
  end

  if is_http_200?(response)
    check_master_version(response)
    content_type, body = parse_response(response)
    deserialize_destroy(content_type, body)
  else
    nil
  end
end
do_request(request) { |request| ... } click to toggle source

Encapsulate call to request.do_request with the arguments from this class Then yield to the code block that was called in We certainly could have retained the full request.do_request(…) { |r| … } but this makes the code much cleaner and we only then actually make the call to request.do_request from here, thus if we change what we pass or how we get it, we only need to change it here.

# File lib/puppet/indirector/rest.rb, line 189
def do_request(request)
  request.do_request(self.class.srv_service, self.class.server, self.class.port) { |request| yield(request) }
end
find(request) click to toggle source
# File lib/puppet/indirector/rest.rb, line 86
def find(request)
  uri, body = Puppet::Network::HTTP::API::V1.request_to_uri_and_body(request)
  uri_with_query_string = "#{uri}?#{body}"

  response = do_request(request) do |request|
    # WEBrick in Ruby 1.9.1 only supports up to 1024 character lines in an HTTP request
    # http://redmine.ruby-lang.org/issues/show/3991
    if "GET #{uri_with_query_string} HTTP/1.1\r\n".length > 1024
      http_post(request, uri, body, headers)
    else
      http_get(request, uri_with_query_string, headers)
    end
  end

  if is_http_200?(response)
    check_master_version(response)
    content_type, body = parse_response(response)
    result = deserialize_find(content_type, body)
    result.name = request.key if result.respond_to?(:name=)
    result

  elsif is_http_404?(response)
    return nil unless request.options[:fail_on_404]

    # 404 can get special treatment as the indirector API can not produce a meaningful
    # reason to why something is not found - it may not be the thing the user is
    # expecting to find that is missing, but something else (like the environment).
    # While this way of handling the issue is not perfect, there is at least an error
    # that makes a user aware of the reason for the failure.
    #
    content_type, body = parse_response(response)
    msg = "Find #{elide(uri_with_query_string, 100)} resulted in 404 with the message: #{body}"
    raise Puppet::Error, msg
  else
    nil
  end
end
head(request) click to toggle source
# File lib/puppet/indirector/rest.rb, line 124
def head(request)
  response = do_request(request) do |request|
    http_head(request, Puppet::Network::HTTP::API::V1.indirection2uri(request), headers)
  end

  if is_http_200?(response)
    check_master_version(response)
    true
  else
    false
  end
end
headers() click to toggle source

Provide appropriate headers.

# File lib/puppet/indirector/rest.rb, line 45
def headers
  add_accept_encoding({"Accept" => model.supported_formats.join(", ")})
end
http_delete(request, path, headers = nil, *args) click to toggle source
# File lib/puppet/indirector/rest.rb, line 73
def http_delete(request, path, headers = nil, *args)
  http_request(:delete, request, path, add_profiling_header(headers), *args)
end
http_get(request, path, headers = nil, *args) click to toggle source
# File lib/puppet/indirector/rest.rb, line 61
def http_get(request, path, headers = nil, *args)
  http_request(:get, request, path, add_profiling_header(headers), *args)
end
http_head(request, path, headers = nil, *args) click to toggle source
# File lib/puppet/indirector/rest.rb, line 69
def http_head(request, path, headers = nil, *args)
  http_request(:head, request, path, add_profiling_header(headers), *args)
end
http_post(request, path, data, headers = nil, *args) click to toggle source
# File lib/puppet/indirector/rest.rb, line 65
def http_post(request, path, data, headers = nil, *args)
  http_request(:post, request, path, data, add_profiling_header(headers), *args)
end
http_put(request, path, data, headers = nil, *args) click to toggle source
# File lib/puppet/indirector/rest.rb, line 77
def http_put(request, path, data, headers = nil, *args)
  http_request(:put, request, path, data, add_profiling_header(headers), *args)
end
http_request(method, request, *args) click to toggle source
# File lib/puppet/indirector/rest.rb, line 81
def http_request(method, request, *args)
  conn = network(request)
  conn.send(method, *args)
end
network(request) click to toggle source
# File lib/puppet/indirector/rest.rb, line 56
def network(request)
  Puppet::Network::HttpPool.http_instance(request.server || self.class.server,
                                          request.port || self.class.port)
end
save(request) click to toggle source
# File lib/puppet/indirector/rest.rb, line 167
def save(request)
  raise ArgumentError, "PUT does not accept options" unless request.options.empty?

  response = do_request(request) do |request|
    http_put(request, Puppet::Network::HTTP::API::V1.indirection2uri(request), request.instance.render, headers.merge({ "Content-Type" => request.instance.mime }))
  end

  if is_http_200?(response)
    check_master_version(response)
    content_type, body = parse_response(response)
    deserialize_save(content_type, body)
  else
    nil
  end
end
validate_key(request) click to toggle source
# File lib/puppet/indirector/rest.rb, line 193
def validate_key(request)
  # Validation happens on the remote end
end

Public Class Methods

port() click to toggle source
# File lib/puppet/indirector/rest.rb, line 40
def self.port
  Puppet.settings[port_setting || :masterport].to_i
end
server() click to toggle source
# File lib/puppet/indirector/rest.rb, line 36
def self.server
  Puppet.settings[server_setting || :server]
end
srv_service() click to toggle source
# File lib/puppet/indirector/rest.rb, line 32
def self.srv_service
  @srv_service || :puppet
end
use_port_setting(setting) click to toggle source

Specify the setting that we should use to get the port.

# File lib/puppet/indirector/rest.rb, line 23
def self.use_port_setting(setting)
  @port_setting = setting
end
use_server_setting(setting) click to toggle source

Specify the setting that we should use to get the server name.

# File lib/puppet/indirector/rest.rb, line 18
def self.use_server_setting(setting)
  @server_setting = setting
end
use_srv_service(service) click to toggle source

Specify the service to use when doing SRV record lookup

# File lib/puppet/indirector/rest.rb, line 28
def self.use_srv_service(service)
  @srv_service = service
end