File: rest.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (64 lines) | stat: -rw-r--r-- 1,644 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Access objects via REST
class Puppet::Indirector::REST < Puppet::Indirector::Terminus
  def find(request)
    raise NotImplementedError
  end

  def head(request)
    raise NotImplementedError
  end

  def search(request)
    raise NotImplementedError
  end

  def destroy(request)
    raise NotImplementedError
  end

  def save(request)
    raise NotImplementedError
  end

  def validate_key(request)
    # Validation happens on the remote end
  end

  private

  def convert_to_http_error(response)
    if response.body.to_s.empty? && response.reason
      returned_message = response.reason
    elsif response['content-type'].is_a?(String)
      content_type, body = parse_response(response)
      if content_type =~ /[pj]son/
        returned_message = Puppet::Util::Json.load(body)["message"]
      else
        returned_message = response.body
      end
    else
      returned_message = response.body
    end

    message = _("Error %{code} on SERVER: %{returned_message}") % { code: response.code, returned_message: returned_message }
    Net::HTTPError.new(message, Puppet::HTTP::ResponseConverter.to_ruby_response(response))
  end

  # Returns the content_type, stripping any appended charset, and the
  # body, decompressed if necessary
  def parse_response(response)
    if response['content-type']
      [ response['content-type'].gsub(/\s*;.*$/,''), response.body ]
    else
      raise _("No content type in http response; cannot parse")
    end
  end

  def elide(string, length)
    if Puppet::Util::Log.level == :debug || string.length <= length
      string
    else
      string[0, length - 3] + "..."
    end
  end
end