Class: Ridley::DataBagItemObject

Inherits:
ChefObject show all
Defined in:
lib/ridley/chef_objects/data_bag_item_obect.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ChefObject

#<=>, #==, chef_id, #chef_id, chef_json_class, chef_type, #eql?, #hash, #inspect, set_chef_id, set_chef_json_class, set_chef_type

Constructor Details

#initialize(resource, data_bag, new_attrs = {}) ⇒ DataBagItemObject

Returns a new instance of DataBagItemObject.

Parameters:



19
20
21
22
# File 'lib/ridley/chef_objects/data_bag_item_obect.rb', line 19

def initialize(resource, data_bag, new_attrs = {})
  super(resource, new_attrs)
  @data_bag = data_bag
end

Instance Attribute Details

#data_bagRidley::DataBagObject (readonly)



7
8
9
# File 'lib/ridley/chef_objects/data_bag_item_obect.rb', line 7

def data_bag
  @data_bag
end

Instance Method Details

#decryptHash

Decrypts this data bag item.

Returns:

  • (Hash)

    decrypted attributes



45
46
47
48
# File 'lib/ridley/chef_objects/data_bag_item_obect.rb', line 45

def decrypt
  decrypted_hash = Hash[_attributes_.map { |key, value| [key, key == "id" ? value : decrypt_value(value)] }]
  mass_assign(decrypted_hash)
end

#decrypt_value(value) ⇒ Hash

Decrypts an individual value stored inside the data bag item.

Examples:

data_bag_item.decrypt_value("Xk0E8lV9r4BhZzcg4wal0X4w9ZexN3azxMjZ9r1MCZc=")
  => {test: {database: {username: "test"}}}

Parameters:

  • an (String)

    encrypted String value

Returns:

  • (Hash)

    a decrypted attribute value



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ridley/chef_objects/data_bag_item_obect.rb', line 59

def decrypt_value(value)
  if encrypted_data_bag_secret.nil?
    raise Errors::EncryptedDataBagSecretNotSet
  end

  decoded_value = Base64.decode64(value)

  cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
  cipher.decrypt
  cipher.pkcs5_keyivgen(encrypted_data_bag_secret)
  decrypted_value = cipher.update(decoded_value) + cipher.final

  YAML.load(decrypted_value)
end

#from_hash(hash) ⇒ Object

Parameters:

  • hash (#to_hash)

Returns:

  • (Object)


99
100
101
102
103
104
# File 'lib/ridley/chef_objects/data_bag_item_obect.rb', line 99

def from_hash(hash)
  hash = Hashie::Mash.new(hash.to_hash)

  mass_assign(hash.has_key?(:raw_data) ? hash[:raw_data] : hash)
  self
end

#reloadObject

Reload the attributes of the instantiated resource

Returns:

  • (Object)


77
78
79
80
# File 'lib/ridley/chef_objects/data_bag_item_obect.rb', line 77

def reload
  mass_assign(resource.find(data_bag, self)._attributes_)
  self
end

#saveBoolean

Creates a resource on the target remote or updates one if the resource already exists.

Returns:

  • (Boolean)

    true if successful and false for failure

Raises:



32
33
34
35
36
37
38
39
40
# File 'lib/ridley/chef_objects/data_bag_item_obect.rb', line 32

def save
  raise Errors::InvalidResource.new(self.errors) unless valid?

  mass_assign(resource.create(data_bag, self)._attributes_)
  true
rescue Errors::HTTPConflict
  self.update
  true
end

#updateBoolean

Updates the instantiated resource on the target remote with any changes made to self

Returns:

  • (Boolean)

Raises:



89
90
91
92
93
94
# File 'lib/ridley/chef_objects/data_bag_item_obect.rb', line 89

def update
  raise Errors::InvalidResource.new(self.errors) unless valid?

  mass_assign(resource.update(data_bag, self)._attributes_)
  true
end