Class: Puppet::Rails::Resource

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Benchmark, Util::CollectionMerger, Util::ReferenceSerializer
Defined in:
lib/puppet/rails/resource.rb

Overview

API:

  • public

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Benchmark

#accumulate_benchmark, #debug_benchmark, #log_accumulated_marks, #railsmark, #time_debug?, #write_benchmarks

Methods included from Util::ReferenceSerializer

#serialize_value, #unserialize_value

Methods included from Util::CollectionMerger

#ar_hash_merge

Class Method Details

.rails_resource_initial_args(resource) ⇒ Object

Determine the basic details on the resource.

API:

  • public



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/puppet/rails/resource.rb', line 28

def self.rails_resource_initial_args(resource)
  result = [:type, :title, :line].inject({}) do |hash, param|
    # 'type' isn't a valid column name, so we have to use another name.
    to = (param == :type) ? :restype : param
    if value = resource.send(param)
      hash[to] = value
    end
    hash
  end

  # We always want a value here, regardless of what the resource has,
  # so we break it out separately.
  result[:exported] = resource.exported || false

  result
end

.tagsObject

API:

  • public



23
24
25
# File 'lib/puppet/rails/resource.rb', line 23

def self.tags
  @tags
end

Instance Method Details

#[](param) ⇒ Object

API:

  • public



86
87
88
89
90
91
92
# File 'lib/puppet/rails/resource.rb', line 86

def [](param)
  if param == 'id'
    super
  else
    super || parameter(param)
  end
end

#add_param_to_list(param) ⇒ Object

API:

  • public



70
71
72
# File 'lib/puppet/rails/resource.rb', line 70

def add_param_to_list(param)
  params_list << param
end

#add_resource_tag(tag) ⇒ Object

API:

  • public



45
46
47
48
# File 'lib/puppet/rails/resource.rb', line 45

def add_resource_tag(tag)
  pt = Puppet::Rails::PuppetTag.accumulate_by_name(tag)
  resource_tags.build(:puppet_tag => pt)
end

#add_tag_to_list(tag) ⇒ Object

API:

  • public



82
83
84
# File 'lib/puppet/rails/resource.rb', line 82

def add_tag_to_list(tag)
  tags_list << tag
end

#fileObject

API:

  • public



50
51
52
# File 'lib/puppet/rails/resource.rb', line 50

def file
  (f = self.source_file) ? f.filename : nil
end

#file=(file) ⇒ Object

API:

  • public



54
55
56
# File 'lib/puppet/rails/resource.rb', line 54

def file=(file)
  self.source_file = Puppet::Rails::SourceFile.find_or_create_by_filename(file)
end

#merge_attributes(resource) ⇒ Object

API:

  • public



102
103
104
105
106
107
108
109
110
# File 'lib/puppet/rails/resource.rb', line 102

def merge_attributes(resource)
  args = self.class.rails_resource_initial_args(resource)
  args.each do |param, value|
    self[param] = value unless resource[param] == value
  end

  # Handle file specially
  self.file = resource.file if (resource.file and  (!resource.file or self.file != resource.file))
end

#merge_parameters(resource) ⇒ Object

API:

  • public



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/puppet/rails/resource.rb', line 112

def merge_parameters(resource)
  catalog_params = {}
  resource.each do |param, value|
    catalog_params[param.to_s] = value
  end

  db_params = {}

  deletions = []
  params_list.each do |value|
    # First remove any parameters our catalog resource doesn't have at all.
    deletions << value['id'] and next unless catalog_params.include?(value['name'])

    # Now store them for later testing.
    db_params[value['name']] ||= []
    db_params[value['name']] << value
  end

  # Now get rid of any parameters whose value list is different.
  # This might be extra work in cases where an array has added or lost
  # a single value, but in the most common case (a single value has changed)
  # this makes sense.
  db_params.each do |name, value_hashes|
    values = value_hashes.collect { |v| v['value'] }

    value_hashes.each { |v| deletions << v['id'] } unless value_compare(catalog_params[name], values)
  end

  # Perform our deletions.
  Puppet::Rails::ParamValue.delete(deletions) unless deletions.empty?

  # Lastly, add any new parameters.
  catalog_params.each do |name, value|
    next if db_params.include?(name) && ! db_params[name].find{ |val| deletions.include?( val["id"] ) }
    values = value.is_a?(Array) ? value : [value]

    values.each do |v|
      param_values.build(:value => serialize_value(v), :line => resource.line, :param_name => Puppet::Rails::ParamName.accumulate_by_name(name))
    end
  end
end

#merge_parser_resource(resource) ⇒ Object

