Class: Tinychef::DataBag

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_nameObject (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_pathObject (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

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/tinychef/data_bag.rb', line 12

def name
  @name
end

#recipeObject (readonly)

Returns the value of attribute recipe.



12
13
14
# File 'lib/tinychef/data_bag.rb', line 12

def recipe
  @recipe
end

#ruby_nameObject (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_pathObject (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

#createObject



31
32
33
34
# File 'lib/tinychef/data_bag.rb', line 31

def create 
  prepare_dir
  create_empty_file
end

#decryptObject



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

#encryptObject



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