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:



21
22
23
24
# File 'lib/ridley/chef_objects/data_bag_item_obect.rb', line 21

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

Instance Attribute Details

#data_bagRidley::DataBagObject (readonly)



9
10
11
# File 'lib/ridley/chef_objects/data_bag_item_obect.rb', line 9

def data_bag
  @data_bag
end

Instance Method Details

#decryptHash

Decrypts this data bag item.

Returns:

  • (Hash)

    decrypted attributes



47
48
49
50
# File 'lib/ridley/chef_objects/data_bag_item_obect.rb', line 47

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



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

def decrypt_value(value)
  case format_version_of(value)
  when 0
    decrypt_v0_value(value)
  when 1
    decrypt_v1_value(value)
  else
    raise NotImplementedError, "Currently decrypting only version 0 & 1 databags are supported"
  end
end

#from_hash(hash) ⇒ Object

Parameters:

  • hash (#to_hash)

Returns:

  • (Object)


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

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)


75
76
77
78
# File 'lib/ridley/chef_objects/data_bag_item_obect.rb', line 75

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:



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

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:



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

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

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