For a given use case, I need to update a node's tags through a custom LWRP. I have tried two approaches:
using 'tag'/'untag'. This yields the error:
No resource or method named `tag' for ...
using node[:tags].concat(<new_tags_array>)
. This works but the tag does not persist, so I can't use it for search.
Is there something that I am missing here?
Thanks
EDIT: Actually, the question is: is it even possible to update node attributes from a provider?
EDIT2: So this correctly sets the tags:
tags = node[:tags]
tags.concat(new_tags)
node.override[:tags] = tags
However, the tags get reset in each chef-client run, so if you check for the existence of any of these new tags before including them (on a second chef-client run), you won't get any tag.
I found what the issue was.
To sum up: the initial problem was that I was not able to use chef's tag/untag methods within a LWRP's provider (there might be a way to do it, but I have not found any), so I opted to modify the 'tags' attribute on the node, which holds all the tags.
It was also important for me that the tags persisted between chef-client runs.
The way to achieve this is to set the attribute type to normal
, which is never reset according to the chef docs:
At the beginning of a chef-client run, all default, override, and automatic attributes are reset. The chef-client rebuilds them using data collected by Ohai at the beginning of the chef-client run and by attributes that are defined in cookbooks, roles, and environments. Normal attributes are never reset. All attributes are then merged and applied to the node according to attribute precedence. At the conclusion of the chef-client run, all default, override, and automatic attributes disappear, leaving only a collection of normal attributes that will persist until the next chef-client run.
And this will make it:
tags = node[:tags]
tags.concat(new_tags)
node.normal[:tags] = tags