Class: Puppet::ResourceApi::BaseContext
- Inherits:
-
Object
- Object
- Puppet::ResourceApi::BaseContext
- Defined in:
- lib/puppet/resource_api/base_context.rb
Overview
This class provides access to all common external dependencies of providers and transports. The runtime environment will inject an appropriate implementation.
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
- #reset_failed ⇒ Object
- #send_log(_level, _message) ⇒ Object
- #transport ⇒ Object
Constructor Details
#initialize(definition) ⇒ BaseContext
Returns a new instance of BaseContext.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/puppet/resource_api/base_context.rb', line 15 def initialize(definition) if definition.is_a?(Hash) # this is only for backwards compatibility @type = Puppet::ResourceApi::TypeDefinition.new(definition) elsif definition.is_a? Puppet::ResourceApi::BaseTypeDefinition @type = definition else raise ArgumentError, 'BaseContext requires definition to be a child of Puppet::ResourceApi::BaseTypeDefinition, not <%{actual_type}>' % { actual_type: definition.class } end end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
13 14 15 |
# File 'lib/puppet/resource_api/base_context.rb', line 13 def type @type end |
Instance Method Details
#attribute_changed(title, attribute, is, should, message: nil) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/puppet/resource_api/base_context.rb', line 120 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}#{ ? ": #{}" : ''}") end |
#device ⇒ Object
26 27 28 |
# File 'lib/puppet/resource_api/base_context.rb', line 26 def device raise 'Received device() on an unprepared BaseContext. Use a PuppetContext instead.' end |
#failed(titles, message: 'Updating has failed') ⇒ Object
133 134 135 136 137 138 139 140 141 |
# File 'lib/puppet/resource_api/base_context.rb', line 133 def failed(titles, message: 'Updating has failed') setup_context(titles) begin err() # raise message ensure @context = nil end end |
#failed? ⇒ Boolean
34 35 36 |
# File 'lib/puppet/resource_api/base_context.rb', line 34 def failed? @failed end |
#failing(titles, message: 'Failing') ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/puppet/resource_api/base_context.rb', line 78 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
42 43 44 45 |
# File 'lib/puppet/resource_api/base_context.rb', line 42 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
143 144 145 146 147 148 149 150 |
# File 'lib/puppet/resource_api/base_context.rb', line 143 def log_exception(exception, message: 'Error encountered', trace: false) = "#{}: #{exception}" if trace += "\n" += exception.backtrace.join("\n") end err() end |
#processed(title, is, should) ⇒ Object
115 116 117 118 |
# File 'lib/puppet/resource_api/base_context.rb', line 115 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
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/puppet/resource_api/base_context.rb', line 93 def processing(title, is, should, = '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 |
#reset_failed ⇒ Object
38 39 40 |
# File 'lib/puppet/resource_api/base_context.rb', line 38 def reset_failed @failed = false end |
#send_log(_level, _message) ⇒ Object
152 153 154 |
# File 'lib/puppet/resource_api/base_context.rb', line 152 def send_log(_level, ) raise 'Received send_log() on an unprepared BaseContext. Use IOContext, or PuppetContext instead.' end |
#transport ⇒ Object
30 31 32 |
# File 'lib/puppet/resource_api/base_context.rb', line 30 def transport raise 'No transport available.' end |