deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject [8/8] git commit: CIMI: Initial support for persisting attributes in database
Date Fri, 30 Nov 2012 08:48:25 GMT
CIMI: Initial support for persisting attributes in database

* DataMapper is required for CIMI from this commit.
* This patch support storing of :name, :description and :properties.


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/b47315ab
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/b47315ab
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/b47315ab

Branch: refs/heads/master
Commit: b47315abbcd72a9e0af427d4fedb7c4ef88178b5
Parents: f8cb264
Author: Michal Fojtik <mfojtik@redhat.com>
Authored: Tue Nov 13 11:59:06 2012 +0100
Committer: Michal fojtik <mfojtik@redhat.com>
Committed: Fri Nov 30 09:42:19 2012 +0100

----------------------------------------------------------------------
 server/config.ru                             |    1 +
 server/deltacloud-core.gemspec               |    3 +
 server/lib/cimi/collections/base.rb          |    1 +
 server/lib/cimi/helpers.rb                   |    1 +
 server/lib/cimi/models/base.rb               |   10 ++++
 server/lib/cimi/models/machine.rb            |   21 +++++++-
 server/lib/db.rb                             |   55 +++++++++++++++++++++
 server/lib/db/entity.rb                      |   29 +++++++++++
 server/lib/db/provider.rb                    |   15 ++++++
 server/lib/deltacloud/drivers/base_driver.rb |    4 ++
 server/lib/deltacloud/models/base_model.rb   |    4 ++
 11 files changed, 141 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/b47315ab/server/config.ru
----------------------------------------------------------------------
diff --git a/server/config.ru b/server/config.ru
index efed62f..406c769 100644
--- a/server/config.ru
+++ b/server/config.ru
@@ -54,6 +54,7 @@ end
 #       different root_url's
 #
 frontends.each do |frontend|
+  frontend = frontend.strip
   if Deltacloud[frontend.to_sym].nil?
     puts "ERROR: Unknown frontend (#{frontend}). Valid values are 'deltacloud,cimi,ec2'"
     exit(1)

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/b47315ab/server/deltacloud-core.gemspec
----------------------------------------------------------------------
diff --git a/server/deltacloud-core.gemspec b/server/deltacloud-core.gemspec
index 2c4f0f8..cecc154 100644
--- a/server/deltacloud-core.gemspec
+++ b/server/deltacloud-core.gemspec
@@ -79,6 +79,9 @@ Gem::Specification.new do |s|
   s.add_dependency('nokogiri', '>= 1.4.3')
   s.add_dependency('require_relative') if RUBY_VERSION < '1.9'
 
+  s.add_dependency('data_mapper')
+  s.add_dependency('dm-sqlite-adapter')
+
   # dependencies for various cloud providers:
 
   # RHEV-M and oVirt

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/b47315ab/server/lib/cimi/collections/base.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/base.rb b/server/lib/cimi/collections/base.rb
index a78b78a..30c595b 100644
--- a/server/lib/cimi/collections/base.rb
+++ b/server/lib/cimi/collections/base.rb
@@ -23,6 +23,7 @@ module CIMI::Collections
     include CIMI::Model
 
     helpers Deltacloud::Helpers::Drivers
+    helpers Deltacloud::Helpers::Database
     helpers Sinatra::AuthHelper
     helpers Sinatra::Rabbit::URLHelper
     helpers Deltacloud::Helpers::Application

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/b47315ab/server/lib/cimi/helpers.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/helpers.rb b/server/lib/cimi/helpers.rb
index 9d069b1..c2f2461 100644
--- a/server/lib/cimi/helpers.rb
+++ b/server/lib/cimi/helpers.rb
@@ -26,6 +26,7 @@ end
 # Declare namespace for CIMI models
 #
 
+require_relative '../db'
 require_relative '../deltacloud/drivers'
 require_relative '../deltacloud/models'
 require_relative '../deltacloud/helpers/driver_helper'

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/b47315ab/server/lib/cimi/models/base.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models/base.rb b/server/lib/cimi/models/base.rb
index 6a73cf6..a5d8f34 100644
--- a/server/lib/cimi/models/base.rb
+++ b/server/lib/cimi/models/base.rb
@@ -285,4 +285,14 @@ class CIMI::Model::Base < CIMI::Model::Resource
     end
     self.class.new(attrs)
   end
