Class: Puppet::Indirector::Request
- Includes:
- Util::PsychSupport, Util::Warnings
- Defined in:
- lib/puppet/indirector/request.rb
Overview
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.
Constant Summary collapse
- OPTION_ATTRIBUTES =
trusted_information is specifically left out because we can’t serialize it and keep it “trusted”
[:ip, :node, :authenticated, :ignore_terminus, :ignore_cache, :ignore_cache_save, :instance, :environment]
Instance Attribute Summary collapse
-
#authenticated ⇒ Object
Returns the value of attribute authenticated.
-
#ignore_cache ⇒ Object
Returns the value of attribute ignore_cache.
-
#ignore_cache_save ⇒ Object
Returns the value of attribute ignore_cache_save.
-
#ignore_terminus ⇒ Object
Returns the value of attribute ignore_terminus.
-
#indirection_name ⇒ Object
Returns the value of attribute indirection_name.
-
#instance ⇒ Object
Returns the value of attribute instance.
-
#ip ⇒ Object
Returns the value of attribute ip.
-
#key ⇒ Object
Returns the value of attribute key.
-
#method ⇒ Object
Returns the value of attribute method.
-
#node ⇒ Object
Returns the value of attribute node.
-
#options ⇒ Object
Returns the value of attribute options.
-
#port ⇒ Object
Returns the value of attribute port.
-
#protocol ⇒ Object
Returns the value of attribute protocol.
-
#server ⇒ Object
Returns the value of attribute server.
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
-
#authenticated? ⇒ Boolean
Is this an authenticated request?.
- #description ⇒ Object
- #do_request(srv_service = :puppet, default_server = nil, default_port = nil, &block) ⇒ Object
- #encode_params(params) ⇒ Object
- #environment ⇒ Object
- #environment=(env) ⇒ Object
- #expand_into_parameters(data) ⇒ Object
- #expand_primitive_types_into_parameters(data) ⇒ Object
-
#ignore_cache? ⇒ Boolean
LAK:NOTE This is a messy interface to the cache, and it’s only used by the Configurer class.
- #ignore_cache_save? ⇒ Boolean
- #ignore_terminus? ⇒ Boolean
-
#indirection ⇒ Object
Look up the indirection based on the name provided.
-
#initialize(indirection_name, method, key, instance, options = {}) ⇒ Request
constructor
A new instance of Request.
- #initialize_from_hash(hash) ⇒ Object
- #model ⇒ Object
-
#plural? ⇒ Boolean
Are we trying to interact with multiple resources, or just one?.
-
#query_string ⇒ Object
Create the query string, if options are present.
- #remote? ⇒ Boolean
- #to_data_hash ⇒ Object
- #to_hash ⇒ Object
Methods included from Util::Warnings
clear_warnings, debug_once, maybe_log, notice_once, warnonce
Methods included from Util::PsychSupport
Constructor Details
#initialize(indirection_name, method, key, instance, options = {}) ⇒ Request
Returns a new instance of Request.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/puppet/indirector/request.rb', line 63 def initialize(indirection_name, method, key, instance, = {}) @instance = instance ||= {} self.indirection_name = indirection_name self.method = method = .inject({}) { |hash, ary| hash[ary[0].to_sym] = ary[1]; hash } set_attributes() @options = if key # If the request key is a URI, then we need to treat it specially, # because it rewrites the key. We could otherwise strip server/port/etc # info out in the REST class, but it seemed bad design for the REST # class to rewrite the key. if key.to_s =~ /^\w+:\// and not Puppet::Util.absolute_path?(key.to_s) # it's a URI set_uri_key(key) else @key = key end end @key = @instance.name if ! @key and @instance end |
Instance Attribute Details
#authenticated ⇒ Object
Returns the value of attribute authenticated.
15 16 17 |
# File 'lib/puppet/indirector/request.rb', line 15 def authenticated @authenticated end |
#ignore_cache ⇒ Object
Returns the value of attribute ignore_cache.
15 16 17 |
# File 'lib/puppet/indirector/request.rb', line 15 def ignore_cache @ignore_cache end |
#ignore_cache_save ⇒ Object
Returns the value of attribute ignore_cache_save.
15 16 17 |
# File 'lib/puppet/indirector/request.rb', line 15 def ignore_cache_save @ignore_cache_save end |
#ignore_terminus ⇒ Object
Returns the value of attribute ignore_terminus.
15 16 17 |
# File 'lib/puppet/indirector/request.rb', line 15 def ignore_terminus @ignore_terminus end |
#indirection_name ⇒ Object
Returns the value of attribute indirection_name.
19 20 21 |
# File 'lib/puppet/indirector/request.rb', line 19 def indirection_name @indirection_name end |
#instance ⇒ Object
Returns the value of attribute instance.
15 16 17 |
# File 'lib/puppet/indirector/request.rb', line 15 def instance @instance end |
#ip ⇒ Object
Returns the value of attribute ip.
15 16 17 |
# File 'lib/puppet/indirector/request.rb', line 15 def ip @ip end |
#key ⇒ Object
Returns the value of attribute key.
15 16 17 |
# File 'lib/puppet/indirector/request.rb', line 15 def key @key end |
#method ⇒ Object
Returns the value of attribute method.
15 16 17 |
# File 'lib/puppet/indirector/request.rb', line 15 def method @method end |
#node ⇒ Object
Returns the value of attribute node.
15 16 17 |
# File 'lib/puppet/indirector/request.rb', line 15 def node @node end |
#options ⇒ Object
Returns the value of attribute options.
15 16 17 |
# File 'lib/puppet/indirector/request.rb', line 15 def @options end |
#port ⇒ Object
Returns the value of attribute port.
17 18 19 |
# File 'lib/puppet/indirector/request.rb', line 17 def port @port end |
#protocol ⇒ Object
Returns the value of attribute protocol.
17 18 19 |
# File 'lib/puppet/indirector/request.rb', line 17 def protocol @protocol end |
#server ⇒ Object
Returns the value of attribute server.
17 18 19 |
# File 'lib/puppet/indirector/request.rb', line 17 def server @server end |
#uri ⇒ Object
Returns the value of attribute uri.
17 18 19 |
# File 'lib/puppet/indirector/request.rb', line 17 def uri @uri end |
Instance Method Details
#authenticated? ⇒ Boolean
Is this an authenticated request?
26 27 28 29 |
# File 'lib/puppet/indirector/request.rb', line 26 def authenticated? # Double negative, so we just get true or false ! ! authenticated end |
#description ⇒ Object
181 182 183 |
# File 'lib/puppet/indirector/request.rb', line 181 def description return(uri ? uri : "/#{indirection_name}/#{key}") end |
#do_request(srv_service = :puppet, default_server = nil, default_port = nil, &block) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/puppet/indirector/request.rb', line 185 def do_request(srv_service=:puppet, default_server=nil, default_port=nil, &block) # We were given a specific server to use, so just use that one. # This happens if someone does something like specifying a file # source using a puppet:// URI with a specific server. return yield(self) if !self.server.nil? if Puppet.settings[:use_srv_records] # We may want to consider not creating a new resolver here # every request eventually, to take advantage of the resolver's # caching behavior. resolver = Puppet::Network::Resolver.new resolver.each_srv_record(Puppet.settings[:srv_domain], srv_service) do |srv_server, srv_port| begin self.server = srv_server self.port = srv_port return yield(self) rescue SystemCallError => e Puppet.warning _("Error connecting to %{srv_server}:%{srv_port}: %{message}") % { srv_server: srv_server, srv_port: srv_port, message: e. } end end end if default_server self.server = default_server else self.server = Puppet.lookup(:server) do primary_server = Puppet.settings[:server_list][0] if primary_server #TRANSLATORS 'server_list' is the name of a setting and should not be translated debug_once _("Selected server from first entry of the `server_list` setting: %{server}") % {server: primary_server[0]} primary_server[0] else #TRANSLATORS 'server' is the name of a setting and should not be translated debug_once _("Selected server from the `server` setting: %{server}") % {server: Puppet.settings[:server]} Puppet.settings[:server] end end end if default_port self.port = default_port else self.port = Puppet.lookup(:serverport) do primary_server = Puppet.settings[:server_list][0] if primary_server #TRANSLATORS 'server_list' is the name of a setting and should not be translated debug_once _("Selected port from the first entry of the `server_list` setting: %{port}") % {port: primary_server[1]} primary_server[1] else #TRANSLATORS 'serverport' is the name of a setting and should not be translated debug_once _("Selected port from the `serverport` setting: %{port}") % {port: Puppet.settings[:serverport]} Puppet.settings[:serverport] end end end return yield(self) end |
#encode_params(params) ⇒ Object
147 148 149 150 151 |
# File 'lib/puppet/indirector/request.rb', line 147 def encode_params(params) params.collect do |key, value| "#{key}=#{Puppet::Util.uri_query_encode(value.to_s)}" end.join("&") end |
#environment ⇒ Object
31 32 33 34 35 |
# File 'lib/puppet/indirector/request.rb', line 31 def environment # If environment has not been set directly, we should use the application's # current environment @environment ||= Puppet.lookup(:current_environment) end |
#environment=(env) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/puppet/indirector/request.rb', line 37 def environment=(env) @environment = if env.is_a?(Puppet::Node::Environment) env else Puppet.lookup(:environments).get!(env) end end |
#expand_into_parameters(data) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/puppet/indirector/request.rb', line 118 def (data) data.inject([]) do |params, key_value| key, value = key_value = case value when Array value.collect { |val| [key, val] } else [key_value] end params.concat(()) end end |
#expand_primitive_types_into_parameters(data) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/puppet/indirector/request.rb', line 133 def (data) data.inject([]) do |params, key_value| key, value = key_value case value when nil params when true, false, String, Symbol, Integer, Float params << [key, value] else raise ArgumentError, _("HTTP REST queries cannot handle values of type '%{klass}'") % { klass: value.class } end end end |
#ignore_cache? ⇒ Boolean
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.
51 52 53 |
# File 'lib/puppet/indirector/request.rb', line 51 def ignore_cache? ignore_cache end |
#ignore_cache_save? ⇒ Boolean
55 56 57 |
# File 'lib/puppet/indirector/request.rb', line 55 def ignore_cache_save? ignore_cache_save end |
#ignore_terminus? ⇒ Boolean
59 60 61 |
# File 'lib/puppet/indirector/request.rb', line 59 def ignore_terminus? ignore_terminus end |
#indirection ⇒ Object
Look up the indirection based on the name provided.
93 94 95 |
# File 'lib/puppet/indirector/request.rb', line 93 def indirection Puppet::Indirector::Indirection.instance(indirection_name) end |
#initialize_from_hash(hash) ⇒ Object
153 154 155 156 157 158 159 |
# File 'lib/puppet/indirector/request.rb', line 153 def initialize_from_hash(hash) @indirection_name = hash['indirection_name'].to_sym @method = hash['method'].to_sym @key = hash['key'] @instance = hash['instance'] @options = hash['options'] end |
#model ⇒ Object
101 102 103 104 105 |
# File 'lib/puppet/indirector/request.rb', line 101 def model ind = indirection raise ArgumentError, _("Could not find indirection '%{indirection}'") % { indirection: indirection_name } unless ind ind.model end |
#plural? ⇒ Boolean
Are we trying to interact with multiple resources, or just one?
108 109 110 |
# File 'lib/puppet/indirector/request.rb', line 108 def plural? method == :search end |
#query_string ⇒ Object
Create the query string, if options are present.
113 114 115 116 |
# File 'lib/puppet/indirector/request.rb', line 113 def query_string return "" if .nil? || .empty? encode_params((.to_a)) end |
#remote? ⇒ Boolean
244 245 246 |
# File 'lib/puppet/indirector/request.rb', line 244 def remote? self.node or self.ip end |
#to_data_hash ⇒ Object
161 162 163 164 165 166 167 |
# File 'lib/puppet/indirector/request.rb', line 161 def to_data_hash { 'indirection_name' => @indirection_name.to_s, 'method' => @method.to_s, 'key' => @key, 'instance' => @instance, 'options' => @options } end |
#to_hash ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/puppet/indirector/request.rb', line 169 def to_hash result = .dup OPTION_ATTRIBUTES.each do |attribute| value = send(attribute) if value result[attribute] = value end end result end |