Class: Ridley::DataBagItem
- Inherits:
-
Object
- Object
- Ridley::DataBagItem
- Includes:
- ActiveModel::Serialization, ActiveModel::Validations
- Defined in:
- lib/ridley/resources/data_bag_item.rb
Overview
Instance Attribute Summary collapse
Class Method Summary collapse
- .all(connection, data_bag) ⇒ Array<Object>
- .create(connection, data_bag, object) ⇒ Ridley::DataBagItem
- .delete(connection, data_bag, object) ⇒ Ridley::DataBagItem
- .delete_all(connection, data_bag) ⇒ Array<Ridley::DataBagItem>
- .find(connection, data_bag, object) ⇒ nil, Ridley::DataBagItem
- .find!(connection, data_bag, object) ⇒ Ridley::DataBagItem
- .update(connection, data_bag, object) ⇒ Ridley::DataBagItem
Instance Method Summary collapse
- #attribute(key) ⇒ Object (also: #[])
- #attribute=(key, value) ⇒ Object (also: #[]=)
-
#chef_id ⇒ String
(also: #id)
Alias for accessing the value of the ‘id’ attribute.
-
#decrypt ⇒ Hash
Decrypts this data bag item.
- #decrypt_value(value) ⇒ Object
- #from_hash(hash) ⇒ Object
-
#initialize(connection, data_bag, attributes = {}) ⇒ DataBagItem
constructor
A new instance of DataBagItem.
-
#save ⇒ Boolean
Creates a resource on the target remote or updates one if the resource already exists.
- #to_hash ⇒ Object
- #to_json(options = {}) ⇒ String (also: #as_json)
- #to_s ⇒ Object
Constructor Details
#initialize(connection, data_bag, attributes = {}) ⇒ DataBagItem
Returns a new instance of DataBagItem.
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
#attributes ⇒ HashWithIndifferentAccess
104 105 106 |
# File 'lib/ridley/resources/data_bag_item.rb', line 104 def attributes @attributes end |
#data_bag ⇒ Ridley::DataBag (readonly)
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>
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
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
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>
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
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
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
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: []
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: []=
137 138 139 |
# File 'lib/ridley/resources/data_bag_item.rb', line 137 def attribute=(key, value) @attributes[key] = value end |
#chef_id ⇒ String Also known as: id
Alias for accessing the value of the ‘id’ attribute
120 121 122 |
# File 'lib/ridley/resources/data_bag_item.rb', line 120 def chef_id @attributes[:id] end |
#decrypt ⇒ Hash
Decrypts this data bag item.
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
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 |
#save ⇒ Boolean
Creates a resource on the target remote or updates one if the resource already exists.
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_hash ⇒ Object
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
200 201 202 |
# File 'lib/ridley/resources/data_bag_item.rb', line 200 def to_json( = {}) MultiJson.encode(self.attributes, ) end |
#to_s ⇒ Object
209 210 211 |
# File 'lib/ridley/resources/data_bag_item.rb', line 209 def to_s self.attributes end |