Class: Puppet::Indirector::Request

Inherits:
Object
  • Object
show all
Includes:
Util::PsychSupport
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, :instance, :environment]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::PsychSupport

#encode_with, #init_with

Constructor Details

#initialize(indirection_name, method, key, instance, options = {}) ⇒ Request

Returns a new instance of Request.



61
62
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
# File 'lib/puppet/indirector/request.rb', line 61

def initialize(indirection_name, method, key, instance, options = {})
  @instance = instance
  options ||= {}

  self.indirection_name = indirection_name
  self.method = method

  options = options.inject({}) { |hash, ary| hash[ary[0].to_sym] = ary[1]; hash }

  set_attributes(options)

  @options = 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

#authenticatedObject



13
14
15
# File 'lib/puppet/indirector/request.rb', line 13

def authenticated
  @authenticated
end

#ignore_cacheObject



13
14
15
# File 'lib/puppet/indirector/request.rb', line 13

def ignore_cache
  @ignore_cache
end

#ignore_terminusObject



13
14
15
# File 'lib/puppet/indirector/request.rb', line 13

def ignore_terminus
  @ignore_terminus
end

#indirection_nameObject



17
18
19
# File 'lib/puppet/indirector/request.rb', line 17

def indirection_name
  @indirection_name
end

#instanceObject



13
14
15
# File 'lib/puppet/indirector/request.rb', line 13

def instance
  @instance
end

#ipObject



13
14
15
# File 'lib/puppet/indirector/request.rb', line 13

def ip
  @ip
end

#keyObject



13
14
15
# File 'lib/puppet/indirector/request.rb', line 13

def key
  @key
end

#methodObject



13
14
15
# File 'lib/puppet/indirector/request.rb', line 13

def method
  @method
end

#nodeObject



13
14
15
# File 'lib/puppet/indirector/request.rb', line 13

def node
  @node
end

#optionsObject



13
14
15
# File 'lib/puppet/indirector/request.rb', line 13

def options
  @options
end

#portObject



15
16
17
# File 'lib/puppet/indirector/request.rb', line 15

def port
  @port
end

#protocolObject



15
16
17
# File 'lib/puppet/indirector/request.rb', line 15

def protocol
  @protocol
end

#serverObject



15
16
17
# File 'lib/puppet/indirector/request.rb', line 15

def server
  @server
end

#uriObject



15
16
17
# File 'lib/puppet/indirector/request.rb', line 15

def uri
  @uri
end

Instance Method Details

#authenticated?Boolean

Is this an authenticated request?

Returns:

  • (Boolean)


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

def authenticated?
  # Double negative, so we just get true or false
  ! ! authenticated
end

#descriptionObject



177
178
179
# File 'lib/puppet/indirector/request.rb', line 177

def description
  return(uri ? uri : "/#{indirection_name}/#{key}")
end

#do_request(srv_service = :puppet, default_server = , default_port = , &block) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/puppet/indirector/request.rb', line 181

def do_request(srv_service=:puppet, default_server=Puppet.settings[:server], default_port=Puppet.settings[:masterport], &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]
    Puppet::Network::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}: #{e.message}"
      end
    end
  end

  # ... Fall back onto the default server.
  Puppet.debug "No more servers left, falling back to #{default_server}:#{default_port}" if Puppet.settings[:use_srv_records]
  self.server = default_server
  self.port   = default_port
  return yield(self)
end

#encode_params(params) ⇒ Object



144
145
146
147
148
# File 'lib/puppet/indirector/request.rb', line 144

def encode_params(params)
  params.collect do |key, value|
    "#{key}=#{CGI.escape(value.to_s)}"
  end.join("&")
end

#environmentObject



29
30
31
32
33
# File 'lib/puppet/indirector/request.rb', line 29

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



35
36
37
38
39
40
41
42
# File 'lib/puppet/indirector/request.rb', line 35

def environment=(env)
  @environment =
  if env.is_a?(Puppet::Node::Environment)
    env
  else
    Puppet.lookup(:environments).get!(env)
  end
end

#escaped_keyObject



44
45
46
# File 'lib/puppet/indirector/request.rb', line 44

def escaped_key
  URI.escape(key)
end

#expand_into_parameters(data) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/puppet/indirector/request.rb', line 115

def expand_into_parameters(data)
  data.inject([]) do |params, key_value|
    key, value = key_value

    expanded_value = case value
                     when Array
                       value.collect { |val| [key, val] }
                     else
                       [key_value]
                     end

    params.concat(expand_primitive_types_into_parameters(expanded_value))
  end
end

#expand_primitive_types_into_parameters(data) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/puppet/indirector/request.rb', line 130

def expand_primitive_types_into_parameters(data)
  data.inject([]) do |params, key_value|
    key, value = key_value
    case value
    when nil
      params
    when true, false, String, Symbol, Fixnum, Bignum, Float
      params << [key, value]
    else
      raise ArgumentError, "HTTP REST queries cannot handle values of type '#{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.

Returns:

  • (Boolean)


53
54
55
# File 'lib/puppet/indirector/request.rb', line 53

def ignore_cache?
  ignore_cache
end

#ignore_terminus?Boolean

Returns:

  • (Boolean)


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

def ignore_terminus?
  ignore_terminus
end

#indirectionObject

Look up the indirection based on the name provided.



91
92
93
# File 'lib/puppet/indirector/request.rb', line 91

def indirection
  Puppet::Indirector::Indirection.instance(indirection_name)
end

#initialize_from_hash(hash) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/puppet/indirector/request.rb', line 150

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

#modelObject

Raises:

  • (ArgumentError)


99
100
101
102
# File 'lib/puppet/indirector/request.rb', line 99

def model
  raise ArgumentError, "Could not find indirection '#{indirection_name}'" unless i = indirection
  i.model
end

#plural?Boolean

Are we trying to interact with multiple resources, or just one?

Returns:

  • (Boolean)


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

def plural?
  method == :search
end

#query_stringObject

Create the query string, if options are present.



110
111
112
113
# File 'lib/puppet/indirector/request.rb', line 110

def query_string
  return "" if options.nil? || options.empty?
  encode_params(expand_into_parameters(options.to_a))
end

#remote?Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/puppet/indirector/request.rb', line 206

def remote?
  self.node or self.ip
end

#to_data_hashObject



158
159
160
161
162
163
164
# File 'lib/puppet/indirector/request.rb', line 158

def to_data_hash
  { 'indirection_name' => @indirection_name.to_s,
    'method' => @method.to_s,
    'key' => @key,
    'instance' => @instance,
    'options' => @options }
end

#to_hashObject



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

def to_hash
  result = options.dup

  OPTION_ATTRIBUTES.each do |attribute|
    if value = send(attribute)
      result[attribute] = value
    end
  end
  result
end