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.



30
31
32
33
34
35
# 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

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



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

def action
  @action
end

#conditionalObject

Returns the value of attribute conditional.



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

def conditional
  @conditional
end

#current_resourceObject

Returns the value of attribute current_resource.



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

def current_resource
  @current_resource
end

#elapsed_timeObject (readonly)

Returns the value of attribute elapsed_time.



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

def elapsed_time
  @elapsed_time
end

#exceptionObject

Returns the value of attribute exception.



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

def exception
  @exception
end

#new_resourceObject (readonly)

Returns the value of attribute new_resource.



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

def new_resource
  @new_resource
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#current_resource_state_reporterObject



116
117
118
119
120
# 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_millisecondsObject



60
61
62
# 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) ⇒ Object



46
47
48
49
50
# File 'lib/chef/data_collector/resource_report.rb', line 46

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

#finishObject



56
57
58
# File 'lib/chef/data_collector/resource_report.rb', line 56

def finish
  @elapsed_time = new_resource.elapsed_time
end

#new_resource_state_reporterObject



110
111
112
113
114
# 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?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/chef/data_collector/resource_report.rb', line 64

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

#resource_identityObject

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.



104
105
106
107
108
# 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) ⇒ Object



37
38
39
40
# File 'lib/chef/data_collector/resource_report.rb', line 37

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

#to_hashObject Also known as: to_h, for_json



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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

#up_to_dateObject



52
53
54
# File 'lib/chef/data_collector/resource_report.rb', line 52

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

#updatedObject



42
43
44
# File 'lib/chef/data_collector/resource_report.rb', line 42

def updated
  @status = "updated"
end