Node.attribute? check nested

Hello.
Official documentation mentions such construction:
not_if { node.attribute?(“some_command_complete”) }

Can I check nested attribute with it?
For example, if i have attribute node[‘foo’][‘bar’], how can i check if it is set?

node['foo']['bar'] is just a variable so any ruby logic will work

puts "it's not set" unless node['foo']['bar']

if node['foo']['bar'] == "biz" etc.

On Sun, Jul 14, 2013 at 11:02 AM, Daniil S daniil_sb@yahoo.com wrote:

Hello.
Official documentation mentions such construction:
not_if { node.attribute?("some_command_complete") }

Can I check nested attribute with it?
For example, if i have attribute node['foo']['bar'], how can i check if it
is set?

this will bork , if node['foo'] is nil,

im not aware of anything from chef api, but i know this is a very common
requirement, I tend to use a helper method, like this :

def nested_attrbs?(*args)
ret = true
n = node
args.each do |nesting|
unless n.attribute?(nesting)
ret = false
break
else
n = n[nesting]
end
end
ret
end

On Sun, Jul 14, 2013 at 9:24 AM, Nic Grayson nic.grayson@banno.com wrote:

node['foo']['bar'] is just a variable so any ruby logic will work

puts "it's not set" unless node['foo']['bar']

if node['foo']['bar'] == "biz" etc.

On Sun, Jul 14, 2013 at 11:02 AM, Daniil S daniil_sb@yahoo.com wrote:

Hello.
Official documentation mentions such construction:
not_if { node.attribute?("some_command_complete") }

Can I check nested attribute with it?
For example, if i have attribute node['foo']['bar'], how can i check if
it is set?

Good practice here to make sure you're setting default['foo'] = {} in
your attributes file. Otherwise, a "tried to treat a nil like a hash"
error will break your script, if node['foo'] isn't set.

If you can't be sure that node['foo'] will be set (for some reason),
you can append a 'rescue false' to your test, to prevent that:

if ( (!node['foo']['bar'].nil?) rescue false)

do something only if node['foo']['bar'] is set

end

(Note that the rescue modifier will eat all errors within the clause
it modifies. Don't use it for more complicated statements.)

Cheers,

--
Nathaniel Eliot
T9 Productions

On Sun, Jul 14, 2013 at 11:24 AM, Nic Grayson nic.grayson@banno.com wrote:

node['foo']['bar'] is just a variable so any ruby logic will work

puts "it's not set" unless node['foo']['bar']

if node['foo']['bar'] == "biz" etc.

On Sun, Jul 14, 2013 at 11:02 AM, Daniil S daniil_sb@yahoo.com wrote:

Hello.
Official documentation mentions such construction:
not_if { node.attribute?("some_command_complete") }

Can I check nested attribute with it?
For example, if i have attribute node['foo']['bar'], how can i check if it
is set?

I've run into this pattern a lot and created a set of helper functions to
make working with them easier:

It is a recursive function that usually feeds off the node attribute, so in
theory bad things could happen if the node hash is really large, I have not
had issues with it however.

Normally I mix these functions in to all appropriate namespaces (resource,
provider, recipe) when the library is loaded so I can use them easily.
Beware of namespace clashes though.

On Sun, Jul 14, 2013 at 12:47 PM, Nathaniel Eliot <
temujin9@t9productions.com> wrote:

Good practice here to make sure you're setting default['foo'] = {} in
your attributes file. Otherwise, a "tried to treat a nil like a hash"
error will break your script, if node['foo'] isn't set.

If you can't be sure that node['foo'] will be set (for some reason),
you can append a 'rescue false' to your test, to prevent that:

if ( (!node['foo']['bar'].nil?) rescue false)

do something only if node['foo']['bar'] is set

end

(Note that the rescue modifier will eat all errors within the clause
it modifies. Don't use it for more complicated statements.)

Cheers,

--
Nathaniel Eliot
T9 Productions

On Sun, Jul 14, 2013 at 11:24 AM, Nic Grayson nic.grayson@banno.com
wrote:

node['foo']['bar'] is just a variable so any ruby logic will work

puts "it's not set" unless node['foo']['bar']

if node['foo']['bar'] == "biz" etc.

On Sun, Jul 14, 2013 at 11:02 AM, Daniil S daniil_sb@yahoo.com wrote:

Hello.
Official documentation mentions such construction:
not_if { node.attribute?("some_command_complete") }

Can I check nested attribute with it?
For example, if i have attribute node['foo']['bar'], how can i check if
it
is set?