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 63 def initialize(indirection_name, method, key, instance, options = {}) 64 @instance = instance 65 options ||= {} 66 67 self.indirection_name = indirection_name 68 self.method = method 69 70 options = options.inject({}) { |hash, ary| hash[ary[0].to_sym] = ary[1]; hash } 71 72 set_attributes(options) 73 74 @options = options 75 76 if key 77 # If the request key is a URI, then we need to treat it specially, 78 # because it rewrites the key. We could otherwise strip server/port/etc 79 # info out in the REST class, but it seemed bad design for the REST 80 # class to rewrite the key. 81 82 if key.to_s =~ /^\w+:\// and not Puppet::Util.absolute_path?(key.to_s) # it's a URI 83 set_uri_key(key) 84 else 85 @key = key 86 end 87 end 88 89 @key = @instance.name if ! @key and @instance 90 end
Public Instance Methods
Is this an authenticated request?
# File lib/puppet/indirector/request.rb 26 def authenticated? 27 # Double negative, so we just get true or false 28 ! ! authenticated 29 end
# File lib/puppet/indirector/request.rb 140 def description 141 return(uri ? uri : "/#{indirection_name}/#{key}") 142 end
# File lib/puppet/indirector/request.rb 31 def environment 32 # If environment has not been set directly, we should use the application's 33 # current environment 34 @environment ||= Puppet.lookup(:current_environment) 35 end
# File lib/puppet/indirector/request.rb 37 def environment=(env) 38 @environment = 39 if env.is_a?(Puppet::Node::Environment) 40 env 41 else 42 Puppet.lookup(:environments).get!(env) 43 end 44 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 51 def ignore_cache? 52 ignore_cache 53 end
# File lib/puppet/indirector/request.rb 55 def ignore_cache_save? 56 ignore_cache_save 57 end
# File lib/puppet/indirector/request.rb 59 def ignore_terminus? 60 ignore_terminus 61 end
Look up the indirection based on the name provided.
# File lib/puppet/indirector/request.rb 93 def indirection 94 Puppet::Indirector::Indirection.instance(indirection_name) 95 end
# File lib/puppet/indirector/request.rb 97 def indirection_name=(name) 98 @indirection_name = name.to_sym 99 end
# File lib/puppet/indirector/request.rb 112 def initialize_from_hash(hash) 113 @indirection_name = hash['indirection_name'].to_sym 114 @method = hash['method'].to_sym 115 @key = hash['key'] 116 @instance = hash['instance'] 117 @options = hash['options'] 118 end
# File lib/puppet/indirector/request.rb 101 def model 102 ind = indirection 103 raise ArgumentError, _("Could not find indirection '%{indirection}'") % { indirection: indirection_name } unless ind 104 ind.model 105 end
Are we trying to interact with multiple resources, or just one?
# File lib/puppet/indirector/request.rb 108 def plural? 109 method == :search 110 end
# File lib/puppet/indirector/request.rb 144 def remote? 145 self.node or self.ip 146 end
# File lib/puppet/indirector/request.rb 120 def to_data_hash 121 { 'indirection_name' => @indirection_name.to_s, 122 'method' => @method.to_s, 123 'key' => @key, 124 'instance' => @instance, 125 'options' => @options } 126 end
# File lib/puppet/indirector/request.rb 128 def to_hash 129 result = options.dup 130 131 OPTION_ATTRIBUTES.each do |attribute| 132 value = send(attribute) 133 if value 134 result[attribute] = value 135 end 136 end 137 result 138 end
Private Instance Methods
# File lib/puppet/indirector/request.rb 150 def set_attributes(options) 151 OPTION_ATTRIBUTES.each do |attribute| 152 if options.include?(attribute.to_sym) 153 send(attribute.to_s + "=", options[attribute]) 154 options.delete(attribute) 155 end 156 end 157 end
Parse the key as a URI, setting attributes appropriately.
# File lib/puppet/indirector/request.rb 160 def set_uri_key(key) 161 @uri = key 162 begin 163 # calling uri_encode for UTF-8 characters will % escape them and keep them UTF-8 164 uri = URI.parse(Puppet::Util.uri_encode(key)) 165 rescue => detail 166 raise ArgumentError, _("Could not understand URL %{key}: %{detail}") % { key: key, detail: detail }, detail.backtrace 167 end 168 169 # Just short-circuit these to full paths 170 if uri.scheme == "file" 171 @key = Puppet::Util.uri_to_path(uri) 172 return 173 end 174 175 @server = uri.host if uri.host && !uri.host.empty? 176 177 # If the URI class can look up the scheme, it will provide a port, 178 # otherwise it will default to '0'. 179 if uri.port.to_i == 0 and uri.scheme == "puppet" 180 @port = Puppet.settings[:serverport].to_i 181 else 182 @port = uri.port.to_i 183 end 184 185 # filebucket:// is only used internally to pass request details 186 # from Dipper objects to the indirector. The wire always uses HTTPS. 187 if uri.scheme == 'filebucket' 188 @protocol = 'https' 189 else 190 @protocol = uri.scheme 191 end 192 193 @key = Puppet::Util.uri_unescape(uri.path.sub(/^\//, '')) 194 end