Class: Puppet::Indirector::Indirection

Inherits:
Object
  • Object
show all
Includes:
Util::Docs, Util::MethodHelper
Defined in:
lib/puppet/indirector/indirection.rb

Overview

The class that connects functional classes with their different collection back-ends. Each indirection has a set of associated terminus classes, each of which is a subclass of Puppet::Indirector::Terminus.

Constant Summary collapse

@@indirections =
[]

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Instance Attribute Summary collapse

Attributes included from Util::Docs

#nodoc

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Docs

#desc, #dochook, #doctable, #markdown_definitionlist, #markdown_header, #nodoc?, #pad, scrub

Methods included from Util::MethodHelper

#requiredopts, #set_options, #symbolize_options

Constructor Details

#initialize(model, name, options = {}) ⇒ Indirection

Returns a new instance of Indirection.

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/puppet/indirector/indirection.rb', line 91

def initialize(model, name, options = {})
  @model = model
  @name = name
  @termini = {}

  @cache_class = nil
  @terminus_class = nil

  raise(ArgumentError, "Indirection #{@name} is already defined") if @@indirections.find { |i| i.name == @name }
  @@indirections << self

  @indirected_class = options.delete(:indirected_class)
  if mod = options[:extend]
    extend(mod)
    options.delete(:extend)
  end

  # This is currently only used for cache_class and terminus_class.
  set_options(options)
end

Instance Attribute Details

#cache_classObject



49
50
51
# File 'lib/puppet/indirector/indirection.rb', line 49

def cache_class
  @cache_class
end

#modelObject



14
15
16
# File 'lib/puppet/indirector/indirection.rb', line 14

def model
  @model
end

#nameObject



14
15
16
# File 'lib/puppet/indirector/indirection.rb', line 14

def name
  @name
end

#terminiObject (readonly)



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

def termini
  @termini
end

#terminus_settingObject

This can be used to select the terminus class.



126
127
128
# File 'lib/puppet/indirector/indirection.rb', line 126

def terminus_setting
  @terminus_setting
end

Class Method Details

.instance(name) ⇒ Object

Find an indirection by name. This is provided so that Terminus classes can specifically hook up with the indirections they are associated with.



21
22
23
# File 'lib/puppet/indirector/indirection.rb', line 21

def self.instance(name)
  @@indirections.find { |i| i.name == name }
end

.instancesObject

Return a list of all known indirections. Used to generate the reference.



27
28
29
# File 'lib/puppet/indirector/indirection.rb', line 27

def self.instances
  @@indirections.collect { |i| i.name }
end

.model(name) ⇒ Object

Find an indirected model by name. This is provided so that Terminus classes can specifically hook up with the indirections they are associated with.



33
34
35
36
# File 'lib/puppet/indirector/indirection.rb', line 33

def self.model(name)
  return nil unless match = @@indirections.find { |i| i.name == name }
  match.model
end

Instance Method Details

#allow_remote_requests?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/puppet/indirector/indirection.rb', line 176

def allow_remote_requests?
  terminus.allow_remote_requests?
end

#cacheObject

Create and return our cache terminus.

Raises:



39
40
41
42
# File 'lib/puppet/indirector/indirection.rb', line 39

def cache
  raise(Puppet::DevError, "Tried to cache when no cache class was set") unless cache_class
  terminus(cache_class)
end

#cache?Boolean

Should we use a cache?

Returns:

  • (Boolean)


45
46
47
# File 'lib/puppet/indirector/indirection.rb', line 45

def cache?
  cache_class ? true : false
end

#deleteObject

This is only used for testing.



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

def delete
  @@indirections.delete(self) if @@indirections.include?(self)
end

#destroy(key, options = {}) ⇒ Object

Remove something via the terminus.



246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/puppet/indirector/indirection.rb', line 246

def destroy(key, options={})
  request = request(:destroy, key, nil, options)
  terminus = prepare(request)

  result = terminus.destroy(request)

  if cache? and cache.find(request(:find, key, nil, options))
    # Reuse the existing request, since it's equivalent.
    cache.destroy(request)
  end

  result
end

#docObject

Generate the full doc string.



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/puppet/indirector/indirection.rb', line 78

def doc
  text = ""

  text << scrub(@doc) << "\n\n" if @doc

  text << "* **Indirected Class**: `#{@indirected_class}`\n";
  if terminus_setting
    text << "* **Terminus Setting**: #{terminus_setting}\n"
  end

  text
end

#expirationObject

Calculate the expiration date for a returned instance.



73
74
75
# File 'lib/puppet/indirector/indirection.rb', line 73

def expiration
  Time.now + ttl
end

#expire(key, options = {}) ⇒ Object

Expire a cached object, if one is cached. Note that we don’t actually remove it, we expire it and write it back out to disk. This way people can still use the expired object if they want.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/puppet/indirector/indirection.rb', line 161

def expire(key, options={})
  request = request(:expire, key, nil, options)

  return nil unless cache?

  return nil unless instance = cache.find(request(:find, key, nil, options))

  Puppet.info "Expiring the #{self.name} cache of #{instance.name}"

  # Set an expiration date in the past
  instance.expiration = Time.now - 60

  cache.save(request(:save, nil, instance, options))
end

#find(key, options = {}) ⇒ Object

Search for an instance in the appropriate terminus, caching the results if caching is configured..



182
183
184
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
# File 'lib/puppet/indirector/indirection.rb', line 182

