deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 3/8] CIMI: Updated CIMI models to use Sequel methods and helpers
Date Wed, 16 Jan 2013 11:58:28 GMT
From: Michal Fojtik <mfojtik@redhat.com>


Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/lib/cimi/models/address_template.rb     | 25 ++++------------
 server/lib/cimi/models/machine.rb              |  2 +-
 server/lib/cimi/models/machine_template.rb     | 25 ++++------------
 server/lib/cimi/models/volume_configuration.rb | 25 ++++------------
 server/lib/cimi/models/volume_template.rb      | 40 ++++++++++----------------
 5 files changed, 34 insertions(+), 83 deletions(-)

diff --git a/server/lib/cimi/models/address_template.rb b/server/lib/cimi/models/address_template.rb
index 2d81f23..1e28602 100644
--- a/server/lib/cimi/models/address_template.rb
+++ b/server/lib/cimi/models/address_template.rb
@@ -42,20 +42,13 @@ class CIMI::Model::AddressTemplate < CIMI::Model::Base
       if context.driver.respond_to? :address_templates
         context.driver.address_templates(context.credentials, {:env=>context})
       else
-        Deltacloud::Database::AddressTemplate.all(
-          'provider.driver' => driver_symbol.to_s,
-          'provider.url' => current_provider
-        ).map { |t| from_db(t, context) }
+        current_db.address_teplates.map { |t| from_db(t, context) }
       end
     else
       if context.driver.respond_to? :address_template
         context.driver.address_template(context.credentials, id, :env=>context)
       else
-        template = Deltacloud::Database::AddressTemplate.first(
-          'provider.driver' => driver_symbol.to_s,
-          'provider.url' => current_provider,
-          :id => id
-        )
+        template = current_db.address_templates_dataset.first(:id => id)
         raise CIMI::Model::NotFound unless template
         from_db(template, context)
       end
@@ -64,7 +57,7 @@ class CIMI::Model::AddressTemplate < CIMI::Model::Base
 
   def self.create_from_json(body, context)
     json = JSON.parse(body)
