Class: Bolt::ResourceInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/resource_instance.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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
# 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']
  @state         = resource_hash['state'] || {}
  @desired_state = resource_hash['desired_state'] || {}
  @events        = resource_hash['events'] || []
end

Instance Attribute Details

#desired_stateObject (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

#eventsObject

Returns the value of attribute events.



8
9
10
# File 'lib/bolt/resource_instance.rb', line 8

def events
  @events
end

#stateObject (readonly)

Returns the value of attribute state.



7
8
9
# File 'lib/bolt/resource_instance.rb', line 7

def state
  @state
end

#targetObject (readonly)

Returns the value of attribute target.



7
8
9
# File 'lib/bolt/resource_instance.rb', line 7

def target
  @target
end

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/bolt/resource_instance.rb', line 7

def title
  @title
end

#typeObject (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_typeObject

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

.format_reference(type, title) ⇒ Object



86
87
88
# File 'lib/bolt/resource_instance.rb', line 86

def self.format_reference(type, title)
  "#{type.capitalize}[#{title}]"
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, …)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bolt/resource_instance.rb', line 46

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)



40
41
42
# File 'lib/bolt/resource_instance.rb', line 40

def self.from_asserted_hash(resource_hash)
  new(resource_hash)
end

Instance Method Details

#[](attribute) ⇒ Object



95
96
97
# File 'lib/bolt/resource_instance.rb', line 95

def [](attribute)
  @state[attribute]
end

#_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



99
100
101
# File 'lib/bolt/resource_instance.rb', line 99

def add_event(event)
  @events << event
end

#assert_hash(loc, value) ⇒ Object



127
128
129
130
131
# File 'lib/bolt/resource_instance.rb', line 127

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: ==

Returns:

  • (Boolean)


62
63
64
65
66
67
# File 'lib/bolt/resource_instance.rb', line 62

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



122
123
124
125
# File 'lib/bolt/resource_instance.rb', line 122

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



110
111
112
113
# File 'lib/bolt/resource_instance.rb', line 110

def overwrite_state(state)
  assert_hash('state', state)
  @state = state
end

#referenceObject Also known as: to_s



90
91
92
# File 'lib/bolt/resource_instance.rb', line 90

def reference
  self.class.format_reference(@type, @title)
end

#set_desired_state(desired_state) ⇒ Object

rubocop:disable Naming/AccessorMethodName



116
117
118
119
# File 'lib/bolt/resource_instance.rb', line 116

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



104
105
106
107
# File 'lib/bolt/resource_instance.rb', line 104

def set_state(state)
  assert_hash('state', state)
  @state.merge!(state)
end

#to_hashObject Also known as: _pcore_init_hash



70
71
72
73
74
75
76
77
78
79
# File 'lib/bolt/resource_instance.rb', line 70

def to_hash
  {
    'target'        => target,
    'type'          => type,
    'title'         => title,
    'state'         => state,
    'desired_state' => desired_state,
    'events'        => events
  }
end

#to_json(opts = nil) ⇒ Object



82
83
84
# File 'lib/bolt/resource_instance.rb', line 82

def to_json(opts = nil)
  to_hash.to_json(opts)
end