Class: DataMapper::Adapters::ChefAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- DataMapper::Adapters::ChefAdapter
- Defined in:
- lib/dm-chef-adapter/adapter.rb
Instance Method Summary collapse
- #attributes_as_fields(attributes) ⇒ Object
- #create(resources) ⇒ Object
- #delete(collection) ⇒ Object
- #read(query) ⇒ Object
- #update(attributes, collection) ⇒ Object
Instance Method Details
#attributes_as_fields(attributes) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/dm-chef-adapter/adapter.rb', line 105 def attributes_as_fields(attributes) pairs = attributes.map do |property, value| dumped = value.kind_of?(Module) ? value.name : property.dump(value) [ property.field, dumped ] end Hash[pairs] end |
#create(resources) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/dm-chef-adapter/adapter.rb', line 30 def create(resources) resources.collect do |resource| if !Chef::DataBag.list.keys.include?(resource.class.storage_name) databag = Chef::DataBag.new databag.name resource.class.storage_name databag.create end begin resource.id = @uuid.sha1_create( UUIDTools::UUID_DNS_NAMESPACE, ("%10.6f#{resource.class.storage_name}" % Time.now.to_f) ) rescue resource.send :instance_variable_set, :@id, resource.key end if !Chef::DataBag.load(resource.class.storage_name).keys.include?(resource.key.join('_')) databag_item = Chef::DataBagItem.new databag_item.data_bag resource.class.storage_name data = {"id" => resource.key.join('_')} data.merge! Chef::JSONCompat.from_json(resource.to_json) databag_item.raw_data = data databag_item.create if !@cache.nil? @cache.delete(resource.class.storage_name) end else raise "DataBagItem #{resource.class.storage_name}/#{resource.key.join('_')} already exists." end end.count end |
#delete(collection) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/dm-chef-adapter/adapter.rb', line 94 def delete(collection) read(collection.query).each do |doc| databag_item = Chef::DataBagItem.load collection.storage_name, doc["id"] databag_item.destroy collection.storage_name, doc["id"] if !@cache.nil? @cache.delete(collection.storage_name) end end end |
#read(query) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/dm-chef-adapter/adapter.rb', line 62 def read(query) records = records_for(query.model.storage_name) ret = [] query.links.reverse.each do |link| children = records_for(link.child_model.storage_name) parents = records_for(link.parent_model.storage_name) children.each do |child| parents.each do |parent| if child[link.child_key.first.name.to_s] == parent[link.parent_key.first.name.to_s] records.each do |record| ret << child.merge(record) end end end end end query.filter_records(ret+records) end |
#update(attributes, collection) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/dm-chef-adapter/adapter.rb', line 81 def update(attributes, collection) fields = attributes_as_fields(attributes) read(collection.query).each do |doc| databag_item = Chef::DataBagItem.load collection.storage_name, doc["id"] databag_item.raw_data.merge! Chef::JSONCompat.from_json(fields.to_json) databag_item.save if !@cache.nil? @cache.delete(collection.storage_name) end end end |