Class: Tinychef::DataBag
- Inherits:
-
Object
- Object
- Tinychef::DataBag
- Defined in:
- lib/tinychef/data_bag.rb
Constant Summary collapse
- OPTIONS_ERROR =
<<-EOH Required arguments: RECIPE: Databags must be archived under the name of a recipe. NAME: Name of this databag. EOH
Instance Attribute Summary collapse
-
#json_name ⇒ Object
readonly
Returns the value of attribute json_name.
-
#json_path ⇒ Object
readonly
Returns the value of attribute json_path.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#recipe ⇒ Object
readonly
Returns the value of attribute recipe.
-
#ruby_name ⇒ Object
readonly
Returns the value of attribute ruby_name.
-
#ruby_path ⇒ Object
readonly
Returns the value of attribute ruby_path.
Instance Method Summary collapse
- #create ⇒ Object
- #decrypt ⇒ Object
- #encrypt ⇒ Object
-
#initialize(recipe, name) ⇒ DataBag
constructor
A new instance of DataBag.
Constructor Details
#initialize(recipe, name) ⇒ DataBag
Returns a new instance of DataBag.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/tinychef/data_bag.rb', line 14 def initialize(recipe, name) if recipe.nil? || name.nil? raise OptionsError.new OPTIONS_ERROR end @name = name @ruby_name = "#{name}.rb" @json_name = "#{name}.json" @recipe = recipe @dir = Pathname.new(File.join('data_bags', @recipe)) @ruby_path = @dir.join @ruby_name @json_path = @dir.join @json_name end |
Instance Attribute Details
#json_name ⇒ Object (readonly)
Returns the value of attribute json_name.
12 13 14 |
# File 'lib/tinychef/data_bag.rb', line 12 def json_name @json_name end |
#json_path ⇒ Object (readonly)
Returns the value of attribute json_path.
12 13 14 |
# File 'lib/tinychef/data_bag.rb', line 12 def json_path @json_path end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/tinychef/data_bag.rb', line 12 def name @name end |
#recipe ⇒ Object (readonly)
Returns the value of attribute recipe.
12 13 14 |
# File 'lib/tinychef/data_bag.rb', line 12 def recipe @recipe end |
#ruby_name ⇒ Object (readonly)
Returns the value of attribute ruby_name.
12 13 14 |
# File 'lib/tinychef/data_bag.rb', line 12 def ruby_name @ruby_name end |
#ruby_path ⇒ Object (readonly)
Returns the value of attribute ruby_path.
12 13 14 |
# File 'lib/tinychef/data_bag.rb', line 12 def ruby_path @ruby_path end |
Instance Method Details
#create ⇒ Object
31 32 33 34 |
# File 'lib/tinychef/data_bag.rb', line 31 def create prepare_dir create_empty_file end |
#decrypt ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/tinychef/data_bag.rb', line 36 def decrypt data = JSON.parse File.read(json_path) secret = Chef::EncryptedDataBagItem.load_secret( key.path ) encrypted_data = Chef::EncryptedDataBagItem.new(data, secret) new_hash = {} data.keys.each do |key| new_hash[key] = encrypted_data[key] end file = File.open ruby_path, 'w' file.write new_hash.inspect file.close end |
#encrypt ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/tinychef/data_bag.rb', line 49 def encrypt data = eval File.read(ruby_path) secret = Chef::EncryptedDataBagItem.load_secret( key.path ) encrypted_data = Chef::EncryptedDataBagItem.encrypt_data_bag_item(data, secret) file = File.open json_path, 'w' file.write encrypted_data.to_json file.close end |