-    new_template = current_db.address_templates.new(
+    new_template = current_db.add_address_template(
       :name => json['name'],
       :description => json['description'],
       :hostname => json['hostname'],
@@ -75,17 +68,14 @@ class CIMI::Model::AddressTemplate < CIMI::Model::Base
       :protocol => json['protocol'],
       :mask => json['mask'],
       :ent_properties => json['properties'].to_json,
-      :be_kind => 'address_template',
-      :be_id => ''
     )
-    new_template.save
     from_db(new_template, context)
   end
 
   def self.create_from_xml(body, context)
     xml = XmlSimple.xml_in(body)
     xml['property'] ||= []
-    new_template = current_db.address_templates.new(
+    new_template = current_db.add_address_template(
       :name => xml['name'].first,
       :description => xml['description'].first,
       :ip => xml['ip'].first,
@@ -95,11 +85,8 @@ class CIMI::Model::AddressTemplate < CIMI::Model::Base
       :dns => xml['dns'].first,
       :protocol => xml['protocol'].nil? ? nil : xml['protocol'].first,
       :mask => xml['mask'].first,
-      :ent_properties => xml['property'].inject({}) { |r, p| r[p['key']]=p['content'];
r },
-      :be_kind => 'machine_template',
-      :be_id => ''
+      :ent_properties => JSON::dump(xml['property'].inject({}) { |r, p| r[p['key']]=p['content'];
r }),
     )
-    new_template.save
     from_db(new_template, context)
   end
 
@@ -121,7 +108,7 @@ class CIMI::Model::AddressTemplate < CIMI::Model::Base
       :dns => model.dns,
       :protocol => model.protocol,
       :mask => model.mask,
-      :property => model.ent_properties,
+      :property => JSON::parse(model.ent_properties),
       :operations => [
         { :href => context.destroy_address_template_url(model.id), :rel => 'http://schemas.dmtf.org/cimi/1/action/delete'
}
       ]
diff --git a/server/lib/cimi/models/machine.rb b/server/lib/cimi/models/machine.rb
index 7ac8f83..67e3eef 100644
--- a/server/lib/cimi/models/machine.rb
+++ b/server/lib/cimi/models/machine.rb
@@ -81,7 +81,7 @@ class CIMI::Model::Machine < CIMI::Model::Base
   def self.create_from_xml(body, context)
     xml = XmlSimple.xml_in(body)
     if xml['machineTemplate'][0]['href']
-      template = current_db.machine_templates.first(:id => xml['machineTemplate'][0]['href'].split('/').last)
+      template = current_db.machine_templates_dataset.first(:id => xml['machineTemplate'][0]['href'].split('/').last)
       hardware_profile_id = template.machine_config.split('/').last
       image_id = template.machine_image.split('/').last
     else
diff --git a/server/lib/cimi/models/machine_template.rb b/server/lib/cimi/models/machine_template.rb
index eb660d2..675f709 100644
--- a/server/lib/cimi/models/machine_template.rb
+++ b/server/lib/cimi/models/machine_template.rb
@@ -44,16 +44,9 @@ class CIMI::Model::MachineTemplate < CIMI::Model::Base
   class << self
     def find(id, context)
       if id == :all
-        Deltacloud::Database::MachineTemplate.all(
-          'provider.driver' => driver_symbol.to_s,
-          'provider.url' => current_provider
-        ).map { |t| from_db(t, context) }
+        current_db.machine_templates.map { |t| from_db(t, context) }
       else
-        template = Deltacloud::Database::MachineTemplate.first(
-          'provider.driver' => driver_symbol.to_s,
-          'provider.url' => current_provider,
-          :id => id
-        )
+        template = current_db.machine_templates_dataset.first(:id => id)
         raise CIMI::Model::NotFound unless template
         from_db(template, context)
       end
@@ -61,31 +54,25 @@ class CIMI::Model::MachineTemplate < CIMI::Model::Base
 
     def create_from_json(body, context)
       json = JSON.parse(body)
-      new_template = current_db.machine_templates.new(
+      new_template = current_db.add_machine_template(
         :name => json['name'],
         :description => json['description'],
         :machine_config => json['machineConfig']['href'],
         :machine_image => json['machineImage']['href'],
         :ent_properties => json['properties'].to_json,
-        :be_kind => 'machine_template',
-        :be_id => ''
       )
-      new_template.save
       from_db(new_template, context)
     end
 
     def create_from_xml(body, context)
       xml = XmlSimple.xml_in(body)
-      new_template = current_db.machine_templates.new(
+      new_template = current_db.add_machine_template(
         :name => xml['name'].first,
         :description => xml['description'].first,
         :machine_config => xml['machineConfig'].first['href'],
         :machine_image => xml['machineImage'].first['href'],
-        :ent_properties => xml['property'].inject({}) { |r, p| r[p['key']]=p['content'];
r },
-        :be_kind => 'machine_template',
-        :be_id => ''
+        :ent_properties => JSON::dump(xml['property'].inject({}) { |r, p| r[p['key']]=p['content'];
r }),
       )
-      new_template.save
       from_db(new_template, context)
     end
 
@@ -102,7 +89,7 @@ class CIMI::Model::MachineTemplate < CIMI::Model::Base
         :description => model.description,
         :machine_config => { :href => model.machine_config },
         :machine_image => { :href => model.machine_image },
-        :property => model.ent_properties,
+        :property => JSON::parse(model.ent_properties),
         :created => Time.parse(model.created_at.to_s).xmlschema,
         :operations => [
           { :href => context.destroy_machine_template_url(model.id), :rel => 'http://schemas.dmtf.org/cimi/1/action/delete'
}
diff --git a/server/lib/cimi/models/volume_configuration.rb b/server/lib/cimi/models/volume_configuration.rb
index b0ba85b..12a3198 100644
--- a/server/lib/cimi/models/volume_configuration.rb
+++ b/server/lib/cimi/models/volume_configuration.rb
@@ -27,32 +27,26 @@ class CIMI::Model::VolumeConfiguration < CIMI::Model::Base
 
   def self.create_from_json(body, context)
     json = JSON.parse(body)
-    new_config = current_db.volume_configurations.new(
+    new_config = current_db.add_volume_configuration(
       :name => json['name'],
       :description => json['description'],
       :format => json['format'],
       :capacity => json['capacity'],
       :ent_properties => json['properties'].to_json,
-      :be_kind => 'volume_configuration',
-      :be_id => ''
     )
-    new_config.save
     from_db(new_config, context)
   end
 
   def self.create_from_xml(body, context)
     xml = XmlSimple.xml_in(body)
     xml['property'] ||= []
-    new_config = current_db.volume_configurations.new(
+    new_config = current_db.add_volume_configuration(
       :name => xml['name'].first,
       :description => xml['description'].first,
       :format => xml['format'].first,
       :capacity => xml['capacity'].first,
-      :ent_properties => xml['property'].inject({}) { |r, p| r[p['key']]=p['content'];
r },
-      :be_kind => 'volume_configuration',
-      :be_id => ''
+      :ent_properties => JSON::dump(xml['property'].inject({}) { |r, p| r[p['key']]=p['content'];
r }),
     )
-    new_config.save
     from_db(new_config, context)
   end
 
@@ -65,20 +59,13 @@ class CIMI::Model::VolumeConfiguration < CIMI::Model::Base
       if context.driver.respond_to? :volume_configurations
         context.driver.volume_configurations(context.credentials, {:env=>context})
       else
-        Deltacloud::Database::VolumeConfiguration.all(
-          'provider.driver' => driver_symbol.to_s,
-          'provider.url' => current_provider
-        ).map { |t| from_db(t, context) }
+        current_db.volume_configurations.map { |t| from_db(t, context) }
       end
     else
       if context.driver.respond_to? :volume_configuration
         context.driver.volume_configuration(context.credentials, id, :env=>context)
       else
-        config = Deltacloud::Database::VolumeConfiguration.first(
-          'provider.driver' => driver_symbol.to_s,
-          'provider.url' => current_provider,
-          :id => id
-        )
+        config = current_db.volume_configurations_dataset.first(:id => id)
         raise CIMI::Model::NotFound unless config
         from_db(config, context)
       end
@@ -94,7 +81,7 @@ class CIMI::Model::VolumeConfiguration < CIMI::Model::Base
       :description => model.description,
       :format => model.format,
       :capacity => context.to_kibibyte(model.capacity, "GB"),
-      :property => model.ent_properties,
+      :property => JSON::parse(model.ent_properties),
       :operations => [
         { :href => context.destroy_volume_configuration_url(model.id), :rel => 'http://schemas.dmtf.org/cimi/1/action/delete'
}
       ]
diff --git a/server/lib/cimi/models/volume_template.rb b/server/lib/cimi/models/volume_template.rb
index a2b8eb7..47bbb3f 100644
--- a/server/lib/cimi/models/volume_template.rb
+++ b/server/lib/cimi/models/volume_template.rb
@@ -30,27 +30,20 @@ class CIMI::Model::VolumeTemplate < CIMI::Model::Base
   end
 
   def self.find(id, context)
-   if id==:all
-     if context.driver.respond_to? :volume_templates
-       context.driver.volume_templates(context.credentials, {:env=>context})
-     else
-       Deltacloud::Database::VolumeTemplate.all(
-         'provider.driver' => driver_symbol.to_s,
-         'provider.url' => current_provider
-       ).map { |t| from_db(t, context) }
+    if id==:all
+      if context.driver.respond_to? :volume_templates
+        context.driver.volume_templates(context.credentials, {:env=>context})
+      else
+        current_db.volume_templates.map { |t| from_db(t, context) }
       end
     else
-     if context.driver.respond_to? :volume_template
-       context.driver.volume_template(context.credentials, id, :env=>context)
-     else
-       template = Deltacloud::Database::VolumeTemplate.first(
-         'provider.driver' => driver_symbol.to_s,
-         'provider.url' => current_provider,
-         :id => id
-       )
-       raise CIMI::Model::NotFound unless template
-       from_db(template, context)
-     end
+      if context.driver.respond_to? :volume_template
+        context.driver.volume_template(context.credentials, id, :env=>context)
+      else
+        template = current_db.volume_templates_dataset.first(:id => id)
+        raise CIMI::Model::NotFound unless template
+        from_db(template, context)
+      end
     end
   end
 
@@ -58,16 +51,13 @@ class CIMI::Model::VolumeTemplate < CIMI::Model::Base
     input = (type == :xml)? XmlSimple.xml_in(body, {"ForceArray"=>false,"NormaliseSpace"=>2})
: JSON.parse(body)
     input['property'] ||= []
     vol_image = input['volumeImage']['href'] if input['volumeImage']
-    new_template = current_db.volume_templates.new(
+    new_template = current_db.add_volume_template(
       :name => input['name'],
       :description => input['description'],
       :volume_config => input['volumeConfig']['href'],
       :volume_image => vol_image,
-      :ent_properties => input['property'].inject({}) { |r, p| r[p['key']]=p['content'];
r },
-      :be_kind => 'volume_template',
-      :be_id => ''
+      :ent_properties => JSON::dump(input['property'].inject({}) { |r, p| r[p['key']]=p['content'];
r }),
     )
-    new_template.save
     from_db(new_template, context)
   end
 
@@ -84,7 +74,7 @@ private
       :description => model.description,
       :volume_config => {:href => model.volume_config},
       :volume_image => {:href => model.volume_image},
-      :property => model.ent_properties,
+      :property => JSON::parse(model.ent_properties),
       :operations => [
         { :href => context.destroy_volume_template_url(model.id), :rel => 'http://schemas.dmtf.org/cimi/1/action/delete'
}
       ]
-- 
1.8.0.2


Mime
View raw message