# Custom resource

**URL:** https://discourse.chef.io/t/custom-resource/16793
**Category:** Chef Infra (archive)
**Created:** [March 10, 2020, 6:18pm UTC](https://discourse.chef.io/t/custom-resource/16793 "2020-03-10T18:18:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![TimFromFL](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@TimFromFL](https://discourse.chef.io/u/TimFromFL)
#### Post date: [March 10, 2020, 6:18pm UTC](https://discourse.chef.io/t/custom-resource/16793/1 "2020-03-10T18:18:41Z")

</div>

I am new to chef and I'm in the process of learning both Chef and Ruby, so I fully admit that this might be a very basic question that I just can't see the answer to. I've tried searching for a solution but have not found any examples or guidance on how to proceed with what I am trying to accomplish.

For demo purposes I have created a new cookbook using the following commands:

```
chef generate cookbook demo
chef generate attribute default
chef generate resource package

```

recipes\default.rb

`demo_package 'Demo'`

resources\package.rb

```
property :resource_prop1, String
property :resource_prop2, String

default_action :install

load_current_value do
  puts
  puts "the current value before change for 'test_prop1': #{resource_prop1}"
  puts "the current value before change for 'test_prop2': #{resource_prop2}"
  resource_prop1 = node['demo_resource']['test_prop1']
  resource_prop2 = node['demo_resource']['test_prop2']
  puts "the current value after change for 'test_prop1': #{resource_prop1}"
  puts "the current value after change for 'test_prop2': #{resource_prop2}"
end

action :install do
    puts "the current value after load properties for 'test_prop1': #{new_resource.resource_prop1}"
    puts "the current value after load properties for 'test_prop2': #{new_resource.resource_prop2}"
end

```

attributes\default.rb

```
default['demo_resource'] = {}
default['demo_resource']['test_prop1'] = 'test value 1'
default['demo_resource']['test_prop2'] = 'test value 2'

```

I am expecting the output to look like this:

```
the current value before change for 'test_prop1':
the current value before change for 'test_prop2':
the current value after change for 'test_prop1': test value 1
the current value after change for 'test_prop2': test value 2
the current value after load properties for 'test_prop1': test value 1
the current value after load properties for 'test_prop2': test value 2

```

but what I actually get is:

```
the current value before change for 'test_prop1':
the current value before change for 'test_prop2':
the current value after change for 'test_prop1': test value 1
the current value after change for 'test_prop2': test value 2
the current value after load properties for 'test_prop1': 
the current value after load properties for 'test_prop2':

```

I have tried changing this line:  
`puts "the current value after load properties for 'test_prop1': #{new_resource.resource_prop1}"`  
into this  
`puts "the current value after load properties for 'test_prop1': #{resource_prop1}"`

But I get this error:

```
NameError
undefined local variable or method `resource_prop1' for #<#Class:0x0000000007b51178>:0x00000000084b3680>
Did you mean? resources

```

Does anyone have any idea what I might be doing wrong?

---

<div class="post-metadata">

### Author: ![Tensibai](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/tensibai/32/29_2.png) [@Tensibai](https://discourse.chef.io/u/Tensibai)
#### Post date: [March 12, 2020, 9:05am UTC](https://discourse.chef.io/t/custom-resource/16793/2 "2020-03-12T09:05:44Z")

</div>

> [@TimFromFL](#):
>
> resource\_prop1 = node['demo\_resource']['test\_prop1']

This line in load\_current values populates `current_resource` and doesn't update `new_resource`.

The goal is to be able to use `converge_if_changed :resource_prop1` (which will compare resource\_prop1 between current\_resource and new\_resource to act or not)

Useful links:

- [https://docs.chef.io/dsl\_custom\_resource/#custom-resource-dsl](https://docs.chef.io/dsl_custom_resource/#custom-resource-dsl)
- [Learn Chef](https://learn.chef.io/modules/build-a-custom-resource#/)

---

<div class="post-metadata">

### Author: ![TimFromFL](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@TimFromFL](https://discourse.chef.io/u/TimFromFL)
#### Post date: [March 13, 2020, 2:11pm UTC](https://discourse.chef.io/t/custom-resource/16793/3 "2020-03-13T14:11:20Z")

</div>

@Tensibai If I understand your response I think it tracks with the solution\workaround that I found. I changed the line in question to:

`package.resource_prop1 = node['demo_resource']['test_prop1']`

and that seemed to fix the issue and everything proceeded as expected.
