1

With some of the default chef resources, it is possible to access some of their attributes after they have been called

# recipes/default.rb
f = file "/tmp/file_resource" do
  owner "root"
  group "root"
  mode "0755"
  action :create
end

log "Path to my file is #{f.path}" # outputs "/tmp/file_resource"

How can this be achieved in a custom LWRP (here is an example)

# resources/default.rb
actions :create
default_action :create

attribute :relative_path, :kind_of => String, :name_attribute => true
attribute :full_path, :kind_of => String

In this provider, I am trying to update the property of new_resource.full_path to be equal to the path of the file resource

# providers/default.rb
action :create do

  f = file "/path/to/my/resource/#{new_resource.relative_path}" do
    owner "root"
    group "root"
    mode "0755"
    action :create
  end

  new_resource.full_path(f.path)
  new_resource.updated_by_last_action(f.updated_by_last_action?)
end

However when I try to access resource.full_path in the recipe, it is nil rather than the expected /path/to/my/resource/relative/path

# recipes/default.rb
resource = my_awesome_lwrp "relative/path" do
  action :create
end

log "Full path for my resource: #{resource.full_path}" # outputs "Full path for my resource:"

This example is rather contrived I know, the real world application/reason for this can be seen in the default resource/provider here https://github.com/peterjmit/chef-ssl-cert

Pete Mitchell
  • 2,879
  • 1
  • 16
  • 22
  • Did you try this example to see if it would work? I'm pretty sure lightweight resources have this ability by default, same as any other resource. – Ajedi32 Jul 30 '14 at 18:10
  • Though I should point out that resource attributes aren't normally assigned using `=`, and that changes you make to the resource in the provider won't be available during recipie compile time. – Ajedi32 Jul 30 '14 at 18:15
  • @Ajedi32 the example doesn't work, perhaps the fact that "changes made to the resources in the provider won't be available during recipe compile time" is the core of the problem here. – Pete Mitchell Jul 30 '14 at 18:17
  • Well, this is a lightweight provider, so maybe the DSL shown here *does* actually run at recipie compile time. I'm not completely certain. My guess though is that it doesn't. – Ajedi32 Jul 30 '14 at 18:26
  • What isn't working? There's some code, but no question, error, stacktrace, etc. – sethvargo Jul 31 '14 at 14:11
  • @sethvargo My expectation was to be able to set/update the property on a resource in a provider, and then access it in a recipe (the lack of ability to do that is demonstrated by `resource.full_path` being `nil` in the recipe - I will update the question for clarity – Pete Mitchell Jul 31 '14 at 15:05
  • I have updated the question, I have _an answer_ but im not 100% sure it is the right approach, I will add an answer shortly – Pete Mitchell Jul 31 '14 at 15:10
  • Right... because the `:create` action hasn't yet be called at that time. So that code (`new_resource.full_path(...)`) hasn't been executed yet. If you wrap that in a `ruby_block`, you'll get the value back. – sethvargo Jul 31 '14 at 15:54

0 Answers0