Using notifications w/ custom resources

I just rewrote an LWRP as a new custom resource, and ran into what smells like a bug, but could just be an oddity with how the new custom resources function.

Given this (abbreviated) code for a baragon_agent resource:

default_action :create

property :group, String, name_attribute: true
property :port, Integer, required: true
property :config, Hash, required: true
property :templates, Array, required: true

action :create do
  file "/etc/baragon/agent-#{group}.yml" do
    mode 0644
    content yaml_config(agent_yaml)
    notifies :restart, "service[baragon-agent-#{group}]"
  end

And a call in the recipe to:

baragon_agent 'default' do
  port 8882
  config node['baragon']['agent_yaml'].to_hash
  templates node['baragon']['templates'].values
end

I end up with an error, b/c it tries to notify service[baragon-agent-] (i.e., it doesn’t seem to pass group in to the notifies properly).

However, if the resource is:

action :create do
  agent_yaml['loadBalancerConfig']['name'] = group

  file "/etc/baragon/agent-#{group}.yml" do
    mode 0644
    content yaml_config(agent_yaml)
    notifies :restart, "service[baragon-agent-#{agent_yaml['loadBalancerConfig']['name']}]"
  end

It works!

Ideas?

I end up with an error, b/c it tries to notify service[baragon-agent-] (i.e., it doesn’t seem to pass group in to the notifies properly).

group is a property of the file resource, it probably defaults to nil, and nil.to_s is "". Chef uses instance_eval to give you the fluid syntax of resources, but the cost is in situations like this you can accidentally call methods on a resource instead of referring to an outside variable. If you added something like agent_group = group and then put "service[baragon-agent-#{agent_group}]" in your file resource, it should work.

I would try:

action :create do
  file "/etc/baragon/agent-#{group}.yml" do
    mode 0644
    content yaml_config(agent_yaml)
    notifies :restart, "service[baragon-agent-#{new_resource.group}]"
  end

As per this part of docs about overriding properties

I.e: specify that within the file resource you’re trying to access the custom_resource property and not the file resource one.

That totally worked, and makes tons of sense. Thanks much; I knew I was missing something.