~ubuntu-branches/ubuntu/trusty/puppet/trusty

« back to all changes in this revision

Viewing changes to lib/puppet/indirector/resource/active_record.rb

  • Committer: Package Import Robot
  • Author(s): Stig Sandbeck Mathisen
  • Date: 2011-10-22 14:08:22 UTC
  • mfrom: (1.1.25) (3.1.32 sid)
  • Revision ID: package-import@ubuntu.com-20111022140822-odxde5lohc45yhuz
Tags: 2.7.6-1
* New upstream release (CVE-2011-3872)
* Remove cherry-picked "groupadd_aix_warning" patch
* Install all new manpages

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'puppet/indirector/active_record'
 
2
 
 
3
class Puppet::Resource::ActiveRecord < Puppet::Indirector::ActiveRecord
 
4
  def search(request)
 
5
    type   = request_to_type_name(request)
 
6
    host   = request.options[:host]
 
7
    filter = request.options[:filter]
 
8
 
 
9
    if filter and filter[1] =~ /^(and|or)$/i then
 
10
      raise Puppet::Error, "Complex search on StoreConfigs resources is not supported"
 
11
    end
 
12
 
 
13
    query = build_active_record_query(type, host, filter)
 
14
    Puppet::Rails::Resource.find(:all, query)
 
15
  end
 
16
 
 
17
  private
 
18
  def request_to_type_name(request)
 
19
    request.key.split('/', 2)[0] or
 
20
      raise "No key found in the request, failing: #{request.inspect}"
 
21
  end
 
22
 
 
23
  def filter_to_active_record(filter)
 
24
    # Don't call me if you don't have a filter, please.
 
25
    filter.is_a?(Array) or raise ArgumentError, "active record filters must be arrays"
 
26
    a, op, b = filter
 
27
 
 
28
    case op
 
29
    when /^(and|or)$/i then
 
30
      extra = []
 
31
      first, args = filter_to_active_record a
 
32
      extra += args
 
33
 
 
34
      second, args = filter_to_active_record b
 
35
      extra += args
 
36
 
 
37
      return "(#{first}) #{op.upcase} (#{second})", extra
 
38
 
 
39
    when "==", "!=" then
 
40
      op = '=' if op == '=='    # SQL, yayz!
 
41
      case a
 
42
      when "title" then
 
43
        return "title #{op} ?", [b]
 
44
 
 
45
      when "tag" then
 
46
        return "puppet_tags.name #{op} ?", [b]
 
47
 
 
48
      else
 
49
        return "param_names.name = ? AND param_values.value #{op} ?", [a, b]
 
50
      end
 
51
 
 
52
    else
 
53
      raise ArgumentError, "unknown operator #{op.inspect} in #{filter.inspect}"
 
54
    end
 
55
  end
 
56
 
 
57
  def build_active_record_query(type, host, filter)
 
58
    raise Puppet::DevError, "Cannot collect resources for a nil host" unless host
 
59
 
 
60
    search = "(exported=? AND restype=?)"
 
61
    arguments = [true, type]
 
62
 
 
63
    if filter then
 
64
      sql, values = filter_to_active_record(filter)
 
65
      search    += " AND #{sql}"
 
66
      arguments += values
 
67
    end
 
68
 
 
69
    # note: we're not eagerly including any relations here because it can
 
70
    # create large numbers of objects that we will just throw out later.  We
 
71
    # used to eagerly include param_names/values but the way the search filter
 
72
    # is built ruined those efforts and we were eagerly loading only the
 
73
    # searched parameter and not the other ones.
 
74
    query = {}
 
75
    case search
 
76
    when /puppet_tags/
 
77
      query = { :joins => { :resource_tags => :puppet_tag } }
 
78
    when /param_name/
 
79
      query = { :joins => { :param_values => :param_name } }
 
80
    end
 
81
 
 
82
    # We're going to collect objects from rails, but we don't want any
 
83
    # objects from this host.
 
84
    if host = Puppet::Rails::Host.find_by_name(host)
 
85
      search += " AND (host_id != ?)"
 
86
      arguments << host.id
 
87
    end
 
88
 
 
89
    query[:conditions] = [search, *arguments]
 
90
 
 
91
    query
 
92
  end
 
93
end