Refactoring: main wrapper class 'nebula' not globally, configuration to separated...
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Tue, 14 Jun 2016 21:47:17 +0000 (23:47 +0200)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Tue, 14 Jun 2016 21:56:24 +0000 (23:56 +0200)
.rubocop.yml
application.rb
lib/api.rb
lib/config.rb [new file with mode: 0644]
lib/nebula.rb

index e1913b5..af1f2e2 100644 (file)
@@ -28,10 +28,7 @@ Style/FormatString:
 # Do not introduce global variables
 # (do want)
 Style/GlobalVars:
-  Exclude:
-    - application.rb
-    - lib/api.rb
-    - lib/nebula.rb
+  Enabled: false
 
 # Extra empty line detected at class body end
 # (easy to read)
index 62df71d..bd44200 100644 (file)
@@ -6,6 +6,7 @@ end
 require './version'
 require './lib/error'
 require './lib/server_cipher_auth'
+require './lib/config'
 require './lib/nebula'
 require './lib/api'
 
@@ -14,4 +15,4 @@ $logger.formatter = proc do |severity, datetime, _progname, msg|
   date_format = datetime.strftime('%Y-%m-%dT%H:%M:%S%z')
   sprintf "[#{date_format}] %5s: #{msg}\n", severity
 end
-$nebula = Now::Nebula.new()
+$config = Now::Config.new()
index ec05fb8..05735a4 100644 (file)
@@ -11,7 +11,7 @@ module Now
 
     def initialize
       super
-      @nebula = $nebula
+      @nebula = Now::Nebula.new($config)
     end
 
     configure do
@@ -27,9 +27,9 @@ module Now
     helpers do
       def switch_user(user)
         if user.nil?
-          @nebula.switch_server()
+          nebula.switch_server()
         else
-          @nebula.switch_user(user)
+          nebula.switch_user(user)
         end
       end
     end
@@ -43,7 +43,7 @@ module Now
       cross_origin
       begin
         switch_user(params['user'])
-        networks = @nebula.list_networks
+        networks = nebula.list_networks
         JSON.pretty_generate(networks)
       rescue NowError => e
         logger.error "[HTTP #{e.code}] #{e.message}"
@@ -55,7 +55,7 @@ module Now
       cross_origin
       begin
         switch_user(params['user'])
-        network = @nebula.get(params['id'])
+        network = nebula.get(params['id'])
         JSON.pretty_generate(network)
       rescue NowError => e
         logger.error "[HTTP #{e.code}] #{e.message}"
diff --git a/lib/config.rb b/lib/config.rb
new file mode 100644 (file)
index 0000000..86b4b3f
--- /dev/null
@@ -0,0 +1,41 @@
+require 'logger'
+require 'yaml'
+
+module Now
+
+  CONFIG_FILES = [
+    ::File.expand_path('~/.config/now.yml'),
+    '/etc/now.yml',
+    ::File.expand_path('../../etc/now.yml', __FILE__),
+  ]
+
+  # Config class for NOW
+  class Config < Hash
+    attr_accessor :logger
+
+    def load_config(file)
+      c = YAML.load_file(file)
+      logger.debug "Config file '#{file}' loaded"
+      return c
+    rescue Errno::ENOENT
+      logger.debug "Config file '#{file}' not found"
+      return {}
+    end
+
+    def initialize
+      @logger = $logger
+      config = {}
+
+      CONFIG_FILES.each do |path|
+        if ::File.exist?(path)
+          config = load_config(path)
+          break
+        end
+      end
+      #logger.debug "[config] Configuration: #{config}"
+
+      replace config
+    end
+
+  end
+end
index a871d9c..af4ea3c 100644 (file)
@@ -4,36 +4,22 @@ require 'yaml'
 module Now
 
   EXPIRE_LENGTH = 8 * 60 * 60
-  CONFIG_FILES = [
-    ::File.expand_path('~/.config/now.yml'),
-    '/etc/now.yml',
-    ::File.expand_path('../../etc/now.yml', __FILE__),
-  ]
 
   # NOW core class for communication with OpenNebula
   class Nebula
-    attr_accessor :logger
+    attr_accessor :logger, :config
     @ctx = nil
     @server_ctx = nil
     @user_ctx = nil
 
-    def load_config(file)
-      c = YAML.load_file(file)
-      logger.debug "Config file '#{file}' loaded"
-      return c
-    rescue Errno::ENOENT
-      logger.debug "Config file '#{file}' not found"
-      return {}
-    end
-
     def one_connect(url, credentials)
       logger.debug "Connecting to #{url} ..."
       return OpenNebula::Client.new(credentials, url)
     end
 
     def switch_user(user)
-      admin_user = @config['opennebula']['admin_user']
-      admin_password = @config['opennebula']['admin_password']
+      admin_user = config['opennebula']['admin_user']
+      admin_password = config['opennebula']['admin_password']
       logger.debug "Authentication from #{admin_user} to #{user}"
 
       server_auth = ServerCipherAuth.new(admin_user, admin_password)
@@ -45,8 +31,8 @@ module Now
     end
 
     def switch_server
-      admin_user = @config['opennebula']['admin_user']
-      admin_password = @config['opennebula']['admin_password']
+      admin_user = config['opennebula']['admin_user']
+      admin_password = config['opennebula']['admin_password']
       logger.debug "Authentication to #{admin_user}"
 
       direct_token = "#{admin_user}:#{admin_password}"
@@ -54,20 +40,14 @@ module Now
       @ctx = @server_ctx
     end
 
-    def initialize
+    def initialize(config)
       @logger = $logger
       logger.info "Starting Network Orchestrator Wrapper (NOW #{VERSION})"
-      @config = {}
 
-      CONFIG_FILES.each do |path|
-        if ::File.exist?(path)
-          @config = load_config(path)
-          break
-        end
-      end
-      logger.debug "Configuration: #{@config}"
+      @config = config
+      #logger.debug "[nebula] Configuration: #{config}"
 
-      @url = @config['opennebula']['endpoint']
+      @url = config['opennebula']['endpoint']
     end
 
     def list_networks