How to access chef resource properties in helper library

393 Views Asked by At
  1. How to access resource_name property in library functions.
  2. How to add custom action attribute for existing community cookbook resource.

Introduction: Sensu cookbook has resource named "sensu_check" having two actions :create or :delete. In recipes around 200 checks are written. Different checks are applicable to only respective type of server viz, Tomcat, Sonar, Solr, etc.

When recipe run all 200 checks are configured on every server which is undesired as only applicable checks should be configured on that type of servers.

I have created one Hash of checks where check_name, applicable server type is mentioned.

One Library function is written which determine whether current_check is appicable on current server or not based on its host name.

Issue:

1) How to retrieve current resource name property i.e. "tomcat_service_check" in Library function. resource snippet -

sensu_ckeck "tomcat_service_check" do
  command "sh start.sh"
  subscriber "tomcat"
  ...
  action :create or :delete  (we want to add :ignore with custom provider)
end

2) Is there any way to add another custom action attribute :ignore in "sensu_check" resource(community cookbook) with custom provider. if Yes, how to tell chef in run time that current resource is not executed due to ignore condition.

1

There are 1 best solutions below

0
coderanger On

You can't directly access that data but you also don't need to. Just make a loop in your recipe like this:

all_of_the_checks.each do |check_name|
  if check_applies_to_host?(node, check_name)
    sensu_check check_name do
      # Whatever else
    end
  end
end

As for part 2, no you cannot directly add new actions to an existing custom resource. If it uses the non-DSL declaration method you may be able to subclass it, but usually you cannot.