Class: Chef::Resource::ChefDataBagResource
- Inherits:
-
LWRPBase
- Object
- LWRPBase
- Chef::Resource::ChefDataBagResource
- Defined in:
- lib/chef/resource/chef_data_bag_resource.rb
Overview
A resource that is backed by a data bag in a Chef server somewhere
Class Attribute Summary collapse
-
.databag_name ⇒ Object
The name of the databag to store the item in.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
The key to store this thing under (/data/bag/<<name>>).
Class Method Summary collapse
-
.stored_attribute(attr_name, *args) ⇒ Void
Mark an attribute as stored by adding it to the internal tracking list ChefDataBagResource.stored_attributes and then delegating to LWRPBase#attribute.
-
.stored_attributes ⇒ Array
A list of attributes to be persisted into the databag.
Instance Method Summary collapse
-
#delete ⇒ Void
Delete this entity from the server.
-
#hydrate(chef_server = Cheffish.default_chef_server) ⇒ Object
Load persisted data from the server’s databag.
-
#initialize(name, run_context = nil) ⇒ ChefDataBagResource
constructor
A new instance of ChefDataBagResource.
-
#load_from_hash(hash) ⇒ Object
Load instance variable data from a hash.
- #new_resource ⇒ Object
-
#save ⇒ Void
Save this entity to the server.
-
#storage_hash ⇒ Hash
Convert the values in ChefDataBagResource.stored_attributes to a hash for storing in a databag.
Constructor Details
#initialize(name, run_context = nil) ⇒ ChefDataBagResource
Returns a new instance of ChefDataBagResource.
15 16 17 18 19 |
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 15 def initialize(name, run_context=nil) super Chef::Log.debug("Re-hydrating #{name} from #{self.class.databag_name}...") self.hydrate end |
Class Attribute Details
.databag_name ⇒ Object
The name of the databag to store the item in.
12 13 14 |
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 12 def databag_name @databag_name end |
Instance Attribute Details
#name ⇒ Object (readonly)
The key to store this thing under (/data/bag/<<name>>).
8 9 10 |
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 8 def name @name end |
Class Method Details
.stored_attribute(attr_name, *args) ⇒ Void
Mark an attribute as stored by adding it to the internal tracking list stored_attributes and then delegating to LWRPBase#attribute
38 39 40 41 42 |
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 38 def self.stored_attribute(attr_name, *args) @stored_attributes ||= [] @stored_attributes << attr_name self.attribute attr_name, *args end |
.stored_attributes ⇒ Array
A list of attributes to be persisted into the databag.
23 24 25 |
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 23 def self.stored_attributes @stored_attributes || [] end |
Instance Method Details
#delete ⇒ Void
Delete this entity from the server
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 118 def delete # Clone for inline_resource _name = self.name _databag_name = self.class.databag_name Cheffish.inline_resource(self, @action) do chef_data_bag_item _name do data_bag _databag_name action :delete end end end |
#hydrate(chef_server = Cheffish.default_chef_server) ⇒ Object
Load persisted data from the server’s databag. If the databag does not exist on the server, returns nil.
databag.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 54 def hydrate(chef_server = Cheffish.default_chef_server) chef_api = Cheffish.chef_server_api(chef_server) begin data = chef_api.get("/data/#{self.class.databag_name}/#{name}") load_from_hash(data) Chef::Log.debug("Rehydrating resource from #{self.class.databag_name}/#{name}: #{data}") rescue Net::HTTPServerException => e if e.response.code == '404' nil else raise end end end |
#load_from_hash(hash) ⇒ Object
Load instance variable data from a hash. For each key,value pair, set @<key> to value
72 73 74 75 76 77 |
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 72 def load_from_hash hash hash.each do |k,v| self.instance_variable_set("@#{k}", v) end self end |
#new_resource ⇒ Object
131 132 133 |
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 131 def new_resource self end |
#save ⇒ Void
Save this entity to the server. If you have significant information that could be lost, you should do this as quickly as possible.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 98 def save create_databag_if_needed self.class.databag_name # Clone for inline_resource _databag_name = self.class.databag_name _hash = self.storage_hash _name = self.name Cheffish.inline_resource(self, @action) do chef_data_bag_item _name do data_bag _databag_name raw_data _hash action :create end end end |
#storage_hash ⇒ Hash
Convert the values in stored_attributes to a hash for storing in a databag
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/chef/resource/chef_data_bag_resource.rb', line 81 def storage_hash ignored = [] hash = {} (self.class.stored_attributes - ignored).each do |attr_name| varname = "@#{attr_name.to_s.gsub('@', '')}" key = varname.gsub('@', '') hash[key] = self.instance_variable_get varname end hash end |