deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject [2/2] git commit: CIMI: Moved helper methods out of db.rb to separe helper file
Date Mon, 10 Dec 2012 14:01:00 GMT
CIMI: Moved helper methods out of db.rb to separe helper file


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

Branch: refs/heads/master
Commit: 1a1705079b335b38caadc1556629a1a51e48232d
Parents: 5967a99
Author: Michal Fojtik <mfojtik@redhat.com>
Authored: Mon Dec 10 11:08:36 2012 +0100
Committer: Michal fojtik <mfojtik@redhat.com>
Committed: Mon Dec 10 15:00:34 2012 +0100

----------------------------------------------------------------------
 server/lib/cimi/helpers.rb                 |    3 +-
 server/lib/cimi/helpers/database_helper.rb |   46 +++++++++++++++++++
 server/lib/cimi/server.rb                  |    2 +
 server/lib/db.rb                           |   56 ++---------------------
 4 files changed, 54 insertions(+), 53 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/1a170507/server/lib/cimi/helpers.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/helpers.rb b/server/lib/cimi/helpers.rb
index c2f2461..ea71cbf 100644
--- a/server/lib/cimi/helpers.rb
+++ b/server/lib/cimi/helpers.rb
@@ -26,11 +26,12 @@ end
 # Declare namespace for CIMI models
 #
 
-require_relative '../db'
 require_relative '../deltacloud/drivers'
 require_relative '../deltacloud/models'
 require_relative '../deltacloud/helpers/driver_helper'
 require_relative '../deltacloud/helpers/auth_helper'
 require_relative '../deltacloud/helpers/deltacloud_helper'
 require_relative '../deltacloud/helpers/rabbit_helper'
+
 require_relative './helpers/cimi_helper'
+require_relative './helpers/database_helper'

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/1a170507/server/lib/cimi/helpers/database_helper.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/helpers/database_helper.rb b/server/lib/cimi/helpers/database_helper.rb
new file mode 100644
index 0000000..d6aa55b
--- /dev/null
+++ b/server/lib/cimi/helpers/database_helper.rb
@@ -0,0 +1,46 @@
+module Deltacloud
+  module Helpers
+
+    module Database
+
+      def test_environment?
+        Deltacloud.test_environment?
+      end
+
+      def store_attributes_for(model, values={})
+        return if test_environment?
+        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)
+        return {} if test_environment?
+        entity = get_entity(model)
+        entity.nil? ? {} : entity.to_hash
+      end
+
+      def delete_attributes_for(model)
+        return if test_environment?
+        entity = get_entity(model)
+        !entity.nil? && entity.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

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/1a170507/server/lib/cimi/server.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
index b1fc751..02891c5 100644
--- a/server/lib/cimi/server.rb
+++ b/server/lib/cimi/server.rb
@@ -21,6 +21,8 @@ require 'sinatra/base'
 require 'sinatra/rabbit'
 require_relative '../sinatra'
 
+require_relative '../db'
+
 require_relative './helpers'
 require_relative './collections'
 

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/1a170507/server/lib/db.rb
----------------------------------------------------------------------
diff --git a/server/lib/db.rb b/server/lib/db.rb
index ef83462..2a5114d 100644
--- a/server/lib/db.rb
+++ b/server/lib/db.rb
@@ -1,13 +1,7 @@
 module Deltacloud
 
   def self.test_environment?
-    ENV['RACK_ENV'] == 'test'
-  end
-
-  module Database
-    def test_environment?
-      Deltacloud.test_environment?
-    end
+    ENV['RACK_ENV'] == 'test' || ENV['DELTACLOUD_NO_DATABASE']
   end
 
   unless test_environment?
@@ -17,59 +11,17 @@ module Deltacloud
     require_relative './db/machine_template'
   end
 
-  DATABASE_LOCATION = ENV['DATABASE_LOCATION'] || "/var/tmp/deltacloud-mock-#{ENV['USER']}/db.sqlite"
+  DATABASE_LOCATION = ENV['DATABASE_LOCATION'] || File.join('/', 'var', 'tmp', "deltacloud-mock-#{ENV['USER']}",
'db.sqlite')
 
   def self.initialize_database
-    DataMapper::Logger.new($stdout, :debug)
+    DataMapper::Logger.new($stdout, :debug) if ENV['API_VERBOSE']
     dbdir = File::dirname(DATABASE_LOCATION)
-    unless File::directory?(dbdir)
-      FileUtils::mkdir(dbdir)
-    end
+    FileUtils::mkdir(dbdir) unless File::directory?(dbdir)
     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 test_environment?
-        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)
-        return {} if test_environment?
-        entity = get_entity(model)
-        entity.nil? ? {} : entity.to_hash
-      end
-
-      def delete_attributes_for(model)
-        return if test_environment?
-        entity = get_entity(model)
-        !entity.nil? && entity.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 unless Deltacloud.test_environment?


Mime
View raw message