What does node['foo'].push('bar') equate to?


#1

The slack_handler cookbook sets an attribute like so:

node['chef_client']['handler']['slack']['webhooks']['name'].push('webhook1')

Is “name” an array? What would the equivalent hierarchy look like in a role?

node['foo'].push('bar')
node['foo']['bar']['derp'] = 42

I’m assuming it would be this:

"foo": {
  "bar": [{
     "derp": 42 
   }]
}

I’m trying to test this in chef-shell, but I get the following error.

chef (12.8.1)> node.default['foo']
 => {}
chef (12.8.1)> node.default['foo'].push('bar')
NoMethodError: Undefined node attribute or method `push' on `node'. To set an attribute, use `push=value' instead.

#2

You can’t automagically create an array in ruby like that. You could do something like:

....
node['foo']['bar'] = []
node['foo']['bar'].push( { 'derp' => 42 } )

To get:

"foo": {
  "bar": [{
     "derp": 42 
   }]
}

#3

So is the readme wrong on this handler?

Add webhook1 URL

node[‘chef_client’][‘handler’][‘slack’][‘webhooks’][‘name’].push(‘webhook1’)
node[‘chef_client’][‘handler’][‘slack’][‘webhooks’][‘webhook1’][‘url’] = ‘https://hooks.slack.com/1/2/3

Add webhook2 URL

node[‘chef_client’][‘handler’][‘slack’][‘webhooks’][‘name’].push(‘webhook2’)
node[‘chef_client’][‘handler’][‘slack’][‘webhooks’][‘webhook2’][‘url’] = ‘https://hooks.slack.com/1/2/4


#4

Sorry, updated that post in discourse, my code was off :slight_smile:

Mark
Tact is the ability to tell a man he has an open mind when he has a hole in
his head.


#5

No, it is fine because of https://github.com/rackspace-cookbooks/chef-slack_handler/blob/master/attributes/default.rb#L26


#6

so, in a recipe it would look like:

node.default[‘chef_client’][‘handler’][‘slack’][‘webhooks’][‘name’].push(‘webhook1’)
#node[‘chef_client’][‘handler’][‘slack’][‘webhooks’][‘name’][0] == ‘webhook1’