How to get why_run working with custom resources

Hi,

I have a simple custom resource which just wraps a template resource. It works, but when I use --why-run I get:

* Whyrun not supported for twi_properties[XXX], bypassing load.

This is what my custom resource looks like:

class TWIProperties < ChefCompat::Resource
  resource_name :twi_properties
  property :production, [TrueClass, FalseClass], default: false
  ...
  property :other, Hash, default: {}
  property :template_cookbook, String, default: 'twiapp'

  default_action :create

  action :create do
    template name do
      source 'twiproperties.erb'
      cookbook template_cookbook
      variables(
        ...
      )
    end
  end

What am I doing wrong? I have tried adding a whyrun_supported? method, but that didnt help (and I believe I don’t need it).

Regards,
Christine

Hello Christine,

I have exactly the same question.

By looking at chef code, I am not sure it is possible.

My understandings of custom resource management (i.e all the code is in resources/ directory and only the dsl is used):

  • Resource::LWRPBase.build_from_file reads the file where the code is written
  • it defines a uniq class whose pretty name: “Custom resource #{resource_name} from cookbook #{cookbook_name}”

No code defined in the resource file is executed in the context of a provider.

The provider is created on the fly (at convergence time?).

After 12.5 if I understood properly it has been changed to converge_if_changed as per the custom_resource DSL here https://docs.chef.io/dsl_custom_resource.html but I’m unsure of what has changed exactly between both syntax.

Previsouly the trick was to wrap the usual resources in a converge_by block, i.e:

action :create do
  converge_by "Doing creation of TWI #{ @new_resource }" do
    template name do
      source 'twiproperties.erb'
      cookbook template_cookbook
      variables(
        ...
      )
    end
  end
end

This is the syntax pre 12.5 as documented here: https://docs.chef.io/release/12-2/custom_resources.html

Thanks for your answer

I think that why-run could run previously (with a resource/provider implementation) without this trick.
If your provider was specifying whyrun_supported?, everything worked becasue your provider was a collection of resource supporting the whyrun_mode.

It’s a bug https://github.com/chef/chef/issues/4537

Thanks to Lamont, there’s a workaround you can put in the resource file

action_class do
  def whyrun_supported?
    true
  end
end

Regards,
Christine

1 Like

thanks Christine for the pointer.

It does not seem to work for me, I’ll try to understand why.