Class: Ridley::DataBagItemResource

Inherits:
Resource
  • Object
show all
Defined in:
lib/ridley/resources/data_bag_item_resource.rb

Overview

Author:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#<=>, #==, chef_id, chef_json_class, chef_type, #eql?, #hash, resource_path, set_chef_id, set_chef_json_class, set_chef_type, set_resource_path, #to_s

Constructor Details

#initialize(client, data_bag, new_attrs = {}) ⇒ DataBagItemResource


107
108
109
110
# File 'lib/ridley/resources/data_bag_item_resource.rb', line 107

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

Instance Attribute Details

#data_bagRidley::DataBagResource (readonly)


95
96
97
# File 'lib/ridley/resources/data_bag_item_resource.rb', line 95

def data_bag
  @data_bag
end

Class Method Details

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


8
9
10
11
12
# File 'lib/ridley/resources/data_bag_item_resource.rb', line 8

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

.create(client, data_bag, object) ⇒ Ridley::DataBagItemResource


43
44
45
46
47
48
49
50
51
52
# File 'lib/ridley/resources/data_bag_item_resource.rb', line 43

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

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

.delete(client, data_bag, object) ⇒ Ridley::DataBagItemResource


59
60
61
62
# File 'lib/ridley/resources/data_bag_item_resource.rb', line 59

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

.delete_all(client, data_bag) ⇒ Array<Ridley::DataBagItemResource>


68
69
70
71
72
73
74
75
76
77
# File 'lib/ridley/resources/data_bag_item_resource.rb', line 68

def delete_all(client, data_bag)
  mutex = Mutex.new
  deleted = []

  all(client, data_bag).collect do |resource|
    Celluloid::Future.new {
      delete(client, data_bag, resource)
    }
  end.map(&:value)
end

.find(client, data_bag, object) ⇒ nil, Ridley::DataBagItemResource


19
20
21
22
23
# File 'lib/ridley/resources/data_bag_item_resource.rb', line 19

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

.find!(client, data_bag, object) ⇒ Ridley::DataBagItemResource

Raises:


33
34
35
36
# File 'lib/ridley/resources/data_bag_item_resource.rb', line 33

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

.update(client, data_bag, object) ⇒ Ridley::DataBagItemResource


84
85
86
87
88
89
# File 'lib/ridley/resources/data_bag_item_resource.rb', line 84

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

Instance Method Details

#chef_idString

Alias for accessing the value of the 'id' attribute


115
116
117
# File 'lib/ridley/resources/data_bag_item_resource.rb', line 115

def chef_id
  get_attribute(:id)
end

#decryptHash

Decrypts this data bag item.


140
141
142
143
# File 'lib/ridley/resources/data_bag_item_resource.rb', line 140

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

#decrypt_value(value) ⇒ Object


145
146
147
148
149
150
151
152
153
154
# File 'lib/ridley/resources/data_bag_item_resource.rb', line 145

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

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

  YAML.load(decrypted_value)
end

#from_hash(hash) ⇒ Object


181
182
183
184
185
186
# File 'lib/ridley/resources/data_bag_item_resource.rb', line 181

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


159
160
161
162
# File 'lib/ridley/resources/data_bag_item_resource.rb', line 159

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

#saveBoolean

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

Raises:


127
128
129
130
131
132
133
134
135
# File 'lib/ridley/resources/data_bag_item_resource.rb', line 127

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

  mass_assign(self.class.create(client, 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

Raises:


171
172
173
174
175
176
# File 'lib/ridley/resources/data_bag_item_resource.rb', line 171

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

  mass_assign(self.class.update(client, data_bag, self)._attributes_)
  true
end