Case Statement and Node Attributes in Cookbook Attributes

So currently and trying to configure an attributes file in a given cookbook recipe, to case against a set of node attributes that are derived from a customer Ohai plugin. When I attempt to do this, I get the following error message when attempting to do this:

NoMethodError
-------------
undefined method `[]' for nil:NilClass

Cookbook Trace:
---------------
  /var/chef/cache/cookbooks/cmhf_packages/attributes/default.rb:1:in `from_file'

Relevant File Content:
----------------------
/var/chef/cache/cookbooks/cmhf_packages/attributes/default.rb:

  1>> case node['ptdata']['application']
  2:  when 'CMEMs'
  3:    case node['ptdata']['environment']['base']
  4:    when 'Production'
  5:      puts "Envrionment not supported."
  6:    when'Stage'
```

There idea here is that I will have a set of attributes that will be set differently depending on the environment that we have defined in our Ohai plugin.  If you look at the nodes attributes, you will see that the data is present:

```
  ptdata:
    application: CMEMs
    environment:
      base:     Stage
      specific: s1
    service:     parse_service
```

Can custom node attributes be evaluated within a cookbooks attribute file?

In general yes you can, however if the plugin is being deployed by Chef itself (usually via the ohai cookbook) then it won’t be available on the first run. See https://coderanger.net/two-pass/ for a full overview of the run process and ordering.

I have already completed the initial run and the data is populated (shown in the search results section of my original post). Not sure why this is still coming up as nil. Appreciate the info on the run ordering. That is very helpful.

Is there anything else that I can look at to verify why the attribute would be showing up as nil?

Interesting enough, I noticed that if I use an environment attribute instead of a node attribute, this seems to work fine.

Further digging I have found that I am able to evaluate against other node attributes that are default to Chef, so there must be an issue with my Ohai plugin. If someone could take a look and give any pointers that would be appreciated:

`require “socket”

Ohai.plugin(:Ptdata) do
provides “ptdata”

collect_data(:linux) do
ptdata Mash.new
hostname = Socket.gethostname.split(".")[0].split("-")
parse_environment = hostname[0][0]
parse_app = hostname[1]
parse_service = hostname[2]
parse_site = hostname[3][1]

case parse_environment
  when 'p'
    ptdata['environment'] = {
      'base' => 'Production',
      'specific' => hostname[0]
    }
  when 's'
    ptdata['environment'] = {
      'base' => 'Stage',
      'specific' => hostname[0]
    }
  when 'h'
    ptdata['environment'] = {
      'base' => 'HATS',
      'specific' => hostname[0]
    }
  when 'q'
    ptdata['environment'] = {
      'base' => 'Quality',
      'specific' => hostname[0]
    }
  when 'd'
    ptdata['environment'] = {
      'base' => 'Development',
      'specific' => hostname[0]
    }
  when 't'
    ptdata['environment'] = {
      'base' => 'Test',
      'specific' => hostname[0]
    }
  else
    ptdata['environment'] = {
      'base' => parse_environment,
      'specific' => hostname[0]
    }
end`

There are additional case statements but you can get the gist.