Class: Ridley::DataBagItem

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serialization, ActiveModel::Validations
Defined in:
lib/ridley/resources/data_bag_item.rb

Overview

Author:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, data_bag, attributes = {}) ⇒ DataBagItem

Returns a new instance of DataBagItem.

Parameters:



111
112
113
114
115
# File 'lib/ridley/resources/data_bag_item.rb', line 111

def initialize(connection, data_bag, attributes = {})
  @connection = connection
  @data_bag = data_bag
  self.attributes = attributes
end

Instance Attribute Details

#attributesHashWithIndifferentAccess

Returns:

  • (HashWithIndifferentAccess)


104
105
106
# File 'lib/ridley/resources/data_bag_item.rb', line 104

def attributes
  @attributes
end

#data_bagRidley::DataBag (readonly)

Returns:



102
103
104
# File 'lib/ridley/resources/data_bag_item.rb', line 102

def data_bag
  @data_bag
end

Class Method Details

.all(connection, data_bag) ⇒ Array<Object>

Parameters:

Returns:

  • (Array<Object>)


11
12
13
14
15
# File 'lib/ridley/resources/data_bag_item.rb', line 11

def all(connection, data_bag)
  connection.get("#{data_bag.class.resource_path}/#{data_bag.name}").body.collect do |id, location|
    new(connection, data_bag, id: id)
  end
end

.create(connection, data_bag, object) ⇒ Ridley::DataBagItem

Parameters:

Returns:



46
47
48
49
50
51
52
53
54
55
# File 'lib/ridley/resources/data_bag_item.rb', line 46

def create(connection, data_bag, object)
  resource = new(connection, data_bag, object.to_hash)
  unless resource.valid?
    raise Errors::InvalidResource.new(resource.errors)
  end

  new_attributes = connection.post("#{data_bag.class.resource_path}/#{data_bag.name}", resource.to_json).body
  resource.from_hash(resource.attributes.deep_merge(new_attributes))
  resource
end

.delete(connection, data_bag, object) ⇒ Ridley::DataBagItem

Parameters:

Returns:



62
63
64
65
# File 'lib/ridley/resources/data_bag_item.rb', line 62

def delete(connection, data_bag, object)
  chef_id = object.respond_to?(:chef_id) ? object.chef_id : object
  new(connection, data_bag).from_hash(connection.delete("#{data_bag.class.resource_path}/#{data_bag.name}/#{chef_id}").body)
end

.delete_all(connection, data_bag) ⇒ Array<Ridley::DataBagItem>

Parameters:

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ridley/resources/data_bag_item.rb', line 71

def delete_all(connection, data_bag)
  mutex = Mutex.new
  deleted = []
  resources = all(connection, data_bag)

  connection.thread_count.times.collect do
    Thread.new(connection, data_bag, resources, deleted) do |connection, data_bag, resources, deleted|
      while resource = mutex.synchronize { resources.pop }
        result = delete(connection, data_bag, resource)
        mutex.synchronize { deleted << result }
      end
    end
  end.each(&:join)

  deleted
end

.find(connection, data_bag, object) ⇒ nil, Ridley::DataBagItem

Parameters:

Returns:



22
23
24
25
26
# File 'lib/ridley/resources/data_bag_item.rb', line 22

def find(connection, data_bag, object)
  find!(connection, data_bag, object)
rescue Errors::HTTPNotFound
  nil
end

.find!(connection, data_bag, object) ⇒ Ridley::DataBagItem

Parameters:

Returns:

Raises:



36
37
38
39
# File 'lib/ridley/resources/data_bag_item.rb', line 36

def find!(connection, data_bag, object)
  chef_id = object.respond_to?(:chef_id) ? object.chef_id : object
  new(connection, data_bag).from_hash(connection.get("#{data_bag.class.resource_path}/#{data_bag.name}/#{chef_id}").body)
end

.update(connection, data_bag, object) ⇒ Ridley::DataBagItem

Parameters:

Returns:



93
94
95
96
97
98
# File 'lib/ridley/resources/data_bag_item.rb', line 93

def update(connection, data_bag, object)
  resource = new(connection, data_bag, object.to_hash)
  new(connection, data_bag).from_hash(
    connection.put("#{data_bag.class.resource_path}/#{data_bag.name}/#{resource.chef_id}", resource.to_json).body
  )
end

Instance Method Details

#attribute(key) ⇒ Object Also known as: []

Parameters:

  • key (String, Symbol)

Returns:

  • (Object)


128
129
130
# File 'lib/ridley/resources/data_bag_item.rb', line 128

def attribute(key)
  @attributes[key]
end

#attribute=(key, value) ⇒ Object Also known as: []=

Parameters:

  • key (String, Symbol)
  • value (Object)

Returns:

  • (Object)


137
138
139
# File 'lib/ridley/resources/data_bag_item.rb', line 137

def attribute=(key, value)
  @attributes[key] = value
end

#chef_idString Also known as: id

Alias for accessing the value of the ‘id’ attribute

Returns:

  • (String)


120
121
122
# File 'lib/ridley/resources/data_bag_item.rb', line 120

def chef_id
  @attributes[:id]
end

#decryptHash

Decrypts this data bag item.

Returns:

  • (Hash)

    decrypted attributes



170
171
172
173
# File 'lib/ridley/resources/data_bag_item.rb', line 170

def decrypt
  decrypted_hash = Hash[attributes.map { |key, value| [key, key == "id" ? value : decrypt_value(value)] }]
  self.attributes = HashWithIndifferentAccess.new(decrypted_hash)
end

#decrypt_value(value) ⇒ Object



175
176
177
178
179
180
181
182
183
184
# File 'lib/ridley/resources/data_bag_item.rb', line 175

def decrypt_value(value)
  decoded_value = Base64.decode64(value)

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

  YAML.load(decrypted_value)
end

#from_hash(hash) ⇒ Object

Parameters:

Returns:

  • (Object)


189
190
191
192
193
194
# File 'lib/ridley/resources/data_bag_item.rb', line 189

def from_hash(hash)
  hash = HashWithIndifferentAccess.new(hash.to_hash)

  self.attributes = hash.has_key?(:raw_data) ? hash[:raw_data] : hash
  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:



157
158
159
160
161
162
163
164
165
# File 'lib/ridley/resources/data_bag_item.rb', line 157

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

  self.attributes = self.class.create(connection, data_bag, self).attributes
  true
rescue Errors::HTTPConflict
  self.attributes = self.class.update(connection, data_bag, self).attributes
  true
end

#to_hashObject



205
206
207
# File 'lib/ridley/resources/data_bag_item.rb', line 205

def to_hash
  self.attributes
end

#to_json(options = {}) ⇒ String Also known as: as_json

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :symbolize_keys (Boolean)
  • :adapter (Class, Symbol, String)

Returns:

  • (String)


200
201
202
# File 'lib/ridley/resources/data_bag_item.rb', line 200

def to_json(options = {})
  MultiJson.encode(self.attributes, options)
end

#to_sObject



209
210
211
# File 'lib/ridley/resources/data_bag_item.rb', line 209

def to_s
  self.attributes
end