class Puppet::Indirector::Request
This class encapsulates all of the information you need to make an Indirection call, and as a result also handles REST calls. It's somewhat analogous to an HTTP Request
object, except tuned for our Indirector.
Constants
- OPTION_ATTRIBUTES
trusted_information is specifically left out because we can't serialize it and keep it “trusted”
Attributes
Public Class Methods
# File lib/puppet/indirector/request.rb 62 def initialize(indirection_name, method, key, instance, options = {}) 63 @instance = instance 64 options ||= {} 65 66 self.indirection_name = indirection_name 67 self.method = method 68 69 options = options.inject({}) { |hash, ary| hash[ary[0].to_sym] = ary[1]; hash } 70 71 set_attributes(options) 72 73 @options = options 74 75 if key 76 # If the request key is a URI, then we need to treat it specially, 77 # because it rewrites the key. We could otherwise strip server/port/etc 78 # info out in the REST class, but it seemed bad design for the REST 79 # class to rewrite the key. 80 81 if key.to_s =~ /^\w+:\// and not Puppet::Util.absolute_path?(key.to_s) # it's a URI 82 set_uri_key(key) 83 else 84 @key = key 85 end 86 end 87 88 @key = @instance.name if ! @key and @instance 89 end
Public Instance Methods
Is this an authenticated request?
# File lib/puppet/indirector/request.rb 25 def authenticated? 26 # Double negative, so we just get true or false 27 ! ! authenticated 28 end
# File lib/puppet/indirector/request.rb 139 def description 140 return(uri ? uri : "/#{indirection_name}/#{key}") 141 end
# File lib/puppet/indirector/request.rb 30 def environment 31 # If environment has not been set directly, we should use the application's 32 # current environment 33 @environment ||= Puppet.lookup(:current_environment) 34 end
# File lib/puppet/indirector/request.rb 36 def environment=(env) 37 @environment = 38 if env.is_a?(Puppet::Node::Environment) 39 env 40 else 41 Puppet.lookup(:environments).get!(env) 42 end 43 end
LAK:NOTE This is a messy interface to the cache, and it's only used by the Configurer class. I decided it was better to implement it now and refactor later, when we have a better design, than to spend another month coming up with a design now that might not be any better.
# File lib/puppet/indirector/request.rb 50 def ignore_cache? 51 ignore_cache 52 end
# File lib/puppet/indirector/request.rb 54 def ignore_cache_save? 55 ignore_cache_save 56 end
# File lib/puppet/indirector/request.rb 58 def ignore_terminus? 59 ignore_terminus 60 end
Look up the indirection based on the name provided.
# File lib/puppet/indirector/request.rb 92 def indirection 93 Puppet::Indirector::Indirection.instance(indirection_name) 94 end
# File lib/puppet/indirector/request.rb 96 def indirection_name=(name) 97 @indirection_name = name.to_sym 98 end
# File lib/puppet/indirector/request.rb 111 def initialize_from_hash(hash) 112 @indirection_name = hash['indirection_name'].to_sym 113 @method = hash['method'].to_sym 114 @key = hash['key'] 115 @instance = hash['instance'] 116 @options = hash['options'] 117 end
# File lib/puppet/indirector/request.rb 100 def model 101 ind = indirection 102 raise ArgumentError, _("Could not find indirection '%{indirection}'") % { indirection: indirection_name } unless ind 103 ind.model 104 end
Are we trying to interact with multiple resources, or just one?
# File lib/puppet/indirector/request.rb 107 def plural? 108 method == :search 109 end
# File lib/puppet/indirector/request.rb 143 def remote? 144 self.node or self.ip 145 end
# File lib/puppet/indirector/request.rb 119 def to_data_hash 120 { 'indirection_name' => @indirection_name.to_s, 121 'method' => @method.to_s, 122 'key' => @key, 123 'instance' => @instance, 124 'options' => @options } 125 end
# File lib/puppet/indirector/request.rb 127 def to_hash 128 result = options.dup 129 130 OPTION_ATTRIBUTES.each do |attribute| 131 value = send(attribute) 132 if value 133 result[attribute] = value 134 end 135 end 136 result 137 end
Private Instance Methods
# File lib/puppet/indirector/request.rb 149 def set_attributes(options) 150 OPTION_ATTRIBUTES.each do |attribute| 151 if options.include?(attribute.to_sym) 152 send(attribute.to_s + "=", options[attribute]) 153 options.delete(attribute) 154 end 155 end 156 end
Parse the key as a URI, setting attributes appropriately.
# File lib/puppet/indirector/request.rb 159 def set_uri_key(key) 160 @uri = key 161 begin 162 # calling uri_encode for UTF-8 characters will % escape them and keep them UTF-8 163 uri = URI.parse(Puppet::Util.uri_encode(key)) 164 rescue => detail 165 raise ArgumentError, _("Could not understand URL %{key}: %{detail}") % { key: key, detail: detail }, detail.backtrace 166 end 167 168 # Just short-circuit these to full paths 169 if uri.scheme == "file" 170 @key = Puppet::Util.uri_to_path(uri) 171 return 172 end 173 174 @server = uri.host if uri.host && !uri.host.empty? 175 176 # If the URI class can look up the scheme, it will provide a port, 177 # otherwise it will default to '0'. 178 if uri.port.to_i == 0 and uri.scheme == "puppet" 179 @port = Puppet.settings[:serverport].to_i 180 else 181 @port = uri.port.to_i 182 end 183 184 # filebucket:// is only used internally to pass request details 185 # from Dipper objects to the indirector. The wire always uses HTTPS. 186 if uri.scheme == 'filebucket' 187 @protocol = 'https' 188 else 189 @protocol = uri.scheme 190 end 191 192 @key = Puppet::Util.uri_unescape(uri.path.sub(/^\//, '')) 193 end