Class: Puppet::ResourceApi::BaseContext
- Inherits:
-
Object
- Object
- Puppet::ResourceApi::BaseContext
- Defined in:
- lib/puppet/resource_api/base_context.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #attribute_changed(title, attribute, is, should, message: nil) ⇒ Object
- #device ⇒ Object
- #failed(titles, message: 'Updating has failed') ⇒ Object
- #failed? ⇒ Boolean
- #failing(titles, message: 'Failing') ⇒ Object
- #feature_support?(feature) ⇒ Boolean
-
#initialize(definition) ⇒ BaseContext
constructor
A new instance of BaseContext.
- #log_exception(exception, message: 'Error encountered', trace: false) ⇒ Object
- #processed(title, is, should) ⇒ Object
- #processing(title, is, should, message: 'Processing') ⇒ Object
- #send_log(_level, _message) ⇒ Object
Constructor Details
#initialize(definition) ⇒ BaseContext
Returns a new instance of BaseContext.
8 9 10 11 12 |
# File 'lib/puppet/resource_api/base_context.rb', line 8 def initialize(definition) raise ArgumentError, 'BaseContext requires definition to be a Hash' unless definition.is_a?(Hash) @typename = definition[:name] @type = Puppet::ResourceApi::TypeDefinition.new(definition) end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/puppet/resource_api/base_context.rb', line 6 def type @type end |
Instance Method Details
#attribute_changed(title, attribute, is, should, message: nil) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/puppet/resource_api/base_context.rb', line 100 def attribute_changed(title, attribute, is, should, message: nil) raise "#{__method__} only accepts a single resource title" if title.respond_to?(:each) printable_is = 'nil' printable_should = 'nil' if is printable_is = is.is_a?(Numeric) ? is : "'#{is}'" end if should printable_should = should.is_a?(Numeric) ? should : "'#{should}'" end notice("#{title}: attribute '#{attribute}' changed from #{printable_is} to #{printable_should}#{message ? ": #{message}" : ''}") end |
#device ⇒ Object
14 15 16 |
# File 'lib/puppet/resource_api/base_context.rb', line 14 def device raise 'Received device() on an unprepared BaseContext. Use a PuppetContext instead.' end |
#failed(titles, message: 'Updating has failed') ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/puppet/resource_api/base_context.rb', line 113 def failed(titles, message: 'Updating has failed') setup_context(titles) begin err() # raise message ensure @context = nil end end |
#failed? ⇒ Boolean
18 19 20 |
# File 'lib/puppet/resource_api/base_context.rb', line 18 def failed? @failed end |
#failing(titles, message: 'Failing') ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/puppet/resource_api/base_context.rb', line 58 def failing(titles, message: 'Failing') start_time = Time.now setup_context(titles, ) begin debug('Start') yield warning("Finished failing in #{format_seconds(Time.now - start_time)} seconds") rescue StandardError => e log_exception(e, message: "Failed after #{format_seconds(Time.now - start_time)} seconds") @failed = true ensure @context = nil end end |
#feature_support?(feature) ⇒ Boolean
22 23 24 25 |
# File 'lib/puppet/resource_api/base_context.rb', line 22 def feature_support?(feature) Puppet.deprecation_warning('context.feature_support? is deprecated. Please use context.type.feature? instead.') type.feature?(feature) end |
#log_exception(exception, message: 'Error encountered', trace: false) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/puppet/resource_api/base_context.rb', line 123 def log_exception(exception, message: 'Error encountered', trace: false) = "#{message}: #{exception}" if trace += "\n" += exception.backtrace.join("\n") end err() end |
#processed(title, is, should) ⇒ Object
95 96 97 98 |
# File 'lib/puppet/resource_api/base_context.rb', line 95 def processed(title, is, should) raise "#{__method__} only accepts a single resource title" if title.respond_to?(:each) notice("Processed #{title} from #{is} to #{should}") end |
#processing(title, is, should, message: 'Processing') ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/puppet/resource_api/base_context.rb', line 73 def processing(title, is, should, message: 'Processing') raise "#{__method__} only accepts a single resource title" if title.respond_to?(:each) start_time = Time.now setup_context(title, ) begin debug("Starting processing of #{title} from #{is} to #{should}") yield notice("Finished processing #{title} in #{format_seconds(Time.now - start_time)} seconds: #{should}") rescue StandardError => e log_exception(e, message: "Failed processing #{title} after #{format_seconds(Time.now - start_time)} seconds") @failed = true ensure @context = nil end end |
#send_log(_level, _message) ⇒ Object
132 133 134 |
# File 'lib/puppet/resource_api/base_context.rb', line 132 def send_log(_level, ) raise 'Received send_log() on an unprepared BaseContext. Use IOContext, or PuppetContext instead.' end |