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.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

#from_hash(hash) ⇒ Object

Parameters:

Returns:

  • (Object)


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

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



186
187
188
# File 'lib/ridley/resources/data_bag_item.rb', line 186

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)


181
182
183
# File 'lib/ridley/resources/data_bag_item.rb', line 181

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

#to_sObject



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

def to_s
  self.attributes
end