=> How to inject custom methods inside existing resource to decide whether resource is allowed to execute or not ?
e.g. sensu__ckeck resource is provided by community cookbook “sensu”
sensu_check "apache-service" do
# provider Chef::Provider::MySensuCheck
command "date"
handlers ["remediator"]
subscribers ["k2-service"]
standalone false
publish false
action [:create]
# not_if {SensuHelper.isCheckValid?}
end
Hash: - ( Hash containing different checks, applicable servers category and active status)
default['cookbook_name']['checks'] = [
{
'checks' =>['apache','apache-service'],
'applicable_servers' => ['httpd','apache-server'],
'active' => true
},
{
....
....
}
]
Intension: -
I’m trying to invoke custom method (isCheckValid?) inside community cookbook resource (sensu_check) which is LWRP based.
isCheckValid? method must determine current resource state i.e. resource.name (apache-service) and check against provided hash to determine whether this check.name is applicable on current host or not.
Tried Solution commented statements in above resource: -
In order to achieve this below are few scenarios are tried but not working efficiently as aspected -
-
Helper Method -
Created SensuHelper.isCheckValid? method but unable to get current resource information (new_resource.name) as load_current_resource is able to be invoked in provider. -
Custom Provider -
We are able to get current resource information in custom provider but need to rewrite whole Provider actions in HWRP overriding community cookbook provider which is not desired.
Help needed on to solve above problem: -
- How to inject such custom methods in LWRP or HWRP Providers (either by subclassing way) which can be called from resources.
- How to write custom helper methods where we can retrieve current resource information and check desired resource state.
- Any other solution to achieve such filtration either with guard condition or custom classes.
Note: - Main Objective here is to use any Helper methods where we should be able to get current resource state i.e. what we get from Chef::Provider#load_current_resource and write helper logic.