Make sure this resource is equivalent to the provided Parser resource.

API:

  • public



95
96
97
98
99
100
# File 'lib/puppet/rails/resource.rb', line 95

def merge_parser_resource(resource)
  accumulate_benchmark("Individual resource merger", :attributes) { merge_attributes(resource) }
  accumulate_benchmark("Individual resource merger", :parameters) { merge_parameters(resource) }
  accumulate_benchmark("Individual resource merger", :tags) { merge_tags(resource) }
  save
end

#merge_tags(resource) ⇒ Object

Make sure the tag list is correct.

API:

  • public



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/puppet/rails/resource.rb', line 155

def merge_tags(resource)
  in_db = []
  deletions = []
  resource_tags = resource.tags
  tags_list.each do |tag|
    deletions << tag['id'] and next unless resource_tags.include?(tag['name'])
    in_db << tag['name']
  end
  Puppet::Rails::ResourceTag.delete(deletions) unless deletions.empty?

  (resource_tags - in_db).each do |tag|
    add_resource_tag(tag)
  end
end

#nameObject

API:

  • public



176
177
178
# File 'lib/puppet/rails/resource.rb', line 176

def name
  ref
end

#parameter(param) ⇒ Object

API:

  • public



180
181
182
183
184
# File 'lib/puppet/rails/resource.rb', line 180

def parameter(param)
  if pn = param_names.find_by_name(param)
    return (pv = param_values.find(:first, :conditions => [ 'param_name_id = ?', pn])) ? pv.value : nil
  end
end

#params_listObject

API:

  • public



62
63
64
# File 'lib/puppet/rails/resource.rb', line 62

def params_list
  @params_list ||= []
end

#params_list=(params) ⇒ Object

API:

  • public



66
67
68
# File 'lib/puppet/rails/resource.rb', line 66

def params_list=(params)
  @params_list = params
end

#ref(dummy_argument = :work_arround_for_ruby_GC_bug) ⇒ Object

API:

  • public



186
187
188
# File 'lib/puppet/rails/resource.rb', line 186

def ref(dummy_argument=:work_arround_for_ruby_GC_bug)
  "#{self[:restype].split("::").collect { |s| s.capitalize }.join("::")}[#{self.title}]"
end

#tags_listObject

API:

  • public



74
75
76
# File 'lib/puppet/rails/resource.rb', line 74

def tags_list
  @tags_list ||= []
end

#tags_list=(tags) ⇒ Object

API:

  • public



78
79
80
# File 'lib/puppet/rails/resource.rb', line 78

def tags_list=(tags)
  @tags_list = tags
end

#titleObject

API:

  • public



58
59
60
# File 'lib/puppet/rails/resource.rb', line 58

def title
  unserialize_value(self[:title])
end

#to_hashObject

Returns a hash of parameter names and values, no ActiveRecord instances.

API:

  • public



191
192
193
194
195
196
197
# File 'lib/puppet/rails/resource.rb', line 191

def to_hash
  Puppet::Rails::ParamValue.find_all_params_from_resource(self).inject({}) do |hash, value|
    hash[value['name']] ||= []
    hash[value['name']] << value.value
    hash
  end
end

#to_resource(scope) ⇒ Object

Convert our object to a resource. Do not retain whether the object is exported, though, since that would cause it to get stripped from the configuration.

API:

  • public



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
# File 'lib/puppet/rails/resource.rb', line 202

def to_resource(scope)
  hash = self.attributes
  hash["type"] = hash["restype"]
  hash.delete("restype")

  # FIXME At some point, we're going to want to retain this information
  # for logging and auditing.
  hash.delete("host_id")
  hash.delete("updated_at")
  hash.delete("source_file_id")
  hash.delete("created_at")
  hash.delete("id")
  hash.each do |p, v|
    hash.delete(p) if v.nil?
  end
  hash[:scope] = scope
  hash[:source] = scope.source
  hash[:parameters] = []
  names = []
  self.param_names.each do |pname|
    # We can get the same name multiple times because of how the
    # db layout works.
    next if names.include?(pname.name)
    names << pname.name
    hash[:parameters] << pname.to_resourceparam(self, scope.source)
  end
  obj = Puppet::Parser::Resource.new(hash.delete("type"), hash.delete("title"), hash)

  # Store the ID, so we can check if we're re-collecting the same resource.
  obj.collector_id = self.id

  obj
end

#value_compare(v, db_value) ⇒ Object

API:

  • public



170
171
172
173
174
# File 'lib/puppet/rails/resource.rb', line 170

def value_compare(v,db_value)
  v = [v] unless v.is_a?(Array)

  v == db_value
end