I need help coming up with the best “interface” for consumers of my cookbook.
Let’s say I have the following in my cookbook’s attributes/default.rb
default['my_cookbook']['my_default_hashes'] = {
'hash1' = {
'property1_1' => 'value1_1',
'property1_2' => 'value1_2'
},
'hash2' = {
'property2_1' => 'value2_1',
'property2_2' => 'value2_2'
},
'hash3' = {
'property3_1' => 'value3_1',
'property3_2' => 'value3_2'
}
}
default['my_cookbook']['my_selected_hashes'] = {} # empty by default
I want to use node['my_cookbook']['my_selected_hashes']
as input to one of my template .erb files. I have another cookbook, let’s call it wrapper_cookbook, that consumes my_cookbook. Before I converge wrapper_cookbook, I want to set node['my_cookbook']['my_selected_hashes']
with hash1
and hash3
(but not hash2
) and ALSO add a brand-new hash called hash4
. And to further complicate things, I want to use a different value for property3_1
(from hash3
), while keeping node['my_cookbook']['my_default_hashes']
unmodified (I’m treating it as read-only).
What is the best way to do this?
Here are some ideas of what node['my_cookbook']['my_selected_hashes']
would look like in wrapper_cookbook:
Idea #1:
node.set['my_cookbook']['my_selected_hashes'] = {
'hash1' = {}, # left empty to indicate that I want the default values from node['my_cookbook']['my_default_hashes']['hash1']
'hash3' = {
'property3_1' = 'some_new_value' # this is the only attribute from hash3 I want to override
},
'hash4' = {
'property4_1' => 'value4_1',
'property4_2' => 'value4_2'
}
}
Idea #2:
# A new node attribute not in the original attributes/default.rb file: This is an array of my selected hashes from node['my_cookbook']['my_default_hashes']
node.set['my_cookbook']['predefined_hashes_i_want'] = ['hash1', 'hash3']
# This is also a new node attribute not in the original attributes/default.rb file.
node.set['my_cookbook']['my_default_hashes_overrides'] = {
'hash3' = {
'property3_1' = 'some_new_value' # this is the only attribute from hash3 I want to override
},
}
node.set['my_cookbook']['my_selected_hashes'] = {
'hash4' = {
'property4_1' => 'value4_1',
'property4_2' => 'value4_2'
}
}
The idea above is that the hashes named in node['my_cookbook']['predefined_hashes_i_want']
would be added to node[‘my_cookbook’][‘my_selected_hashes’] while ensuring that hash3 in node['my_cookbook']['my_selected_hashes']
has a new value for its property3_1
.
Which of the 2 above is better? Is there a third option that would be the best approach? I appreciate the help!