Class: Chef::Resource::Notification
- Inherits:
-
Object
- Object
- Chef::Resource::Notification
- Defined in:
- lib/chef/resource/resource_notification.rb
Overview
Instance Attribute Summary collapse
-
#action ⇒ Action
the action to notify.
-
#notifying_resource ⇒ Resource
the Chef resource performing the notification.
-
#resource ⇒ Resource
the Chef resource object to notify to.
-
#unified_mode ⇒ Object
Returns the value of attribute unified_mode.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#duplicates?(other_notification) ⇒ Boolean
Is the current notification a duplicate of another notification.
-
#fix_notifier_reference(resource_collection) ⇒ void
This will look up the notifying_resource if it is not a Resource Object.
-
#fix_resource_reference(resource_collection, always_raise = false) ⇒ void
This will look up the resource if it is not a Resource Object.
-
#initialize(resource, action, notifying_resource, unified_mode = false) ⇒ Notification
constructor
A new instance of Notification.
-
#resolve_resource_reference(resource_collection, always_raise = false) ⇒ void
If resource and/or notifying_resource is not a resource object, this will look them up in the resource collection and fix the references from strings to actual Resource objects.
Constructor Details
#initialize(resource, action, notifying_resource, unified_mode = false) ⇒ Notification
Returns a new instance of Notification.
31 32 33 34 35 36 |
# File 'lib/chef/resource/resource_notification.rb', line 31 def initialize(resource, action, , unified_mode = false) @resource = resource @action = action&.to_sym @notifying_resource = @unified_mode = unified_mode end |
Instance Attribute Details
#action ⇒ Action
the action to notify
27 28 29 |
# File 'lib/chef/resource/resource_notification.rb', line 27 def action @action end |
#notifying_resource ⇒ Resource
the Chef resource performing the notification
27 28 29 |
# File 'lib/chef/resource/resource_notification.rb', line 27 def @notifying_resource end |
#resource ⇒ Resource
the Chef resource object to notify to
27 28 29 |
# File 'lib/chef/resource/resource_notification.rb', line 27 def resource @resource end |
#unified_mode ⇒ Object
Returns the value of attribute unified_mode.
29 30 31 |
# File 'lib/chef/resource/resource_notification.rb', line 29 def unified_mode @unified_mode end |
Instance Method Details
#==(other) ⇒ Object
136 137 138 139 140 |
# File 'lib/chef/resource/resource_notification.rb', line 136 def ==(other) return false unless other.is_a?(self.class) other.resource == resource && other.action == action && other. == end |
#duplicates?(other_notification) ⇒ Boolean
Is the current notification a duplicate of another notification
42 43 44 45 46 47 48 49 |
# File 'lib/chef/resource/resource_notification.rb', line 42 def duplicates?(other_notification) unless other_notification.respond_to?(:resource) && other_notification.respond_to?(:action) msg = "only duck-types of Chef::Resource::Notification can be checked for duplication "\ "you gave #{other_notification.inspect}" raise ArgumentError, msg end other_notification.resource == resource && other_notification.action == action end |
#fix_notifier_reference(resource_collection) ⇒ void
This method returns an undefined value.
This will look up the notifying_resource if it is not a Resource Object. It will complain if it finds multiple resources, can’t find a resource, or gets invalid syntax.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/chef/resource/resource_notification.rb', line 108 def fix_notifier_reference(resource_collection) matching_notifier = resource_collection.find() if Array(matching_notifier).size > 1 msg = "Notification #{self} from #{} was created with a reference to multiple notifying "\ "resources, but can only originate from one resource. Destination resource was defined "\ "on #{resource.source_line}" raise Chef::Exceptions::InvalidResourceReference, msg end self. = matching_notifier rescue Chef::Exceptions::ResourceNotFound => e err = Chef::Exceptions::ResourceNotFound.new(<<~FAIL) Resource #{resource} is configured to receive notifications from #{} with action #{action}, \ but #{} cannot be found in the resource collection. #{resource} is defined in \ #{resource.source_line} FAIL err.set_backtrace(e.backtrace) raise err rescue Chef::Exceptions::InvalidResourceSpecification => e err = Chef::Exceptions::InvalidResourceSpecification.new(<<~F) Resource #{resource} is configured to receive notifications from #{} with action #{action}, \ but #{.inspect} is not valid syntax to look up a resource in the resource collection. Notification \ is defined near #{resource.source_line} F err.set_backtrace(e.backtrace) raise err end |
#fix_resource_reference(resource_collection, always_raise = false) ⇒ void
This method returns an undefined value.
This will look up the resource if it is not a Resource Object. It will complain if it finds multiple resources, can’t find a resource, or gets invalid syntax.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/chef/resource/resource_notification.rb', line 73 def fix_resource_reference(resource_collection, always_raise = false) matching_resource = resource_collection.find(resource) if Array(matching_resource).size > 1 msg = "Notification #{self} from #{} was created with a reference to multiple resources, "\ "but can only notify one resource. Notifying resource was defined on #{.source_line}" raise Chef::Exceptions::InvalidResourceReference, msg end self.resource = matching_resource rescue Chef::Exceptions::ResourceNotFound => e # in unified mode we allow lazy notifications to resources not yet declared if !unified_mode || always_raise err = Chef::Exceptions::ResourceNotFound.new(<<~FAIL) resource #{} is configured to notify resource #{resource} with action #{action}, \ but #{resource} cannot be found in the resource collection. #{} is defined in \ #{.source_line} FAIL err.set_backtrace(e.backtrace) raise err end rescue Chef::Exceptions::InvalidResourceSpecification => e err = Chef::Exceptions::InvalidResourceSpecification.new(<<~F) Resource #{} is configured to notify resource #{resource} with action #{action}, \ but #{resource.inspect} is not valid syntax to look up a resource in the resource collection. Notification \ is defined near #{.source_line} F err.set_backtrace(e.backtrace) raise err end |
#resolve_resource_reference(resource_collection, always_raise = false) ⇒ void
This method returns an undefined value.
If resource and/or notifying_resource is not a resource object, this will look them up in the resource collection and fix the references from strings to actual Resource objects.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/chef/resource/resource_notification.rb', line 56 def resolve_resource_reference(resource_collection, always_raise = false) return resource if resource.is_a?(Chef::Resource) && .is_a?(Chef::Resource) unless resource.is_a?(Chef::Resource) fix_resource_reference(resource_collection, always_raise) end unless .is_a?(Chef::Resource) fix_notifier_reference(resource_collection) end end |