Class: Puppet::Indirector::REST

Inherits:
Terminus show all
Defined in:
lib/puppet/indirector/rest.rb

Overview

Access objects via REST

Constant Summary collapse

IndirectedRoutes =
Puppet::Network::HTTP::API::IndirectedRoutes
EXCLUDED_FORMATS =
[:yaml, :b64_zlib_yaml, :dot]
MAJOR_VERSION_JSON_DEFAULT =

puppet major version where JSON is enabled by default

5

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE, Util::PUPPET_STACK_INSERTION_FRAME, Util::RFC_3986_URI_REGEX

Constants included from Util::POSIX

Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS

Constants included from Util::SymbolicFileMode

Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Class Attribute Summary collapse

Attributes included from Util::Docs

#doc, #nodoc

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Terminus

abstract_terminus?, #allow_remote_requests?, const2name, #indirection, indirection_name, inherited, #initialize, mark_as_abstract_terminus, model, #model, #name, name2const, register_terminus_class, terminus_class, terminus_classes, #terminus_type, #validate, #validate_model

Methods included from Util::InstanceLoader

#instance_hash, #instance_load, #instance_loader, #instance_loading?, #loaded_instance, #loaded_instances

Methods included from Util

absolute_path?, benchmark, chuser, clear_environment, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, format_backtrace_array, format_puppetstack_frame, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, resolve_stackframe, safe_posix_fork, set_env, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, which, withenv, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, groups_of, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from Util::SymbolicFileMode

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Methods included from Util::Docs

#desc, #dochook, #doctable, #markdown_definitionlist, #markdown_header, #nodoc?, #pad, scrub

Constructor Details

This class inherits a constructor from Puppet::Indirector::Terminus

Class Attribute Details

.port_settingObject (readonly)

Returns the value of attribute port_setting.



21
22
23
# File 'lib/puppet/indirector/rest.rb', line 21

def port_setting
  @port_setting
end

.server_settingObject (readonly)

Returns the value of attribute server_setting.



21
22
23
# File 'lib/puppet/indirector/rest.rb', line 21

def server_setting
  @server_setting
end

Class Method Details

.portObject

Select the port to use based on the settings configuration for this indirection, taking into account the HA server list.



51
52
53
# File 'lib/puppet/indirector/rest.rb', line 51

def self.port
  Puppet::Util::Connection.determine_port(port_setting, server_setting)
end

.serverObject

Select the server to use based on the settings configuration for this indirection, taking into account the HA server list.



45
46
47
# File 'lib/puppet/indirector/rest.rb', line 45

def self.server
  Puppet::Util::Connection.determine_server(server_setting)
end

.srv_serviceObject



39
40
41
# File 'lib/puppet/indirector/rest.rb', line 39

def self.srv_service
  @srv_service || :puppet
end

.use_port_setting(setting) ⇒ Object

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



30
31
32
# File 'lib/puppet/indirector/rest.rb', line 30

def self.use_port_setting(setting)
  @port_setting = setting
end

.use_server_setting(setting) ⇒ Object

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



25
26
27
# File 'lib/puppet/indirector/rest.rb', line 25

def self.use_server_setting(setting)
  @server_setting = setting
end

.use_srv_service(service) ⇒ Object

Specify the service to use when doing SRV record lookup



35
36
37
# File 'lib/puppet/indirector/rest.rb', line 35

def self.use_srv_service(service)
  @srv_service = service
end

Instance Method Details

#add_profiling_header(headers) ⇒ Object



75
76
77
78
79
80
# File 'lib/puppet/indirector/rest.rb', line 75

def add_profiling_header(headers)
  if (Puppet[:profile])
    headers[Puppet::Network::HTTP::HEADER_ENABLE_PROFILING] = "true"
  end
  headers
end

#destroy(request) ⇒ Object

Raises:

  • (ArgumentError)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/puppet/indirector/rest.rb', line 180

def destroy(request)
  Puppet.deprecation_warning(_("Puppet::Indirector::Rest#destroy is deprecated. Use Puppet::HTTP::Client instead."))
  raise ArgumentError, _("DELETE does not accept options") unless request.options.empty?

  response = do_request(request) do |req|
    http_delete(req, IndirectedRoutes.request_to_uri(req), headers)
  end

  if is_http_200?(response)
    content_type, body = parse_response(response)
    deserialize_destroy(content_type, body)
  else
    nil
  end
end

#do_request(request) ⇒ Object

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.



218
219
220
221
222
223
224
# File 'lib/puppet/indirector/rest.rb', line 218

def do_request(request)
  response = request.do_request(self.class.srv_service, self.class.server, self.class.port) { |req| yield(req) }

  handle_response(request, response) if response

  response
end

#find(request) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/puppet/indirector/rest.rb', line 114

