Chef-helpers: deep attribute access by JSONPath


#1

Hi,

FWIW, I’ve released a new version of chef-helpers.gem - my collection of
utils and extra methods I use in recipes. It’s available at

One feature that may be particularly useful (and if it’s interesting, it
may be worth extracting to a separate gem) is overloading Chef::Node#[] to
allow reading nested attributes using JSONPath notation (
http://goessner.net/articles/JsonPath/):

chef > require 'chef-helpers'
 => true
chef > node['$..name']
 => ["portinari-2.local", "Java(TM) SE Runtime Environment", "Java

HotSpot™ 64-Bit Server VM", “Darwin”]
chef > node[’$.kernel.name’]
=> [“Darwin”]

You can read about more sophisticated use cases at
http://3ofcoins.net/2013/07/21/access-chef-nodes-attributes-by-jsonpath/ -
I bump into such situations quite often, and easy access to deep attributes
saves me a lot of effort (and prevents confusing tracebacks if the code is
not defensive enough). I’m curious to hear your thoughts.

– M


#2

Nice! I didn’t know about JsonPath until today, but it sounds really useful
for accessing node attributes, especially in light of the recent
discussions on this list. Like! :slight_smile:
On Jul 22, 2013 12:07 PM, “Maciej Pasternacki” maciej@3ofcoins.net wrote:

Hi,

FWIW, I’ve released a new version of chef-helpers.gem - my collection of
utils and extra methods I use in recipes. It’s available at
https://github.com/3ofcoins/chef-helpers/

One feature that may be particularly useful (and if it’s interesting, it
may be worth extracting to a separate gem) is overloading Chef::Node#[] to
allow reading nested attributes using JSONPath notation (
http://goessner.net/articles/JsonPath/):

chef > require 'chef-helpers'
 => true
chef > node['$..name']
 => ["portinari-2.local", "Java(TM) SE Runtime Environment", "Java

HotSpot™ 64-Bit Server VM", “Darwin”]
chef > node[’$.kernel.name’]
=> [“Darwin”]

You can read about more sophisticated use cases at
http://3ofcoins.net/2013/07/21/access-chef-nodes-attributes-by-jsonpath/- I bump into such situations quite often, and easy access to deep
attributes saves me a lot of effort (and prevents confusing tracebacks if
the code is not defensive enough). I’m curious to hear your thoughts.

– M