Class: Puppet::Indirector::REST
- Includes:
- Network::HTTP::API::V1
- Defined in:
- lib/puppet/indirector/rest.rb
Overview
Access objects via REST
Direct Known Subclasses
FileBucketFile::Rest, FileContent::Rest, FileMetadata::Rest, Puppet::Indirector::ResourceType::Rest, Status::Rest, Node::Facts::Rest, Node::Rest, Resource::Catalog::Rest, Resource::Rest, Run::Rest, SSL::Certificate::Rest, SSL::CertificateRequest::Rest, SSL::CertificateRevocationList::Rest, Transaction::Report::Rest
Constant Summary
Constants included from Network::HTTP::API::V1
Network::HTTP::API::V1::METHOD_MAP
Class Attribute Summary collapse
-
.port_setting ⇒ Object
readonly
Returns the value of attribute port_setting.
-
.server_setting ⇒ Object
readonly
Returns the value of attribute server_setting.
Attributes included from Util::Docs
Class Method Summary collapse
- .port ⇒ Object
- .server ⇒ Object
-
.use_port_setting(setting) ⇒ Object
Specify the setting that we should use to get the port.
-
.use_server_setting(setting) ⇒ Object
Specify the setting that we should use to get the server name.
Instance Method Summary collapse
- #convert_to_http_error(response) ⇒ Object
-
#deserialize(response, multiple = false) ⇒ Object
Figure out the content type, turn that into a format, and use the format to extract the body of the response.
- #destroy(request) ⇒ Object
- #find(request) ⇒ Object
- #head(request) ⇒ Object
-
#headers ⇒ Object
Provide appropriate headers.
- #network(request) ⇒ Object
- #save(request) ⇒ Object
- #search(request) ⇒ Object
Methods included from Network::HTTP::API::V1
#indirection2uri, #indirection_method, #plurality, #pluralize, #uri2indirection
Methods inherited from Terminus
abstract_terminus?, const2name, #indirection, indirection_name, inherited, #initialize, mark_as_abstract_terminus, #model, model, #name, name2const, register_terminus_class, terminus_class, terminus_classes, #terminus_type
Methods included from Util::InstanceLoader
#instance_docs, #instance_hash, #instance_load, #instance_loader, #instance_loading?, #loaded_instance, #loaded_instances
Methods included from Util
activerecord_version, benchmark, chuser, classproxy, #execfail, #execpipe, execute, logmethods, memory, proxy, recmkdir, secure_open, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, which, withumask
Methods included from Util::POSIX
#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid
Methods included from Util::Docs
#desc, #dochook, #doctable, #nodoc?, #pad, scrub
Constructor Details
This class inherits a constructor from Puppet::Indirector::Terminus
Class Attribute Details
.port_setting ⇒ Object (readonly)
Returns the value of attribute port_setting.
14 15 16 |
# File 'lib/puppet/indirector/rest.rb', line 14 def port_setting @port_setting end |
.server_setting ⇒ Object (readonly)
Returns the value of attribute server_setting.
14 15 16 |
# File 'lib/puppet/indirector/rest.rb', line 14 def server_setting @server_setting end |
Class Method Details
.port ⇒ Object
31 32 33 |
# File 'lib/puppet/indirector/rest.rb', line 31 def self.port Puppet.settings[port_setting || :masterport].to_i end |
.server ⇒ Object
22 23 24 |
# File 'lib/puppet/indirector/rest.rb', line 22 def self.server Puppet.settings[server_setting || :server] end |
.use_port_setting(setting) ⇒ Object
Specify the setting that we should use to get the port.
27 28 29 |
# File 'lib/puppet/indirector/rest.rb', line 27 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.
18 19 20 |
# File 'lib/puppet/indirector/rest.rb', line 18 def self.use_server_setting(setting) @server_setting = setting end |
Instance Method Details
#convert_to_http_error(response) ⇒ Object
60 61 62 63 |
# File 'lib/puppet/indirector/rest.rb', line 60 def convert_to_http_error(response) = "Error #{response.code} on SERVER: #{(response.body||'').empty? ? response. : uncompress_body(response)}" Net::HTTPError.new(, response) end |
#deserialize(response, multiple = false) ⇒ Object
Figure out the content type, turn that into a format, and use the format to extract the body of the response.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/puppet/indirector/rest.rb', line 37 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) ⇒ Object
100 101 102 103 |
# File 'lib/puppet/indirector/rest.rb', line 100 def destroy(request) raise ArgumentError, "DELETE does not accept options" unless request..empty? deserialize network(request).delete(indirection2uri(request), headers) end |
#find(request) ⇒ Object
74 75 76 77 78 |
# File 'lib/puppet/indirector/rest.rb', line 74 def find(request) return nil unless result = deserialize(network(request).get(indirection2uri(request), headers)) result.name = request.key if result.respond_to?(:name=) result end |
#head(request) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/puppet/indirector/rest.rb', line 80 def head(request) response = network(request).head(indirection2uri(request), headers) 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 ⇒ Object
Provide appropriate headers.
66 67 68 |
# File 'lib/puppet/indirector/rest.rb', line 66 def headers add_accept_encoding({"Accept" => model.supported_formats.join(", ")}) end |
#network(request) ⇒ Object
70 71 72 |
# File 'lib/puppet/indirector/rest.rb', line 70 def network(request) Puppet::Network::HttpPool.http_instance(request.server || self.class.server, request.port || self.class.port) end |
#save(request) ⇒ Object
105 106 107 108 |
# File 'lib/puppet/indirector/rest.rb', line 105 def save(request) raise ArgumentError, "PUT does not accept options" unless request..empty? deserialize network(request).put(indirection2uri(request), request.instance.render, headers.merge({ "Content-Type" => request.instance.mime })) end |
#search(request) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/puppet/indirector/rest.rb', line 93 def search(request) unless result = deserialize(network(request).get(indirection2uri(request), headers), true) return [] end result end |