Skip to content

Commit

Permalink
Merge pull request #43 from site5/feature/instance-configuration
Browse files Browse the repository at this point in the history
allow instance configuration
  • Loading branch information
boone committed Jul 9, 2013
2 parents 8391030 + c667838 commit ca18f9b
Show file tree
Hide file tree
Showing 23 changed files with 185 additions and 213 deletions.
6 changes: 3 additions & 3 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ Rubinius 2.0, and JRuby 1.6.2.
Basic Examples
--------------

Solusvm.config('api_id', 'api_password', :url => 'http://www.example.com/api')
server = Solusvm::Server.new
server = Solusvm::Server.new(api_key: 'key', api_id: 'id', url: 'url')

# 200 is the id of the virtual server
server.shutdown(200) # => true
Expand Down Expand Up @@ -152,6 +151,7 @@ Contributors
* [Maran H.](http://github.com/maran)
* [Joshua Priddle](http://github.com/itspriddle)
* [Vince Stratful](http://github.com/Vincepbell)
* [Rubem Nakamura](http://github.com/rubemz)

Note on Patches/Pull Requests
-----------------------------
Expand All @@ -168,4 +168,4 @@ Note on Patches/Pull Requests
Copyright
---------

Copyright (c) 2010-2012 Site5.com. See LICENSE for details.
Copyright (c) 2010-2013 Site5.com. See LICENSE for details.
22 changes: 0 additions & 22 deletions lib/solusvm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,6 @@
require 'xmlsimple'
require 'faraday'

module Solusvm
class << self
attr_accessor :api_endpoint, :api_id, :api_key, :api_options

# Specifies the login and url for making requests
#
# example:
#
# Solusvm.config('id', 'key', :url => 'http://www.example.com/api', :logger => RAILS_LOGGER, :logger_method => :log_info)
#
# Options:
# * <tt>:logger</tt> - Log object used for logging API responses
# * <tt>:logger_method</tt> - The method that performs the logging on the Log object
def config(api_id, api_key, options={})
@api_id = api_id
@api_key = api_key
@api_endpoint = URI.parse(options.delete(:url))
@api_options = options
end
end
end

require 'solusvm/hash'
require 'solusvm/base'
require 'solusvm/general'
Expand Down
39 changes: 34 additions & 5 deletions lib/solusvm/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class Base

attr_reader :returned_parameters

def initialize(config = {})
@config = config
end

# Prepares and sends the API request to the URL specificed in Solusvm.config
#
# class MyClass < Base
Expand Down Expand Up @@ -77,17 +81,42 @@ def successful?
returned_parameters["status"].nil? || returned_parameters["status"] == "success"
end

# URI parsed API URL
# Returns the API endpoint set in the instance configuration. Otherwise,
# it returns the default configuration.
#
# Returns a String
def api_endpoint
Solusvm.api_endpoint.dup
@config.fetch(:url)
end

# Returns the API id set in the instance configuration. Otherwise,
# it returns the default configuration.
#
# Returns a String
def api_id
@config.fetch(:api_id)
end

# Returns the API key set in the instance configuration. Otherwise,
# it returns the default configuration.
#
# Returns a String
def api_key
@config.fetch(:api_key)
end

def api_options(option)
if options = @config[:options]
options[option.to_sym]
end
end

def api_login
{id: Solusvm.api_id, key: Solusvm.api_key}
{id: api_id, key: api_key}
end

def log_messages(options)
logger, logger_method = Solusvm.api_options[:logger], Solusvm.api_options[:logger_method]
logger, logger_method = api_options(:logger), api_options(:logger_method)

if logger && logger.respond_to?(logger_method)
logger.send(logger_method, "[Start] => #{options[:action]}")
Expand All @@ -107,7 +136,7 @@ def statusmsg
def validate_server_type(type, &block)
type = type.strip

if valid = VALID_SERVER_TYPES.include?(type)
if VALID_SERVER_TYPES.include?(type)
yield
else
@returned_parameters = {
Expand Down
12 changes: 6 additions & 6 deletions lib/solusvm/cli/base_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ def output(result="", color=nil, force_new_line=(result.to_s !~ /( |\t)$/))

protected

def configure
Solusvm.config(
present_or_exit(:api_login, :id, "api_login required"),
present_or_exit(:api_key, :key, "api_key required"),
def api_params()
{
api_id: present_or_exit(:api_login, :id, "api_login required"),
api_key: present_or_exit(:api_key, :key, "api_key required"),
url: present_or_exit(:api_url, :url, "api_url required")
)
}
end

def present_or_exit(options_key, default_option_key, message)
options[options_key] || BaseCli.default_option(default_option_key) || (say(message) && raise(SystemExit))
end
end
end
end
5 changes: 2 additions & 3 deletions lib/solusvm/cli/client_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ def list

def api
@client ||= begin
configure
Solusvm::Client.new
Solusvm::Client.new(api_params())
end
end
end
end
end
5 changes: 2 additions & 3 deletions lib/solusvm/cli/general_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ def isos(type)

def api
@general ||= begin
configure
Solusvm::General.new
Solusvm::General.new(api_params)
end
end
end
end
end
5 changes: 2 additions & 3 deletions lib/solusvm/cli/node_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ def virtualservers(vserverid)

def api
@node ||= begin
configure
Solusvm::Node.new
Solusvm::Node.new(api_params)
end
end
end
end
end
5 changes: 2 additions & 3 deletions lib/solusvm/cli/reseller_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ def list

def api
@reseller ||= begin
configure
Solusvm::Reseller.new
Solusvm::Reseller.new(api_params)
end
end
end
end
end
5 changes: 2 additions & 3 deletions lib/solusvm/cli/server_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ def create(hostname, password)

def api
@server ||= begin
configure
Solusvm::Server.new
Solusvm::Server.new(api_params)
end
end

Expand All @@ -165,4 +164,4 @@ def switch(vserverid, switch_value, on_method, off_method)
end
end
end
end
end
20 changes: 7 additions & 13 deletions test/cli/test_client_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def setup
end

def test_should_delegate_client_create_to_client
Solusvm.expects(:config).with("thelogin", "thekey", { url: "theurl" })
api = mock do
expects(:successful?).returns(true)
expects(:create).with() do |options|
Expand All @@ -26,7 +25,7 @@ def test_should_delegate_client_create_to_client
expected.all? { |k,v| options[k] == v }
end.returns("theresult")
end
Solusvm::Client.stubs(new: api)
Solusvm::Client.expects(:new).with(solusvm_params).returns(api)

$stdout.expects(:puts).with("theresult")
Solusvm::Cli.start(cli_expand_base_arguments([
Expand All @@ -41,8 +40,7 @@ def test_should_delegate_client_create_to_client
end

def test_should_delegate_client_change_password_to_client
Solusvm.expects(:config).with("thelogin", "thekey", { url: "theurl" })
Solusvm::Client.stubs(new: mock do
Solusvm::Client.expects(:new).with(solusvm_params).returns(mock do
expects(:successful?).returns(true)
expects(:change_password).with("theusername", "thepassword").returns("theresult")
end)
Expand All @@ -52,8 +50,7 @@ def test_should_delegate_client_change_password_to_client
end

def test_should_delegate_client_authenticate_to_client
Solusvm.expects(:config).with("thelogin", "thekey", { url: "theurl" })
Solusvm::Client.stubs(new: mock do
Solusvm::Client.expects(:new).with(solusvm_params).returns(mock do
expects(:successful?).returns(true)
expects(:authenticate).with("theusername", "thepassword").returns("theresult")
end)
Expand All @@ -63,8 +60,7 @@ def test_should_delegate_client_authenticate_to_client
end

def test_should_delegate_client_check_exists_to_client
Solusvm.expects(:config).with("thelogin", "thekey", { url: "theurl" })
Solusvm::Client.stubs(new: mock do
Solusvm::Client.expects(:new).with(solusvm_params).returns(mock do
expects(:successful?).returns(true)
expects(:exists?).with("theusername").returns("theresult")
end)
Expand All @@ -74,8 +70,7 @@ def test_should_delegate_client_check_exists_to_client
end

def test_should_delegate_client_delete_to_client
Solusvm.expects(:config).with("thelogin", "thekey", { url: "theurl" })
Solusvm::Client.stubs(new: mock do
Solusvm::Client.stubs(:new).with(solusvm_params).returns(mock do
expects(:successful?).returns(true)
expects(:delete).with("theusername").returns("theresult")
end)
Expand All @@ -85,13 +80,12 @@ def test_should_delegate_client_delete_to_client
end

def test_should_delegate_client_list_to_client
Solusvm.expects(:config).with("thelogin", "thekey", { url: "theurl" })
Solusvm::Client.stubs(new: mock do
Solusvm::Client.expects(:new).with(solusvm_params).returns(mock do
expects(:successful?).returns(true)
expects(:list).returns("theresult")
end)

$stdout.expects(:puts).with("theresult")
Solusvm::Cli.start(cli_expand_base_arguments(["client", "list"]))
end
end
end
11 changes: 4 additions & 7 deletions test/cli/test_general_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ def setup
end

def test_should_delegate_templates_to_general
Solusvm.expects(:config).with("thelogin", "thekey", { url: "theurl" })
Solusvm::General.stubs(new: mock do
Solusvm::General.expects(:new).with(solusvm_params).returns(mock do
expects(:successful?).returns(true)
expects(:templates).with("type").returns("thetemplates")
end)
Expand All @@ -21,8 +20,7 @@ def test_should_delegate_templates_to_general
end

def test_should_delegate_plans_to_general
Solusvm.expects(:config).with("thelogin", "thekey", { url: "theurl" })
Solusvm::General.stubs(new: mock do
Solusvm::General.expects(:new).with(solusvm_params).returns(mock do
expects(:successful?).returns(true)
expects(:plans).with("type").returns("theplans")
end)
Expand All @@ -32,13 +30,12 @@ def test_should_delegate_plans_to_general
end

def test_should_delegate_isos_to_general
Solusvm.expects(:config).with("thelogin", "thekey", { url: "theurl" })
Solusvm::General.stubs(new: mock do
Solusvm::General.expects(:new).with(solusvm_params).returns(mock do
expects(:successful?).returns(true)
expects(:isos).with("type").returns("theisos")
end)

$stdout.expects(:puts).with("theisos")
Solusvm::Cli.start(cli_expand_base_arguments(["general", "isos", "type"]))
end
end
end
20 changes: 7 additions & 13 deletions test/cli/test_node_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ def setup
end

def test_should_delegate_node_available_ips_to_node
Solusvm.expects(:config).with("thelogin", "thekey", { url: "theurl" })
Solusvm::Node.stubs(new: mock do
Solusvm::Node.expects(:new).with(solusvm_params).returns(mock do
expects(:successful?).returns(true)
expects(:available_ips).with("thevserverid").returns("theips")
end)
Expand All @@ -21,8 +20,7 @@ def test_should_delegate_node_available_ips_to_node
end

def test_should_delegate_node_stats_to_node
Solusvm.expects(:config).with("thelogin", "thekey", { url: "theurl" })
Solusvm::Node.stubs(new: mock do
Solusvm::Node.expects(:new).with(solusvm_params).returns(mock do
expects(:successful?).returns(true)
expects(:statistics).with("thevserverid").returns("thestats")
end)
Expand All @@ -32,8 +30,7 @@ def test_should_delegate_node_stats_to_node
end

def test_should_delegate_node_xenresources_to_node
Solusvm.expects(:config).with("thelogin", "thekey", { url: "theurl" })
Solusvm::Node.stubs(new: mock do
Solusvm::Node.expects(:new).with(solusvm_params).returns(mock do
expects(:successful?).returns(true)
expects(:xenresources).with("thevserverid").returns("theresources")
end)
Expand All @@ -43,8 +40,7 @@ def test_should_delegate_node_xenresources_to_node
end

def test_should_delegate_node_virtualservers_to_node
Solusvm.expects(:config).with("thelogin", "thekey", { url: "theurl" })
Solusvm::Node.stubs(new: mock do
Solusvm::Node.expects(:new).with(solusvm_params).returns(mock do
expects(:successful?).returns(true)
expects(:virtualservers).with("thevserverid").returns("thedata")
end)
Expand All @@ -54,8 +50,7 @@ def test_should_delegate_node_virtualservers_to_node
end

def test_should_delegate_nodes_list_to_node
Solusvm.expects(:config).with("thelogin", "thekey", { url: "theurl" })
Solusvm::Node.stubs(new: mock do
Solusvm::Node.expects(:new).with(solusvm_params).returns(mock do
expects(:successful?).returns(true)
expects(:list).with("type").returns("thenodes")
end)
Expand All @@ -65,8 +60,7 @@ def test_should_delegate_nodes_list_to_node
end

def test_should_delegate_nodes_ids_to_node
Solusvm.expects(:config).with("thelogin", "thekey", { url: "theurl" })
Solusvm::Node.stubs(new: mock do
Solusvm::Node.expects(:new).with(solusvm_params).returns(mock do
expects(:successful?).returns(true)
expects(:ids).with("type").returns("thenodes")
end)
Expand All @@ -75,4 +69,4 @@ def test_should_delegate_nodes_ids_to_node
Solusvm::Cli.start(cli_expand_base_arguments(["node", "list-ids", "type"]))
end

end
end
Loading

0 comments on commit ca18f9b

Please sign in to comment.