# class Puppet::Indirector::REST

Access objects via REST

### Attributes

port_setting[R]
server_setting[R]

### 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

### Public Instance Methods

convert_to_http_error(response) click to toggle source

    # File lib/puppet/indirector/rest.rb, line 69
    def convert_to_http_error(response)
      message = "Error #{response.code} on SERVER: #{(response.body||'').empty? ? response.message : uncompress_body(response)}"
      Net::HTTPError.new(message, response)
    end

deserialize(response, multiple = false) click to toggle source

Figure out the content type, turn that into a format, and use the format to extract the body of the response.

    # File lib/puppet/indirector/rest.rb, line 46
    def deserialize(response, multiple = false)
      case response.code
      when "404"
        return nil
      when /^2/
        raise "No content type in http response; cannot parse" unless response['content-type']

        content_type = response['content-type'].gsub(/\s*;.*$/,'') # strip any appended charset

        body = uncompress_body(response)

        # Convert the response to a deserialized object.
        if multiple
          model.convert_from_multiple(content_type, body)
        else
          model.convert_from(content_type, body)
        end
      else
        # Raise the http error if we didn't get a 'success' of some kind.
        raise convert_to_http_error(response)
      end
    end

destroy(request) click to toggle source

    # File lib/puppet/indirector/rest.rb, line 154
    def destroy(request)
      raise ArgumentError, "DELETE does not accept options" unless request.options.empty?

      do_request(request) do |request|
        return deserialize(http_delete(request, indirection2uri(request), headers))
      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 176
    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 108
    def find(request)
      uri, body = 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
      result = deserialize(response)

      return nil unless result

      result.name = request.key if result.respond_to?(:name=)
      result
    end

head(request) click to toggle source

    # File lib/puppet/indirector/rest.rb, line 129
    def head(request)
      response = do_request(request) do |request|
        http_head(request, indirection2uri(request), headers)
      end

      case response.code
      when "404"
        return false
      when /^2/
        return true
      else
        # Raise the http error if we didn't get a 'success' of some kind.
        raise convert_to_http_error(response)
      end
    end

headers() click to toggle source

Provide appropriate headers.

    # File lib/puppet/indirector/rest.rb, line 75
    def headers
      add_accept_encoding({"Accept" => model.supported_formats.join(", ")})
    end

http_delete(request, *args) click to toggle source

    # File lib/puppet/indirector/rest.rb, line 95
    def http_delete(request, *args)
      http_request(:delete, request, *args)
    end

http_get(request, *args) click to toggle source

    # File lib/puppet/indirector/rest.rb, line 83
    def http_get(request, *args)
      http_request(:get, request, *args)
    end

http_head(request, *args) click to toggle source

    # File lib/puppet/indirector/rest.rb, line 91
    def http_head(request, *args)
      http_request(:head, request, *args)
    end

http_post(request, *args) click to toggle source

    # File lib/puppet/indirector/rest.rb, line 87
    def http_post(request, *args)
      http_request(:post, request, *args)
    end

http_put(request, *args) click to toggle source

    # File lib/puppet/indirector/rest.rb, line 99
    def http_put(request, *args)
      http_request(:put, request, *args)
    end

http_request(method, request, *args) click to toggle source

    # File lib/puppet/indirector/rest.rb, line 103
    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 79
    def network(request)
      Puppet::Network::HTTP::Connection.new(request.server || self.class.server, request.port || self.class.port)
    end

save(request) click to toggle source

    # File lib/puppet/indirector/rest.rb, line 162
    def save(request)
      raise ArgumentError, "PUT does not accept options" unless request.options.empty?

      do_request(request) do |request|
        deserialize http_put(request, indirection2uri(request), request.instance.render, headers.merge({ "Content-Type" => request.instance.mime }))
      end
    end

search(request) click to toggle source

    # File lib/puppet/indirector/rest.rb, line 145
    def search(request)
      result = do_request(request) do |request|
        deserialize(http_get(request, indirection2uri(request), headers), true)
      end

      # result from the server can be nil, but we promise to return an array...
      result || []
    end

validate_key(request) click to toggle source

    # File lib/puppet/indirector/rest.rb, line 180
    def validate_key(request)
      # Validation happens on the remote end
    end