def find(key, options={})
  request = request(:find, key, nil, options)
  terminus = prepare(request)

  result = find_in_cache(request)
  if not result.nil?
    result
  elsif request.ignore_terminus?
    nil
  else
    # Otherwise, return the result from the terminus, caching if
    # appropriate.
    result = terminus.find(request)
    if not result.nil?
      result.expiration ||= self.expiration if result.respond_to?(:expiration)
      if cache?
        Puppet.info "Caching #{self.name} for #{request.key}"
        begin
          cache.save request(:save, key, result, options)
        rescue => detail
          Puppet.log_exception(detail)
          raise detail
        end
      end

      filtered = result
      if terminus.respond_to?(:filter)
        Puppet::Util::Profiler.profile("Filtered result for #{self.name} #{request.key}", [:indirector, :filter, self.name, request.key]) do
          filtered = terminus.filter(result)
        end
      end

      filtered
    end
  end
end

#find_in_cache(request) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/puppet/indirector/indirection.rb', line 230

def find_in_cache(request)
  # See if our instance is in the cache and up to date.
  return nil unless cache? and ! request.ignore_cache? and cached = cache.find(request)
  if cached.expired?
    Puppet.info "Not using expired #{self.name} for #{request.key} from cache; expired at #{cached.expiration}"
    return nil
  end

  Puppet.debug "Using cached #{self.name} for #{request.key}"
  cached
rescue => detail
  Puppet.log_exception(detail, "Cached #{self.name} for #{request.key} failed: #{detail}")
  nil
end

#head(key, options = {}) ⇒ Object

Search for an instance in the appropriate terminus, and return a boolean indicating whether the instance was found.



221
222
223
224
225
226
227
228
# File 'lib/puppet/indirector/indirection.rb', line 221

def head(key, options={})
  request = request(:head, key, nil, options)
  terminus = prepare(request)

  # Look in the cache first, then in the terminus.  Force the result
  # to be a boolean.
  !!(find_in_cache(request) || terminus.head(request))
end

#request(*args) ⇒ Object

Set up our request object.



113
114
115
# File 'lib/puppet/indirector/indirection.rb', line 113

def request(*args)
  Puppet::Indirector::Request.new(self.name, *args)
end

#reset_terminus_classObject



140
141
142
# File 'lib/puppet/indirector/indirection.rb', line 140

def reset_terminus_class
  @terminus_class = nil
end

#save(instance, key = nil, options = {}) ⇒ Object

Save the instance in the appropriate terminus. This method is normally an instance method on the indirected class.



277
278
279
280
281
282
283
284
285
286
287
# File 'lib/puppet/indirector/indirection.rb', line 277

def save(instance, key = nil, options={})
  request = request(:save, key, instance, options)
  terminus = prepare(request)

  result = terminus.save(request)

  # If caching is enabled, save our document there
  cache.save(request) if cache?

  result
end

#search(key, options = {}) ⇒ Object

Search for more than one instance. Should always return an array.



261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/puppet/indirector/indirection.rb', line 261

def search(key, options={})
  request = request(:search, key, nil, options)
  terminus = prepare(request)

  if result = terminus.search(request)
    raise Puppet::DevError, "Search results from terminus #{terminus.name} are not an array" unless result.is_a?(Array)
    result.each do |instance|
      next unless instance.respond_to? :expiration
      instance.expiration ||= self.expiration
    end
    return result
  end
end

#terminus(terminus_name = nil) ⇒ Object

Return the singleton terminus for this indirection.

Raises:



118
119
120
121
122
123
# File 'lib/puppet/indirector/indirection.rb', line 118

def terminus(terminus_name = nil)
  # Get the name of the terminus.
  raise Puppet::DevError, "No terminus specified for #{self.name}; cannot redirect" unless terminus_name ||= terminus_class

  termini[terminus_name] ||= make_terminus(terminus_name)
end

#terminus_classObject

Determine the terminus class.



129
130
131
132
133
134
135
136
137
138
# File 'lib/puppet/indirector/indirection.rb', line 129

def terminus_class
  unless @terminus_class
    if setting = self.terminus_setting
      self.terminus_class = Puppet.settings[setting]
    else
      raise Puppet::DevError, "No terminus class nor terminus setting was provided for indirection #{self.name}"
    end
  end
  @terminus_class
end

#terminus_class=(klass) ⇒ Object

Specify the terminus class to use.



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

def terminus_class=(klass)
  validate_terminus_class(klass)
  @terminus_class = klass
end

#ttlObject

Default to the runinterval for the ttl.



68
69
70
# File 'lib/puppet/indirector/indirection.rb', line 68

def ttl
  @ttl ||= Puppet[:runinterval]
end

#ttl=(value) ⇒ Object

Set the time-to-live for instances created through this indirection.

Raises:

  • (ArgumentError)


62
63
64
65
# File 'lib/puppet/indirector/indirection.rb', line 62

def ttl=(value)
  raise ArgumentError, "Indirection TTL must be an integer" unless value.is_a?(Fixnum)
  @ttl = value
end

#validate_terminus_class(terminus_class) ⇒ Object

This is used by terminus_class= and cache=.

Raises:

  • (ArgumentError)


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

def validate_terminus_class(terminus_class)
  raise ArgumentError, "Invalid terminus name #{terminus_class.inspect}" unless terminus_class and terminus_class.to_s != ""
  unless Puppet::Indirector::Terminus.terminus_class(self.name, terminus_class)
    raise ArgumentError, "Could not find terminus #{terminus_class} for indirection #{self.name}"
  end
end