+
+  class << self
+    def store_attributes_for(context, entity, attrs={})
+      stored_attributes = {}
+      stored_attributes[:description] = attrs['description'] if attrs['description']
+      stored_attributes[:name] = attrs['name'] if attrs['name']
+      stored_attributes[:ent_properties] = attrs['properties'].to_json if attrs['properties']
+      context.store_attributes_for(entity, stored_attributes)
+    end
+  end
 end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/b47315ab/server/lib/cimi/models/machine.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models/machine.rb b/server/lib/cimi/models/machine.rb
index 21e0081..07c5a17 100644
--- a/server/lib/cimi/models/machine.rb
+++ b/server/lib/cimi/models/machine.rb
@@ -58,6 +58,10 @@ class CIMI::Model::Machine < CIMI::Model::Base
     instance = context.driver.create_instance(context.credentials, image_id, {
       :hwp_id => hardware_profile_id
     }.merge(additional_params))
+
+    # Store attributes that are not supported by the backend cloud to local
+    # database:
+    store_attributes_for(context, instance, json)
     from_instance(instance, context)
   end
 
@@ -74,6 +78,10 @@ class CIMI::Model::Machine < CIMI::Model::Base
     instance = context.driver.create_instance(context.credentials, image_id, {
       :hwp_id => hardware_profile_id
     }.merge(additional_params))
+
+    # Store attributes that are not supported by the backend cloud to local
+    # database:
+    store_attributes_for(context, instance, xml)
     from_instance(instance, context)
   end
 
@@ -90,6 +98,7 @@ class CIMI::Model::Machine < CIMI::Model::Base
   end
 
   def self.delete!(id, context)
+    context.delete_attributes_for Instance.new(:id => id)
     context.driver.destroy_instance(context.credentials, id)
   end
 
@@ -120,10 +129,16 @@ class CIMI::Model::Machine < CIMI::Model::Base
   def self.from_instance(instance, context)
     cpu =  memory = (instance.instance_profile.id == "opaque")? "n/a" : nil
     machine_conf = CIMI::Model::MachineConfiguration.find(instance.instance_profile.name,
context)
+    stored_attributes = context.load_attributes_for(instance)
+    if stored_attributes[:property]
+      stored_attributes[:property].merge!(convert_instance_properties(instance, context))
+    else
+      stored_attributes[:property] = convert_instance_properties(instance, context)
+    end
     machine_spec = {
       :name => instance.name,
-      :description => "Instance #{instance.name}",
       :created => instance.launch_time.nil? ? Time.now.xmlschema : Time.parse(instance.launch_time.to_s).xmlschema,
+      :description => "No description set for Machine #{instance.name}",
       :id => context.machine_url(instance.id),
       :state => convert_instance_state(instance.state),
       :cpu => cpu || convert_instance_cpu(instance.instance_profile, context),
@@ -131,8 +146,8 @@ class CIMI::Model::Machine < CIMI::Model::Base
       :disks => { :href => context.machine_url(instance.id)+"/disks"},
       :volumes => { :href=>context.machine_url(instance.id)+"/volumes"},
       :operations => convert_instance_actions(instance, context),
-      :property => convert_instance_properties(instance, context)
-    }
+      :property => stored_attributes
+    }.merge(stored_attributes)
     if context.expand? :disks
       machine_spec[:disks] = CIMI::Model::Disk.find(instance, machine_conf, context, :all)
     end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/b47315ab/server/lib/db.rb
