djoos-cookbooks / newrelic

Development repository for the newrelic cookbook
https://supermarket.chef.io/cookbooks/newrelic
MIT License
143 stars 245 forks source link

Use `lazy{}` helper on license properties #345

Closed coderanger closed 6 years ago

coderanger commented 6 years ago

It looks like all the custom resources have some variation on :default => NewRelic.application_monitoring_license(node) which means the default value is being computed at file compile time. If you change that to :default => lazy { NewRelic.application_monitoring_license(node) } it will make life a little easier since it delays the computation of the default until it gets used.

daniel-rocha-ciandt commented 6 years ago

Hi, @coderanger

Ok. We manage to solve this problem by changing this line in the followed files: ../resources/agent_infrastructure.rb ../resources/agent_php.rb

From:

attribute :license, :kind_of => String, :default => NewRelic.application_monitoring_license(node)

To:

attribute :license, :kind_of => String, :default => lazy { NewRelic.application_monitoring_license(node) }

Inside the recipe

node.default['newrelic']['license'] = newrelic['license_key']

Calling our data bag with this structure:

id: newrelic license_key:xxxxxxxxxxxxxxxxxxxxx

Thanks for your help.

djoos commented 6 years ago

Thanks @coderanger for the suggestion!

@coderanger: any way I could hide the lazy'ness in the module instead? That would feel cleaner rather than spreading "lazy {}" across the recipes / resources... Thanks in advance!

coderanger commented 6 years ago

While technically doable in the library file if you tried hard enough, putting it in the resources and recipes is the correct way. Think of it as part of the DSL, not just a Ruby method :)

djoos commented 6 years ago

Ah ok, makes sense - thank you!