Class: Bolt::ResourceInstance
- Inherits:
-
Object
- Object
- Bolt::ResourceInstance
- Defined in:
- lib/bolt/resource_instance.rb
Instance Attribute Summary collapse
-
#desired_state ⇒ Object
readonly
Returns the value of attribute desired_state.
-
#events ⇒ Object
Returns the value of attribute events.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
._pcore_init_from_hash(_init_hash) ⇒ Object
Needed by Puppet to serialize with _pcore_init_hash instead of the object’s attributes.
-
._pcore_type ⇒ Object
Needed by Puppet to recognize Bolt::ResourceInstance as a Puppet object when deserializing.
-
.from_asserted_args(target, type, title, state = nil, desired_state = nil, events = nil) ⇒ Object
Creates a ResourceInstance from positional arguments in a plan when calling ResourceInstance.new(target, type, title, …).
-
.from_asserted_hash(resource_hash) ⇒ Object
Creates a ResourceInstance from a data hash in a plan when calling ResourceInstance.new($resource_hash) or $target.set_resources($resource_hash).
Instance Method Summary collapse
- #_pcore_init_from_hash(init_hash) ⇒ Object
- #add_event(event) ⇒ Object
- #assert_hash(loc, value) ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(resource_hash) ⇒ ResourceInstance
constructor
Parameters will already be validated when calling ResourceInstance.new or set_resources() from a plan.
-
#overwrite_desired_state(desired_state) ⇒ Object
rubocop:enable Naming/AccessorMethodName.
-
#overwrite_state(state) ⇒ Object
rubocop:enable Naming/AccessorMethodName.
- #reference ⇒ Object (also: #to_s)
-
#set_desired_state(desired_state) ⇒ Object
rubocop:disable Naming/AccessorMethodName.
-
#set_state(state) ⇒ Object
rubocop:disable Naming/AccessorMethodName.
- #to_hash ⇒ Object (also: #_pcore_init_hash)
- #to_json(opts = nil) ⇒ Object
Constructor Details
#initialize(resource_hash) ⇒ ResourceInstance
Parameters will already be validated when calling ResourceInstance.new or set_resources() from a plan. We don’t perform any validation in the class itself since Puppet will pass an empty hash to the initializer as part of the deserialization process before passing the _pcore_init_hash.
29 30 31 32 33 34 35 36 37 |
# File 'lib/bolt/resource_instance.rb', line 29 def initialize(resource_hash) @target = resource_hash['target'] @type = resource_hash['type'].to_s.capitalize @title = resource_hash['title'] # get_resources() returns observed state under the 'parameters' key @state = resource_hash['state'] || resource_hash['parameters'] || {} @desired_state = resource_hash['desired_state'] || {} @events = resource_hash['events'] || [] end |
Instance Attribute Details
#desired_state ⇒ Object (readonly)
Returns the value of attribute desired_state.
7 8 9 |
# File 'lib/bolt/resource_instance.rb', line 7 def desired_state @desired_state end |
#events ⇒ Object
Returns the value of attribute events.
8 9 10 |
# File 'lib/bolt/resource_instance.rb', line 8 def events @events end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
7 8 9 |
# File 'lib/bolt/resource_instance.rb', line 7 def state @state end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
7 8 9 |
# File 'lib/bolt/resource_instance.rb', line 7 def target @target end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
7 8 9 |
# File 'lib/bolt/resource_instance.rb', line 7 def title @title end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/bolt/resource_instance.rb', line 7 def type @type end |
Class Method Details
._pcore_init_from_hash(_init_hash) ⇒ Object
Needed by Puppet to serialize with _pcore_init_hash instead of the object’s attributes
16 17 18 19 |
# File 'lib/bolt/resource_instance.rb', line 16 def self._pcore_init_from_hash(_init_hash) raise "ResourceInstance shouldn't be instantiated from a pcore_init class method. "\ "How did this get called?" end |
._pcore_type ⇒ Object
Needed by Puppet to recognize Bolt::ResourceInstance as a Puppet object when deserializing
11 12 13 |
# File 'lib/bolt/resource_instance.rb', line 11 def self._pcore_type ResourceInstance end |
.from_asserted_args(target, type, title, state = nil, desired_state = nil, events = nil) ⇒ Object
Creates a ResourceInstance from positional arguments in a plan when calling ResourceInstance.new(target, type, title, …)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/bolt/resource_instance.rb', line 47 def self.from_asserted_args(target, type, title, state = nil, desired_state = nil, events = nil) new( 'target' => target, 'type' => type, 'title' => title, 'state' => state, 'desired_state' => desired_state, 'events' => events ) end |
.from_asserted_hash(resource_hash) ⇒ Object
Creates a ResourceInstance from a data hash in a plan when calling ResourceInstance.new($resource_hash) or $target.set_resources($resource_hash)
41 42 43 |
# File 'lib/bolt/resource_instance.rb', line 41 def self.from_asserted_hash(resource_hash) new(resource_hash) end |
Instance Method Details
#_pcore_init_from_hash(init_hash) ⇒ Object
21 22 23 |
# File 'lib/bolt/resource_instance.rb', line 21 def _pcore_init_from_hash(init_hash) initialize(init_hash) end |
#add_event(event) ⇒ Object
92 93 94 |
# File 'lib/bolt/resource_instance.rb', line 92 def add_event(event) @events << event end |
#assert_hash(loc, value) ⇒ Object
120 121 122 123 124 |
# File 'lib/bolt/resource_instance.rb', line 120 def assert_hash(loc, value) unless value.is_a?(Hash) raise Bolt::ValidationError, "#{loc} must be of type Hash; got #{value.class}" end end |
#eql?(other) ⇒ Boolean Also known as: ==
63 64 65 66 67 68 |
# File 'lib/bolt/resource_instance.rb', line 63 def eql?(other) self.class.equal?(other.class) && target == other.target && type == other.type && title == other.title end |
#overwrite_desired_state(desired_state) ⇒ Object
rubocop:enable Naming/AccessorMethodName
115 116 117 118 |
# File 'lib/bolt/resource_instance.rb', line 115 def overwrite_desired_state(desired_state) assert_hash('desired_state', desired_state) @desired_state = desired_state end |
#overwrite_state(state) ⇒ Object
rubocop:enable Naming/AccessorMethodName
103 104 105 106 |
# File 'lib/bolt/resource_instance.rb', line 103 def overwrite_state(state) assert_hash('state', state) @state = state end |
#reference ⇒ Object Also known as: to_s
87 88 89 |
# File 'lib/bolt/resource_instance.rb', line 87 def reference "#{type}[#{title}]" end |
#set_desired_state(desired_state) ⇒ Object
rubocop:disable Naming/AccessorMethodName
109 110 111 112 |
# File 'lib/bolt/resource_instance.rb', line 109 def set_desired_state(desired_state) assert_hash('desired_state', desired_state) @desired_state.merge!(desired_state) end |
#set_state(state) ⇒ Object
rubocop:disable Naming/AccessorMethodName
97 98 99 100 |
# File 'lib/bolt/resource_instance.rb', line 97 def set_state(state) assert_hash('state', state) @state.merge!(state) end |
#to_hash ⇒ Object Also known as: _pcore_init_hash
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/bolt/resource_instance.rb', line 71 def to_hash { 'target' => target, 'type' => type, 'title' => title, 'state' => state, 'desired_state' => desired_state, 'events' => events } end |
#to_json(opts = nil) ⇒ Object
83 84 85 |
# File 'lib/bolt/resource_instance.rb', line 83 def to_json(opts = nil) to_hash.to_json(opts) end |