----------------------------------------------------------------------
diff --git a/server/lib/db.rb b/server/lib/db.rb
new file mode 100644
index 0000000..e0f28d4
--- /dev/null
+++ b/server/lib/db.rb
@@ -0,0 +1,55 @@
+module Deltacloud
+
+  require 'data_mapper'
+
+  require_relative './db/provider'
+  require_relative './db/entity'
+
+  DATABASE_LOCATION = ENV['DATABASE_LOCATION'] || "/var/tmp/deltacloud-mock-#{ENV['USER']}/db.sqlite"
+
+  def self.initialize_database
+    DataMapper::Logger.new($stdout, :debug)
+    DataMapper::setup(:default, "sqlite://#{DATABASE_LOCATION}")
+    DataMapper::finalize
+    DataMapper::auto_upgrade!
+  end
+
+  module Helpers
+    module Database
+      include Deltacloud::Database
+
+      def store_attributes_for(model, values={})
+        return if model.nil? or values.empty?
+        current_db.entities.first_or_create(:be_kind => model.to_entity, :be_id =>
model.id).update(values)
+      end
+
+      def load_attributes_for(model)
+        entity = get_entity(model)
+        entity.nil? ? {} : entity.to_hash
+      end
+
+      def delete_attributes_for(model)
+        get_entity(model).destroy
+      end
+
+      def get_entity(model)
+        current_db.entities.first(:be_kind => model.to_entity, :be_id => model.id)
+      end
+
+      def current_provider
+        Thread.current[:provider] || ENV['API_PROVIDER'] || 'default'
+      end
+
+      # This method allows to store things into database based on current driver
+      # and provider.
+      #
+      def current_db
+        Provider.first_or_create(:driver => driver_symbol.to_s, :url => current_provider)
+      end
+
+    end
+  end
+
+end
+
+Deltacloud::initialize_database

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/b47315ab/server/lib/db/entity.rb
----------------------------------------------------------------------
diff --git a/server/lib/db/entity.rb b/server/lib/db/entity.rb
new file mode 100644
index 0000000..b220f9e
--- /dev/null
+++ b/server/lib/db/entity.rb
@@ -0,0 +1,29 @@
+module Deltacloud
+  module Database
+
+    class Entity
+      include DataMapper::Resource
+
+      belongs_to :provider
+
+      property :id, Serial
+      property :be_kind, String, :required => true # => Machine, MachineImage, ...
+      property :be_id, String, :required => true # => Original Machine 'id'
+
+      property :ent_properties, Json
+
+      property :name, String
+      property :description, String
+
+      def to_hash
+        retval = {}
+        retval.merge!(:name => self.name) if !self.name.nil?
+        retval.merge!(:description => self.description) if !self.description.nil?
+        retval.merge!(:property => self.ent_properties) if !self.ent_properties.nil?
+        retval
+      end
+
+    end
+
+  end
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/b47315ab/server/lib/db/provider.rb
----------------------------------------------------------------------
diff --git a/server/lib/db/provider.rb b/server/lib/db/provider.rb
new file mode 100644
index 0000000..6a761e5
--- /dev/null
+++ b/server/lib/db/provider.rb
@@ -0,0 +1,15 @@
+module Deltacloud
+  module Database
+
+    class Provider
+      include DataMapper::Resource
+
+      property :id, Serial
+      property :driver, String, :required => true
+      property :url, Text
+
+      has n, :entities
+    end
+
+  end
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/b47315ab/server/lib/deltacloud/drivers/base_driver.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/drivers/base_driver.rb b/server/lib/deltacloud/drivers/base_driver.rb
index cfb689d..b828d33 100644
--- a/server/lib/deltacloud/drivers/base_driver.rb
+++ b/server/lib/deltacloud/drivers/base_driver.rb
@@ -60,6 +60,10 @@ module Deltacloud
       features.has_key?(collection) and features[collection].include?(feature_name)
     end
 
+    def has_feature?(collection, feature_name)
+      self.class.has_feature?(collection, feature_name)
+    end
+
     def name
       self.class.name.split('::').last.gsub('Driver', '').downcase
     end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/b47315ab/server/lib/deltacloud/models/base_model.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/models/base_model.rb b/server/lib/deltacloud/models/base_model.rb
index 5c19ad5..2553042 100644
--- a/server/lib/deltacloud/models/base_model.rb
+++ b/server/lib/deltacloud/models/base_model.rb
@@ -44,4 +44,8 @@ class BaseModel
     @id
   end
 
+  def to_entity
+    self.class.name.downcase
+  end
+
 end


Mime
View raw message