Thayne
Thayne

Reputation: 7012

Accumulator pattern for ruby_block or other non-template resource

I want to create a chef accumulator custom resource that accumulates data from multiple instances, and then uses a ruby_block resource to modify an existing file.

All the examples of accumulators that I can find use a template resource or similar where it is possible to accumulate more state in the properties of a sub-resource, such as the variables property of the template resource. But in my case there isn't any appropriate property for the ruby_block resource type.

Is there somewhere else I could store accumulator state, that I could reference in the ruby block? Perhaps a class variable?

For example I would like to do something like this:

action :add do 
    items[new_resource.host] = {'user' => new_resource.user , "password" => new_resource.password}
    path = new_resource.file_path
    with_run_context :root do
        edit_resource(:ruby_block, "set-auth #{path}")
            block do 
                json = JSON.parse(IO.read(path))
                #elided handling if file doesn't exist yet
                json['auths'] = items
                IO.write(path, json.to_json)
            end
        end
    end
end

But I'm not sure how to implement items, especially if I want to keep things separate for different values of the file_path property.

Upvotes: 0

Views: 51

Answers (0)

Related Questions