Hi,
We actually have a bunch of approaches to address this problem.
However our favourite one is to use the "cutlery" helper cookbook [1]
and add snippets of code like
# Check that the attribute node["foo"]["bar"]["baz"]["myattr"] exists
and is a string and return it
myattr = RealityForge::AttributeTools.ensure_attribute(node,
"foo.bar.baz.myattr", String)
# Check that the attribute node["foo"]["mOtherAttr"] exists and return it
myOtherAttr = RealityForge::AttributeTools.ensure_attribute(node,
"foo.mOtherAttr")
We can also use the same approach to check contents of databags (pass
databag as first parameter). Of course
"RealityForge::AttributeTools.ensure_attribute" is really long so we
tend to shorten it to "ensure_attribute" via some ruby trickery.
HTH
[1] https://github.com/realityforge/chef-cutlery
--
On Thu, Oct 24, 2013 at 7:39 AM, Sölvi Páll Ásgeirsson < "> > wrote:
> Hello everyone
>
> For some cookbook attributes, you can not set a sensible default.
>
> What i'm doing now is setting the attribute defaults to nil and creating a
> _attribute_validation.rb recipe which is basically a sequence of:
>
> unless foo[:attr]
> Chef::Application.fatal! "foo[:attr] is unset. Override with sane values
> of bar"
> end
>
> This is repetitive and boring. Is there a smarter way of going about this
> which I'm missing?
> Is there a way to do this directly from the attributes/*.rb ?
>
> Thanks alot!
>
> Sölvi Páll Á
Cheers,
Peter Donald
Archive powered by MHonArc 2.6.16.