From: Martyn Taylor <mtaylor(a)redhat.com>
---
.../image_factory/aeolus-image/lib/base_command.rb | 55 ++++++++++++++++++++
.../aeolus-image/sample_data/aeolus-cli | 8 +++
.../aeolus-image/spec/base_command_spec.rb | 24 +++++++++
.../aeolus-image/spec/sample_data/aeolus-cli | 7 +++
.../image_factory/aeolus-image/spec/spec_helper.rb | 9 +++-
5 files changed, 102 insertions(+), 1 deletions(-)
create mode 100644 services/image_factory/aeolus-image/sample_data/aeolus-cli
create mode 100644 services/image_factory/aeolus-image/spec/base_command_spec.rb
create mode 100644 services/image_factory/aeolus-image/spec/sample_data/aeolus-cli
diff --git a/services/image_factory/aeolus-image/lib/base_command.rb
b/services/image_factory/aeolus-image/lib/base_command.rb
index 4d4fa42..54a5ac6 100644
--- a/services/image_factory/aeolus-image/lib/base_command.rb
+++ b/services/image_factory/aeolus-image/lib/base_command.rb
@@ -1,11 +1,17 @@
+require 'yaml'
+require 'rest_client'
+require 'nokogiri'
+
module Aeolus
module Image
#This will house some methods that multiple Command classes need to use.
class BaseCommand
attr_accessor :options
+
def initialize(opts={}, logger=nil)
logger(logger)
@options = opts
+ @config = load_config
end
protected
@@ -19,6 +25,55 @@ module Aeolus
return @logger
end
+ def iwhd
+ create_resource('iwhd')
+ end
+
+ def conductor
+ create_resource('conductor')
+ end
+
+ private
+ def load_config
+ begin
+ @config_location = ENV['HOME'] + "/.aeolus-cli"
+ YAML::load(File.open(@config_location))
+ rescue Errno::ENOENT
+ #TODO: Create a custom exception to wrap CLI Exceptions
+ raise "Unable to locate configuration file: \"" +
@config_location + "\""
+ end
+ end
+
+ def create_resource(resource_name)
+ # Check to see if config has a resource with this name
+ if !(a)config.has_key?(resource_name)
+ raise "Unable to determine resource: " + resource_name + " from
configuration file. Please check: " + @config_location
+ return
+ end
+
+ #Use command line arguments for username/password
+ if @options.has_key?('username')
+ if @options.has_key?('password')
+ RestClient::Resource.new(@config[resource_name]['url'], :user =>
@options['username'], :password => @options['password'])
+ else
+ #TODO: Create a custom exception to wrap CLI Exceptions
+ raise "Password not found for user: " +
@options['username']
+ end
+
+ #Use config for username/password
+ elsif @config[resource_name].has_key?('username')
+ if @config[resource_name].has_key?('password')
+ RestClient::Resource.new(@config[resource_name]['url'], :user =>
@config[resource_name]['username'], :password =>
@config[resource_name]['password'])
+ else
+ #TODO: Create a custom exception to wrap CLI Exceptions
+ raise "Password not found for user: " +
@config[resource_name]['username']
+ end
+
+ # Do not use authentication
+ else
+ RestClient::Resource.new(@config[resource_name]['url'])
+ end
+ end
end
end
end
diff --git a/services/image_factory/aeolus-image/sample_data/aeolus-cli
b/services/image_factory/aeolus-image/sample_data/aeolus-cli
new file mode 100644
index 0000000..f66c3db
--- /dev/null
+++ b/services/image_factory/aeolus-image/sample_data/aeolus-cli
@@ -0,0 +1,8 @@
+# Place this file in: ~/.aeolus-cli
+iwhd:
+ url:
http://localhost:9090
+
+conductor:
+ url:
http://localhost:3000/conductor
+ username: admin
+ password: password
diff --git a/services/image_factory/aeolus-image/spec/base_command_spec.rb
b/services/image_factory/aeolus-image/spec/base_command_spec.rb
new file mode 100644
index 0000000..69e0697
--- /dev/null
+++ b/services/image_factory/aeolus-image/spec/base_command_spec.rb
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+module Aeolus
+ module Image
+ describe BaseCommand do
+ describe BaseCommand do
+ it "should determine the correct credentials for HTTP Authentication"
do
+ basec = BaseCommand.new
+ iwhd = basec.send :iwhd
+ iwhd.user.should == nil
+
+ conductor = basec.send :conductor
+ conductor.user.should == "admin"
+ conductor.password.should == "password"
+
+ basec = BaseCommand.new({'username' => "testusername",
'password'=> "testpassword"})
+ conductor = basec.send :conductor
+ conductor.user.should == "testusername"
+ conductor.password.should == "testpassword"
+ end
+ end
+ end
+ end
+end
\ No newline at end of file
diff --git a/services/image_factory/aeolus-image/spec/sample_data/aeolus-cli
b/services/image_factory/aeolus-image/spec/sample_data/aeolus-cli
new file mode 100644
index 0000000..574a233
--- /dev/null
+++ b/services/image_factory/aeolus-image/spec/sample_data/aeolus-cli
@@ -0,0 +1,7 @@
+iwhd:
+ url:
http://localhost:9090
+
+conductor:
+ url:
http://localhost:3000/conductor
+ username: admin
+ password: password
\ No newline at end of file
diff --git a/services/image_factory/aeolus-image/spec/spec_helper.rb
b/services/image_factory/aeolus-image/spec/spec_helper.rb
index d3adf8d..ad0b5cc 100644
--- a/services/image_factory/aeolus-image/spec/spec_helper.rb
+++ b/services/image_factory/aeolus-image/spec/spec_helper.rb
@@ -32,4 +32,11 @@ end
Spec::Runner.configure do |config|
config.include Helpers
-end
+ config.before(:all) do
+ Aeolus::Image::BaseCommand.class_eval do
+ def load_config
+ YAML::load(File.open(File.join(File.dirname(__FILE__),
"sample_data/aeolus-cli")))
+ end
+ end
+ end
+end
\ No newline at end of file
--
1.7.4