Class: Chef::DataCollector::ResourceReport

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/data_collector/resource_report.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_resource, action, current_resource = nil) ⇒ ResourceReport

Returns a new instance of ResourceReport.



28
29
30
31
32
33
# File 'lib/chef/data_collector/resource_report.rb', line 28

def initialize(new_resource, action, current_resource = nil)
  @new_resource     = new_resource
  @action           = action
  @current_resource = current_resource
  @status           = "unprocessed"
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



25
26
27
# File 'lib/chef/data_collector/resource_report.rb', line 25

def action
  @action
end

#conditionalObject

Returns the value of attribute conditional.



26
27
28
# File 'lib/chef/data_collector/resource_report.rb', line 26

def conditional
  @conditional
end

#current_resourceObject

Returns the value of attribute current_resource.



26
27
28
# File 'lib/chef/data_collector/resource_report.rb', line 26

def current_resource
  @current_resource
end

#elapsed_timeObject (readonly)

Returns the value of attribute elapsed_time.



25
26
27
# File 'lib/chef/data_collector/resource_report.rb', line 25

def elapsed_time
  @elapsed_time
end

#exceptionObject

Returns the value of attribute exception.



26
27
28
# File 'lib/chef/data_collector/resource_report.rb', line 26

def exception
  @exception
end

#new_resourceObject (readonly)

Returns the value of attribute new_resource.



25
26
27
# File 'lib/chef/data_collector/resource_report.rb', line 25

def new_resource
  @new_resource
end

#statusObject (readonly)

Returns the value of attribute status.



25
26
27
# File 'lib/chef/data_collector/resource_report.rb', line 25

def status
  @status
end

Instance Method Details

#elapsed_time_in_millisecondsObject



58
59
60
# File 'lib/chef/data_collector/resource_report.rb', line 58

def elapsed_time_in_milliseconds
  elapsed_time.nil? ? nil : (elapsed_time * 1000).to_i
end

#failed(exception) ⇒ Object



44
45
46
47
48
# File 'lib/chef/data_collector/resource_report.rb', line 44

def failed(exception)
  @current_resource = nil
  @status           = "failed"
  @exception        = exception
end

#finishObject



54
55
56
# File 'lib/chef/data_collector/resource_report.rb', line 54

def finish
  @elapsed_time = new_resource.elapsed_time
end

#potentially_changed?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/chef/data_collector/resource_report.rb', line 62

def potentially_changed?
  %w{updated failed}.include?(status)
end

#skipped(conditional) ⇒ Object



35
36
37
38
# File 'lib/chef/data_collector/resource_report.rb', line 35

def skipped(conditional)
  @status      = "skipped"
  @conditional = conditional
end

#to_hashObject Also known as: to_h, for_json



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chef/data_collector/resource_report.rb', line 66

def to_hash
  hash = {
    "type"           => new_resource.resource_name.to_sym,
    "name"           => new_resource.name.to_s,
    "id"             => new_resource.identity.to_s,
    "after"          => new_resource.state_for_resource_reporter,
    "before"         => current_resource ? current_resource.state_for_resource_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

#up_to_dateObject



50
51
52
# File 'lib/chef/data_collector/resource_report.rb', line 50

def up_to_date
  @status = "up-to-date"
end

#updatedObject



40
41
42
# File 'lib/chef/data_collector/resource_report.rb', line 40

def updated
  @status = "updated"
end