title: get calling cookbook within chef custom resource
body: <p>I have a cookbook <code>my_service</code> with a custom resource <code>write_config</code>.
  This resource is called from another cookbook <code>node_a</code>.</p>
  
  <ul>
  <li><p><code>my_service/resources/write_config.rb</code>:</p>
  
  <pre><code>property :template_source, String, name_property: true 
  property :calling_cookbook, String, required: true
  
  action :create do
    template "/tmp/test.txt" do
      source   template_source
      cookbook calling_cookbook
      owner    'root'
      mode     '0644'
      action   :create
    end
  end
  </code></pre></li>
  <li><p>Node cookbook recipe <code>node_a/default.rb</code>:</p>
  
  <pre><code>my_service_write_config 'config.erb' do
    calling_cookbook 'node_a'
  end
  </code></pre></li>
  </ul>
  
  <p>This works fine, but I would like to get rid of the <code>calling_cookbook</code> property.</p>
  
  <p>Is there a way to automatically get the <code>calling_cookbook</code> name?</p>
  
  <h2>Solution</h2>
  
  <p>(Thank you <a href="https://stackoverflow.com/users/78722/coderanger">coderanger</a>!)
  The basic resource <code>template</code> seems to get evaluated in the context of the calling (<code>node_a</code>) cookbook (?).</p>
  
  <ul>
  <li><p><code>my_service/resources/write_config.rb</code>:</p>
  
  <pre><code>property :template_source, String, name_property: true 
  action :create do
    template "/tmp/test.txt" do
      source   template_source
      owner    'root'
      mode     '0644'
      action   :create
    end
  end
  </code></pre></li>
  <li><p>Using it within the node cookbook recipe <code>node_a/default.rb</code> becomes a one-liner:</p>
  
  <pre><code>...
  include_recipe 'my_service'
  my_service_write_config 'config.erb'
  ...
  </code></pre></li>
  </ul>
  
  <p>Neat!</p>