Class: Bolt::Result
- Inherits:
-
Object
- Object
- Bolt::Result
- Defined in:
- lib/bolt/result.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
- ._pcore_init_from_hash ⇒ Object
- .for_command(target, stdout, stderr, exit_code, action, command) ⇒ Object
- .for_task(target, stdout, stderr, exit_code, task) ⇒ Object
- .for_upload(target, source, destination) ⇒ Object
-
.from_asserted_args(target, value) ⇒ Object
Satisfies the Puppet datatypes API.
- .from_exception(target, exception) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #_pcore_init_from_hash(init_hash) ⇒ Object
- #_pcore_init_hash ⇒ Object
- #eql?(other) ⇒ Boolean
-
#error ⇒ Object
Warning: This will fail outside of a compilation.
-
#error_hash ⇒ Object
This allows access to errors outside puppet compilation it should be prefered over error in bolt code.
- #generic_value ⇒ Object
-
#initialize(target, error: nil, message: nil, value: nil, action: nil, object: nil) ⇒ Result
constructor
A new instance of Result.
- #message ⇒ Object
- #ok? ⇒ Boolean (also: #ok, #success?)
- #status ⇒ Object
- #status_hash ⇒ Object
- #to_data ⇒ Object
- #to_json(opts = nil) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(target, error: nil, message: nil, value: nil, action: nil, object: nil) ⇒ Result
Returns a new instance of Result.
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/bolt/result.rb', line 93 def initialize(target, error: nil, message: nil, value: nil, action: nil, object: nil) @target = target @value = value || {} @action = action @object = object @value_set = !value.nil? if error && !error.is_a?(Hash) raise "TODO: how did we get a string error" end @value['_error'] = error if error @value['_output'] = if end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
8 9 10 |
# File 'lib/bolt/result.rb', line 8 def action @action end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
8 9 10 |
# File 'lib/bolt/result.rb', line 8 def object @object end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
8 9 10 |
# File 'lib/bolt/result.rb', line 8 def target @target end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
8 9 10 |
# File 'lib/bolt/result.rb', line 8 def value @value end |
Class Method Details
._pcore_init_from_hash ⇒ Object
75 76 77 |
# File 'lib/bolt/result.rb', line 75 def self._pcore_init_from_hash raise "Result shouldn't be instantiated from a pcore_init class method. How did this get called?" end |
.for_command(target, stdout, stderr, exit_code, action, command) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bolt/result.rb', line 25 def self.for_command(target, stdout, stderr, exit_code, action, command) value = { 'stdout' => stdout, 'stderr' => stderr, 'exit_code' => exit_code } unless exit_code == 0 value['_error'] = { 'kind' => 'puppetlabs.tasks/command-error', 'issue_code' => 'COMMAND_ERROR', 'msg' => "The command failed with exit code #{exit_code}", 'details' => { 'exit_code' => exit_code } } end new(target, value: value, action: action, object: command) end |
.for_task(target, stdout, stderr, exit_code, task) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/bolt/result.rb', line 42 def self.for_task(target, stdout, stderr, exit_code, task) begin value = JSON.parse(stdout) unless value.is_a? Hash value = nil end rescue JSON::ParserError value = nil end value ||= { '_output' => stdout } if exit_code != 0 && value['_error'].nil? msg = if stdout.empty? "The task failed with exit code #{exit_code}:\n#{stderr}" else "The task failed with exit code #{exit_code}" end value['_error'] = { 'kind' => 'puppetlabs.tasks/task-error', 'issue_code' => 'TASK_ERROR', 'msg' => msg, 'details' => { 'exit_code' => exit_code } } end new(target, value: value, action: 'task', object: task) end |
.for_upload(target, source, destination) ⇒ Object
66 67 68 |
# File 'lib/bolt/result.rb', line 66 def self.for_upload(target, source, destination) new(target, message: "Uploaded '#{source}' to '#{target.host}:#{destination}'", action: 'upload', object: source) end |
.from_asserted_args(target, value) ⇒ Object
Satisfies the Puppet datatypes API
71 72 73 |
# File 'lib/bolt/result.rb', line 71 def self.from_asserted_args(target, value) new(target, value: value) end |
.from_exception(target, exception) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/bolt/result.rb', line 10 def self.from_exception(target, exception) if exception.is_a?(Bolt::Error) error = exception.to_h else error = { 'kind' => 'puppetlabs.tasks/exception-error', 'issue_code' => 'EXCEPTION', 'msg' => exception., 'details' => { 'class' => exception.class.to_s } } error['details']['stack_trace'] = exception.backtrace.join('\n') if exception.backtrace end Result.new(target, error: error) end |
Instance Method Details
#==(other) ⇒ Object
139 140 141 |
# File 'lib/bolt/result.rb', line 139 def ==(other) eql?(other) end |
#[](key) ⇒ Object
135 136 137 |
# File 'lib/bolt/result.rb', line 135 def [](key) value[key] end |
#_pcore_init_from_hash(init_hash) ⇒ Object
79 80 81 82 |
# File 'lib/bolt/result.rb', line 79 def _pcore_init_from_hash(init_hash) opts = init_hash.reject { |k, _v| k == 'target' } initialize(init_hash['target'], opts.map { |k, v| [k.to_sym, v] }.to_h) end |
#_pcore_init_hash ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/bolt/result.rb', line 84 def _pcore_init_hash { 'target' => @target, 'error' => @value['_error'], 'message' => @value['_output'], 'value' => @value, 'action' => @action, 'object' => @object } end |
#eql?(other) ⇒ Boolean
129 130 131 132 133 |
# File 'lib/bolt/result.rb', line 129 def eql?(other) self.class == other.class && target == other.target && value == other.value end |
#error ⇒ Object
Warning: This will fail outside of a compilation. Use error_hash inside bolt. Is it crazy for this to behave differently outside a compiler?
174 175 176 177 178 179 |
# File 'lib/bolt/result.rb', line 174 def error if error_hash Puppet::DataTypes::Error.from_asserted_hash(error_hash) end end |
#error_hash ⇒ Object
This allows access to errors outside puppet compilation it should be prefered over error in bolt code
167 168 169 |
# File 'lib/bolt/result.rb', line 167 def error_hash value['_error'] end |
#generic_value ⇒ Object
123 124 125 126 127 |
# File 'lib/bolt/result.rb', line 123 def generic_value if @value_set value.reject { |k, _| %w[_error _output].include? k } end end |
#message ⇒ Object
106 107 108 |
# File 'lib/bolt/result.rb', line 106 def @value['_output'] end |
#ok? ⇒ Boolean Also known as: ok, success?
159 160 161 |
# File 'lib/bolt/result.rb', line 159 def ok? error_hash.nil? end |
#status ⇒ Object
155 156 157 |
# File 'lib/bolt/result.rb', line 155 def status ok? ? 'success' : 'failure' end |
#status_hash ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/bolt/result.rb', line 110 def status_hash # DEPRECATION: node in status hashes is deprecated and should be removed in 2.0 base = { target: @target.name, action: action, object: object, status: status } # rubocop:disable Style/GlobalVars $future ? base.merge(value: @value) : base.merge(result: @value, node: @target.name) # rubocop:enable Style/GlobalVars end |
#to_data ⇒ Object
151 152 153 |
# File 'lib/bolt/result.rb', line 151 def to_data Bolt::Util.walk_keys(status_hash, &:to_s) end |
#to_json(opts = nil) ⇒ Object
143 144 145 |
# File 'lib/bolt/result.rb', line 143 def to_json(opts = nil) status_hash.to_json(opts) end |
#to_s ⇒ Object
147 148 149 |
# File 'lib/bolt/result.rb', line 147 def to_s to_json end |