class Chef::DataCollector::ResourceReport

Attributes

action[R]
conditional[RW]
current_resource[RW]
elapsed_time[R]
exception[RW]
new_resource[R]
status[R]

Public Class Methods

new(new_resource, action, current_resource = nil) click to toggle source
# File lib/chef/data_collector/resource_report.rb, line 30
def initialize(new_resource, action, current_resource = nil)
  @new_resource     = new_resource
  @action           = action
  @current_resource = current_resource
  @status           = "unprocessed"
end

Public Instance Methods

current_resource_state_reporter() click to toggle source
# File lib/chef/data_collector/resource_report.rb, line 116
def current_resource_state_reporter
  current_resource ? current_resource.state_for_resource_reporter : {}
rescue
  {}
end
elapsed_time_in_milliseconds() click to toggle source
# File lib/chef/data_collector/resource_report.rb, line 60
def elapsed_time_in_milliseconds
  elapsed_time.nil? ? nil : (elapsed_time * 1000).to_i
end
failed(exception) click to toggle source
# File lib/chef/data_collector/resource_report.rb, line 46
def failed(exception)
  @current_resource = nil
  @status           = "failed"
  @exception        = exception
end
finish() click to toggle source
# File lib/chef/data_collector/resource_report.rb, line 56
def finish
  @elapsed_time = new_resource.elapsed_time
end
for_json()
Alias for: to_hash
new_resource_state_reporter() click to toggle source
# File lib/chef/data_collector/resource_report.rb, line 110
def new_resource_state_reporter
  new_resource.state_for_resource_reporter
rescue
  {}
end
potentially_changed?() click to toggle source
# File lib/chef/data_collector/resource_report.rb, line 64
def potentially_changed?
  %w{updated failed}.include?(status)
end
resource_identity() click to toggle source

We should be able to call the identity of a resource safely, but there is an edge case where resources that have a lazy property that is both the name_property and the identity property, it will thow a validation exception causing the chef-client run to fail. We are not fixing this case since Chef is actually doing the right thing but we are making the ResourceReporter smarter so that it detects the failure and sends a message to the data collector containing a static resource identity since we were unable to generate a proper one.

# File lib/chef/data_collector/resource_report.rb, line 104
def resource_identity
  new_resource.identity.to_s
rescue => e
  "unknown identity (due to #{e.class})"
end
skipped(conditional) click to toggle source
# File lib/chef/data_collector/resource_report.rb, line 37
def skipped(conditional)
  @status      = "skipped"
  @conditional = conditional
end
to_h()
Alias for: to_hash
to_hash() click to toggle source
# File lib/chef/data_collector/resource_report.rb, line 68
def to_hash
  hash = {
    "type"           => new_resource.resource_name.to_sym,
    "name"           => new_resource.name.to_s,
    "id"             => resource_identity,
    "after"          => new_resource_state_reporter,
    "before"         => current_resource_state_reporter,
    "duration"       => elapsed_time_in_milliseconds.to_s,
    "delta"          => new_resource.respond_to?(:diff) && potentially_changed? ? new_resource.diff : "",
    "ignore_failure" => new_resource.ignore_failure,
    "result"         => action.to_s,
    "status"         => status,
  }

  if new_resource.cookbook_name
    hash["cookbook_name"]    = new_resource.cookbook_name
    hash["cookbook_version"] = new_resource.cookbook_version.version
    hash["recipe_name"]      = new_resource.recipe_name
  end

  hash["conditional"] = conditional.to_text if status == "skipped"
  hash["error_message"] = exception.message unless exception.nil?

  hash
end
Also aliased as: to_h, for_json
up_to_date() click to toggle source
# File lib/chef/data_collector/resource_report.rb, line 52
def up_to_date
  @status = "up-to-date"
end
updated() click to toggle source
# File lib/chef/data_collector/resource_report.rb, line 42
def updated
  @status = "updated"
end