def find(request)
  Puppet.deprecation_warning(_("Puppet::Indirector::Rest#find is deprecated. Use Puppet::HTTP::Client instead."))
  uri, body = IndirectedRoutes.request_to_uri_and_body(request)
  uri_with_query_string = "#{uri}?#{body}"

  response = do_request(request) do |req|
    # 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
      uri_with_env = "#{uri}?environment=#{request.environment.name}"
      http_post(req, uri_with_env, body, headers)
    else
      http_get(req, uri_with_query_string, headers)
    end
  end

  if is_http_200?(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.
    #
    _, body = parse_response(response)
    msg = _("Find %{uri} resulted in 404 with the message: %{body}") % { uri: elide(uri_with_query_string, 100), body: body }
    raise Puppet::Error, msg
  else
    nil
  end
end

#handle_response(request, response) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/puppet/indirector/rest.rb', line 226

def handle_response(request, response)
  server_version = response[Puppet::Network::HTTP::HEADER_PUPPET_VERSION]
  if server_version
    Puppet.lookup(:server_agent_version) do
      Puppet.push_context(:server_agent_version => server_version)
    end
    if SemanticPuppet::Version.parse(server_version).major < MAJOR_VERSION_JSON_DEFAULT &&
        Puppet[:preferred_serialization_format] != 'pson'
      #TRANSLATORS "PSON" should not be translated
      Puppet.warning(_("Downgrading to PSON for future requests"))
      Puppet[:preferred_serialization_format] = 'pson'
    end
  end
end

#head(request) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/puppet/indirector/rest.rb', line 153

def head(request)
  Puppet.deprecation_warning(_("Puppet::Indirector::Rest#head is deprecated. Use Puppet::HTTP::Client instead."))
  response = do_request(request) do |req|
    http_head(req, IndirectedRoutes.request_to_uri(req), headers)
  end

  if is_http_200?(response)
    true
  else
    false
  end
end

#headersObject

Provide appropriate headers.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/puppet/indirector/rest.rb', line 63

def headers
  # yaml is not allowed on the network
  network_formats = model.supported_formats - EXCLUDED_FORMATS
  mime_types = network_formats.map { |f| model.get_format(f).mime }
  common_headers = {
    "Accept"                                     => mime_types.join(', '),
    Puppet::Network::HTTP::HEADER_PUPPET_VERSION => Puppet.version
  }

  add_accept_encoding(common_headers)
end

#http_delete(request, path, headers = nil, *args) ⇒ Object



101
102
103
# File 'lib/puppet/indirector/rest.rb', line 101

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) ⇒ Object



89
90
91
# File 'lib/puppet/indirector/rest.rb', line 89

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) ⇒ Object



97
98
99
# File 'lib/puppet/indirector/rest.rb', line 97

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) ⇒ Object



93
94
95
# File 'lib/puppet/indirector/rest.rb', line 93

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) ⇒ Object



105
106
107
# File 'lib/puppet/indirector/rest.rb', line 105

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) ⇒ Object



109
110
111
112
# File 'lib/puppet/indirector/rest.rb', line 109

def http_request(method, request, *args)
  conn = network(request)
  conn.send(method, *args)
end

#network(request) ⇒ Object



82
83
84
85
86
87
# File 'lib/puppet/indirector/rest.rb', line 82

def network(request)
  ssl_context = Puppet.lookup(:ssl_context)
  Puppet::Network::HttpPool.connection(request.server || self.class.server,
                                       request.port || self.class.port,
                                       ssl_context: ssl_context)
end

#save(request) ⇒ Object

Raises:

  • (ArgumentError)


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/puppet/indirector/rest.rb', line 196

def save(request)
  Puppet.deprecation_warning(_("Puppet::Indirector::Rest#save is deprecated. Use Puppet::HTTP::Client instead."))
  raise ArgumentError, _("PUT does not accept options") unless request.options.empty?

  response = do_request(request) do |req|
    http_put(req, IndirectedRoutes.request_to_uri(req), req.instance.render, headers.merge({ "Content-Type" => req.instance.mime }))
  end

  if is_http_200?(response)
    content_type, body = parse_response(response)
    deserialize_save(content_type, body)
  else
    nil
  end
end

#search(request) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/puppet/indirector/rest.rb', line 166

def search(request)
  Puppet.deprecation_warning(_("Puppet::Indirector::Rest#search is deprecated. Use Puppet::HTTP::Client instead."))
  response = do_request(request) do |req|
    http_get(req, IndirectedRoutes.request_to_uri(req), headers)
  end

  if is_http_200?(response)
    content_type, body = parse_response(response)
    deserialize_search(content_type, body) || []
  else
    []
  end
end

#use_http_client?Boolean

Should we use puppet’s http client to make requests. Will return false when running in puppetserver

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/puppet/indirector/rest.rb', line 57

def use_http_client?
  Puppet::Network::HttpPool.http_client_class == Puppet::Network::HTTP::Connection ||
    Puppet::Network::HttpPool.http_client_class == Puppet::Network::HTTP::ConnectionAdapter
end

#validate_key(request) ⇒ Object



241
242
243
# File 'lib/puppet/indirector/rest.rb', line 241

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