Module: Deltacloud::Helpers::Database
Constant Summary
collapse
- DATABASE_COLLECTIONS =
[ "machine_template", "address_template",
"volume_configuration", "volume_template" ]
Instance Method Summary
collapse
Methods included from Drivers
#driver, #driver_class, #driver_class_name, #driver_name, #driver_source_name, #driver_symbol, included, #provider_name
Instance Method Details
#current_db ⇒ Object
This method allows to store things into database based on current driver and provider.
48
49
50
|
# File 'lib/cimi/helpers/database_helper.rb', line 48
def current_db
Deltacloud::Database::Provider.find_or_create(:driver => driver_symbol.to_s, :url => current_provider)
end
|
#current_provider ⇒ Object
40
41
42
|
# File 'lib/cimi/helpers/database_helper.rb', line 40
def current_provider
Thread.current[:provider] || ENV['API_PROVIDER'] || 'default'
end
|
#delete_attributes_for(model) ⇒ Object
27
28
29
30
31
|
# File 'lib/cimi/helpers/database_helper.rb', line 27
def delete_attributes_for(model)
return if test_environment?
entity = get_entity(model)
!entity.nil? && entity.destroy
end
|
In XML serialization the values stored in attrs are arrays, dues to XmlSimple. This method will help extract values from them
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/cimi/helpers/database_helper.rb', line 81
def (name, attrs={})
return unless attrs[name]
if name == 'property'
attrs[name].is_a?(Array) ?
attrs[name].inject({}) { |r, v| r[v['key']] = v['content']; r} :
attrs[name]
else
attrs[name].is_a?(Array) ? attrs[name].first : attrs[name]
end
end
|
#get_entity(model) ⇒ Object
33
34
35
36
37
38
|
# File 'lib/cimi/helpers/database_helper.rb', line 33
def get_entity(model)
current_db.entities_dataset.first(
:be_kind => model.to_entity,
:be_id => model.id
)
end
|
#load_attributes_for(model) ⇒ Object
21
22
23
24
25
|
# File 'lib/cimi/helpers/database_helper.rb', line 21
def load_attributes_for(model)
return {} if test_environment?
entity = get_entity(model)
entity.nil? ? {} : entity.to_hash
end
|
#provides?(entity) ⇒ Boolean
16
17
18
19
|
# File 'lib/cimi/helpers/database_helper.rb', line 16
def provides?(entity)
return true if DATABASE_COLLECTIONS.include? entity
return false
end
|
#store_attributes_for(model, attrs = {}) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/cimi/helpers/database_helper.rb', line 52
def store_attributes_for(model, attrs={})
return if test_environment? or model.nil? or attrs.empty?
return if model.id.nil?
unless entity = get_entity(model)
entity = Deltacloud::Database::Entity.new(
:provider_id => current_db.id,
:be_id => model.id,
:be_kind => model.to_entity
)
end
entity.description = ('description', attrs) if attrs.has_key? 'description'
entity.name = ('name', attrs) if attrs.has_key? 'name'
if attrs.has_key? 'properties'
entity.ent_properties = ('properties', attrs).to_json
elsif attrs.has_key? 'property'
entity.ent_properties = ('property', attrs).to_json
end
entity.exists? ? entity.save_changes : entity.save
entity
end
|
#test_environment? ⇒ Boolean
12
13
14
|
# File 'lib/cimi/helpers/database_helper.rb', line 12
def test_environment?
Deltacloud.test_environment?
end
|