solusvm 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +53 -0
- data/VERSION.yml +5 -0
- data/lib/solusvm.rb +36 -0
- data/lib/solusvm/base.rb +82 -0
- data/lib/solusvm/client.rb +23 -0
- data/lib/solusvm/exceptions.rb +4 -0
- data/lib/solusvm/general.rb +20 -0
- data/lib/solusvm/hash.rb +23 -0
- data/lib/solusvm/server.rb +60 -0
- data/solusvm.gemspec +86 -0
- data/test/fixtures/client_authenticate_error.txt +2 -0
- data/test/fixtures/client_authenticate_success.txt +2 -0
- data/test/fixtures/client_create_error.txt +2 -0
- data/test/fixtures/client_create_success.txt +7 -0
- data/test/fixtures/error.txt +2 -0
- data/test/fixtures/general_node_statistics_success.txt +14 -0
- data/test/fixtures/general_nodes_success.txt +3 -0
- data/test/fixtures/general_templates_success.txt +3 -0
- data/test/fixtures/server_boot_success.txt +2 -0
- data/test/fixtures/server_create_success.txt +10 -0
- data/test/fixtures/server_exists_success.txt +2 -0
- data/test/fixtures/server_reboot_success.txt +2 -0
- data/test/fixtures/server_resume_success.txt +2 -0
- data/test/fixtures/server_shutdown_success.txt +2 -0
- data/test/fixtures/server_suspend_success.txt +2 -0
- data/test/fixtures/server_terminate_success.txt +2 -0
- data/test/helper.rb +27 -0
- data/test/test_base.rb +71 -0
- data/test/test_client.rb +41 -0
- data/test/test_general.rb +63 -0
- data/test/test_hash.rb +27 -0
- data/test/test_server.rb +91 -0
- data/test/test_solusvm.rb +12 -0
- metadata +123 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Site5 LLC
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= solusvm
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Justin Mazzi. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "solusvm"
|
8
|
+
gem.summary = "Wrapper for the SolusVM Admin::API"
|
9
|
+
gem.description = "Solusvm allows for easy interaction with the SolusVM Admin::API."
|
10
|
+
gem.email = "jmazzi@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/site5/solusvm"
|
12
|
+
gem.authors = ["Justin Mazzi"]
|
13
|
+
gem.add_dependency 'xml-simple'
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "solusvm #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION.yml
ADDED
data/lib/solusvm.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/https'
|
3
|
+
require 'cgi'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'xmlsimple'
|
6
|
+
|
7
|
+
module Solusvm
|
8
|
+
class << self
|
9
|
+
attr_accessor :api_endpoint, :api_id, :api_key, :api_options
|
10
|
+
|
11
|
+
# Sepcifies the login and url for making requests
|
12
|
+
#
|
13
|
+
# example:
|
14
|
+
#
|
15
|
+
# Solusvm.config('id', 'key', :url => 'http://www.example.com/api', :logger => RAILS_LOGGER, :logger_method => :log_info)
|
16
|
+
#
|
17
|
+
# Options:
|
18
|
+
# * <tt>:logger</tt> - Log object used for logging API responses
|
19
|
+
# * <tt>:logger_method</tt> - The method that performs the logging on the Log object
|
20
|
+
def config(api_id, api_key, options={})
|
21
|
+
@api_id = api_id
|
22
|
+
@api_key = api_key
|
23
|
+
@api_endpoint = URI.parse(options.delete(:url))
|
24
|
+
@api_options = options
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
directory = File.expand_path(File.dirname(__FILE__))
|
30
|
+
|
31
|
+
require File.join(directory, 'solusvm', 'exceptions')
|
32
|
+
require File.join(directory, 'solusvm', 'hash')
|
33
|
+
require File.join(directory, 'solusvm', 'base')
|
34
|
+
require File.join(directory, 'solusvm', 'general')
|
35
|
+
require File.join(directory, 'solusvm', 'client')
|
36
|
+
require File.join(directory, 'solusvm', 'server')
|
data/lib/solusvm/base.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
module Solusvm
|
2
|
+
# Solusvm::Base is the main class for mapping API resources as subclasses.
|
3
|
+
class Base
|
4
|
+
attr_reader :returned_parameters, :statusmsg
|
5
|
+
VALID_SERVER_TYPES = ['openvz', 'xen', 'xen hvm']
|
6
|
+
|
7
|
+
# Prepares and sends the API request to the URL specificed in Solusvm.config
|
8
|
+
#
|
9
|
+
#
|
10
|
+
# class MyClass < Base
|
11
|
+
# def create_server(name)
|
12
|
+
# perform_request(:action => 'name', :id => 1)
|
13
|
+
# end
|
14
|
+
# end
|
15
|
+
# Options:
|
16
|
+
# * <tt>:action</tt> - Specifies which API method to execute
|
17
|
+
# All other options passed in are converted to http query arguments and are passed along to the API
|
18
|
+
def perform_request(options = {})
|
19
|
+
options.merge!(api_login)
|
20
|
+
http = Net::HTTP.new(api_endpoint.host, api_endpoint.port)
|
21
|
+
if api_endpoint.port == 443
|
22
|
+
http.use_ssl = true
|
23
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
24
|
+
end
|
25
|
+
http.start do |http|
|
26
|
+
request = Net::HTTP::Get.new("#{api_endpoint.path}?#{options.to_query}")
|
27
|
+
response = http.request(request)
|
28
|
+
@returned_parameters = parse_response(response.body)
|
29
|
+
log_messages(options)
|
30
|
+
end
|
31
|
+
successful?
|
32
|
+
end
|
33
|
+
|
34
|
+
# Converts the XML response to a Hash
|
35
|
+
def parse_response(body)
|
36
|
+
body = "<solusrequest>#{body}</solusrequest>"
|
37
|
+
XmlSimple.xml_in(body, 'ForceArray' => false)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns true when a request has been successful
|
41
|
+
#
|
42
|
+
# my_class = MyClass.new
|
43
|
+
# my_class.create_server('example.com')
|
44
|
+
# my_class.successful? # => true
|
45
|
+
def successful?
|
46
|
+
returned_parameters['status'] == 'success'
|
47
|
+
end
|
48
|
+
|
49
|
+
# URI parsed API URL
|
50
|
+
def api_endpoint
|
51
|
+
Solusvm.api_endpoint
|
52
|
+
end
|
53
|
+
|
54
|
+
def api_login
|
55
|
+
{:id => Solusvm.api_id, :key => Solusvm.api_key}
|
56
|
+
end
|
57
|
+
|
58
|
+
# TODO: clean this up
|
59
|
+
def log_messages(options)
|
60
|
+
if Solusvm.api_options[:logger] && Solusvm.api_options[:logger].respond_to?(Solusvm.api_options[:logger_method])
|
61
|
+
Solusvm.api_options[:logger].send(Solusvm.api_options[:logger_method], "[Start] => #{options[:action]}")
|
62
|
+
returned_parameters.each do |k,v|
|
63
|
+
Solusvm.api_options[:logger].send(Solusvm.api_options[:logger_method], " #{k} => #{v}")
|
64
|
+
end
|
65
|
+
Solusvm.api_options[:logger].send(Solusvm.api_options[:logger_method], "[End] => #{options[:action]}")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# API response message
|
70
|
+
def statusmsg
|
71
|
+
returned_parameters['statusmsg']
|
72
|
+
end
|
73
|
+
|
74
|
+
# Raises an exception unless a valid type is specified
|
75
|
+
def validate_server_type!(type)
|
76
|
+
type.strip!
|
77
|
+
unless VALID_SERVER_TYPES.include?(type)
|
78
|
+
raise SolusvmError, "Invalid Virtual Server type: #{type}"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Solusvm
|
2
|
+
# Solusvm::Client is the class for working with clients
|
3
|
+
class Client < Base
|
4
|
+
# Creates a client
|
5
|
+
#
|
6
|
+
# Options:
|
7
|
+
# * <tt>:username</tt>
|
8
|
+
# * <tt>:password</tt>
|
9
|
+
# * <tt>:email</tt>
|
10
|
+
# * <tt>:firstname</tt>
|
11
|
+
# * <tt>:lastname</tt>
|
12
|
+
def create(options ={})
|
13
|
+
options.merge!(:action => 'client-create')
|
14
|
+
perform_request(options)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Verify a clients login. Returns true when the specified login is correct
|
18
|
+
def authenticate(username, password)
|
19
|
+
perform_request({:action => 'client-authenticate', :username => username, :password => password})
|
20
|
+
statusmsg.match /validated/i
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Solusvm
|
2
|
+
class General < Base
|
3
|
+
def nodes(type)
|
4
|
+
validate_server_type!(type)
|
5
|
+
perform_request(:action => 'listnodes', :type => type)
|
6
|
+
returned_parameters['nodes'].split(',')
|
7
|
+
end
|
8
|
+
|
9
|
+
def templates(type)
|
10
|
+
validate_server_type!(type)
|
11
|
+
perform_request(:action => 'listtemplates', :type => type)
|
12
|
+
returned_parameters['templates'].split(',')
|
13
|
+
end
|
14
|
+
|
15
|
+
def node_statistics(nodeid)
|
16
|
+
perform_request(:action => 'node-statistics', :nodeid => nodeid)
|
17
|
+
returned_parameters
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/solusvm/hash.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
class Hash
|
2
|
+
unless method_defined?(:to_query)
|
3
|
+
def to_query
|
4
|
+
elements = []
|
5
|
+
self.each do |key, value|
|
6
|
+
elements << "#{CGI::escape(key.to_s)}=#{CGI::escape(value.to_s)}"
|
7
|
+
end
|
8
|
+
elements.join('&')
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
unless method_defined?(:reverse_merge)
|
13
|
+
def reverse_merge(other_hash)
|
14
|
+
other_hash.merge(self)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
unless method_defined?(:reverse_merge!)
|
19
|
+
def reverse_merge!(other_hash)
|
20
|
+
replace(reverse_merge(other_hash))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Solusvm
|
2
|
+
class Server < Base
|
3
|
+
def create(hostname, password, options = {})
|
4
|
+
options.reverse_merge!(
|
5
|
+
:type => 'xen',
|
6
|
+
:username => nil,
|
7
|
+
:ips => 1,
|
8
|
+
:node => nil,
|
9
|
+
:plan => nil,
|
10
|
+
:template => nil,
|
11
|
+
:password => password,
|
12
|
+
:hostname => hostname
|
13
|
+
).merge!(:action => 'vserver-create')
|
14
|
+
perform_request(options)
|
15
|
+
returned_parameters
|
16
|
+
end
|
17
|
+
|
18
|
+
def boot(vid)
|
19
|
+
perform_request(:action => 'vserver-boot', :vserverid => vid)
|
20
|
+
end
|
21
|
+
|
22
|
+
def reboot(vid)
|
23
|
+
perform_request(:action => 'vserver-reboot', :vserverid => vid)
|
24
|
+
end
|
25
|
+
|
26
|
+
def suspend(vid)
|
27
|
+
perform_request(:action => 'vserver-suspend', :vserverid => vid)
|
28
|
+
end
|
29
|
+
|
30
|
+
def resume(vid)
|
31
|
+
perform_request(:action => 'vserver-unsuspend', :vserverid => vid)
|
32
|
+
end
|
33
|
+
|
34
|
+
def shutdown(vid)
|
35
|
+
perform_request(:action => 'vserver-shutdown', :vserverid => vid)
|
36
|
+
end
|
37
|
+
|
38
|
+
def terminate(vid, deleteclient = false)
|
39
|
+
perform_request(:action => 'vserver-terminate', :vserverid => vid, :deleteclient => deleteclient)
|
40
|
+
end
|
41
|
+
|
42
|
+
def exists?(vid)
|
43
|
+
perform_request(:action => 'vserver-checkexists', :vserverid => vid)
|
44
|
+
!statusmsg.match(/Virtual server exists/i).nil?
|
45
|
+
end
|
46
|
+
|
47
|
+
def status(vid)
|
48
|
+
perform_request(:action => 'vserver-status', :vserverid => vid)
|
49
|
+
statusmsg
|
50
|
+
end
|
51
|
+
|
52
|
+
def add_ip(vid)
|
53
|
+
perform_request(:action => 'vserver-addip', :vserverid => vid)
|
54
|
+
end
|
55
|
+
|
56
|
+
def change_plan(vid, plan)
|
57
|
+
perform_request(:action => 'vserver-change', :vserverid => vid, :plan => plan)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/solusvm.gemspec
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{solusvm}
|
8
|
+
s.version = "0.3.3"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Justin Mazzi"]
|
12
|
+
s.date = %q{2010-06-21}
|
13
|
+
s.description = %q{Solusvm allows for easy interaction with the SolusVM Admin::API.}
|
14
|
+
s.email = %q{jmazzi@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION.yml",
|
26
|
+
"lib/solusvm.rb",
|
27
|
+
"lib/solusvm/base.rb",
|
28
|
+
"lib/solusvm/client.rb",
|
29
|
+
"lib/solusvm/exceptions.rb",
|
30
|
+
"lib/solusvm/general.rb",
|
31
|
+
"lib/solusvm/hash.rb",
|
32
|
+
"lib/solusvm/server.rb",
|
33
|
+
"solusvm.gemspec",
|
34
|
+
"test/fixtures/client_authenticate_error.txt",
|
35
|
+
"test/fixtures/client_authenticate_success.txt",
|
36
|
+
"test/fixtures/client_create_error.txt",
|
37
|
+
"test/fixtures/client_create_success.txt",
|
38
|
+
"test/fixtures/error.txt",
|
39
|
+
"test/fixtures/general_node_statistics_success.txt",
|
40
|
+
"test/fixtures/general_nodes_success.txt",
|
41
|
+
"test/fixtures/general_templates_success.txt",
|
42
|
+
"test/fixtures/server_boot_success.txt",
|
43
|
+
"test/fixtures/server_create_success.txt",
|
44
|
+
"test/fixtures/server_exists_success.txt",
|
45
|
+
"test/fixtures/server_reboot_success.txt",
|
46
|
+
"test/fixtures/server_resume_success.txt",
|
47
|
+
"test/fixtures/server_shutdown_success.txt",
|
48
|
+
"test/fixtures/server_suspend_success.txt",
|
49
|
+
"test/fixtures/server_terminate_success.txt",
|
50
|
+
"test/helper.rb",
|
51
|
+
"test/test_base.rb",
|
52
|
+
"test/test_client.rb",
|
53
|
+
"test/test_general.rb",
|
54
|
+
"test/test_hash.rb",
|
55
|
+
"test/test_server.rb",
|
56
|
+
"test/test_solusvm.rb"
|
57
|
+
]
|
58
|
+
s.homepage = %q{http://github.com/site5/solusvm}
|
59
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
60
|
+
s.require_paths = ["lib"]
|
61
|
+
s.rubygems_version = %q{1.3.7}
|
62
|
+
s.summary = %q{Wrapper for the SolusVM Admin::API}
|
63
|
+
s.test_files = [
|
64
|
+
"test/helper.rb",
|
65
|
+
"test/test_base.rb",
|
66
|
+
"test/test_client.rb",
|
67
|
+
"test/test_general.rb",
|
68
|
+
"test/test_hash.rb",
|
69
|
+
"test/test_server.rb",
|
70
|
+
"test/test_solusvm.rb"
|
71
|
+
]
|
72
|
+
|
73
|
+
if s.respond_to? :specification_version then
|
74
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
75
|
+
s.specification_version = 3
|
76
|
+
|
77
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
78
|
+
s.add_runtime_dependency(%q<xml-simple>, [">= 0"])
|
79
|
+
else
|
80
|
+
s.add_dependency(%q<xml-simple>, [">= 0"])
|
81
|
+
end
|
82
|
+
else
|
83
|
+
s.add_dependency(%q<xml-simple>, [">= 0"])
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<status>success</status>
|
2
|
+
<statusmsg></statusmsg>
|
3
|
+
<id>1</id>
|
4
|
+
<name>name</name>
|
5
|
+
<ip>127.0.0.1</ip>
|
6
|
+
<hostname>hostname.com</hostname>
|
7
|
+
<country>country</country>
|
8
|
+
<city>city</city>
|
9
|
+
<sshport>22</sshport>
|
10
|
+
<arch>x86_64</arch>
|
11
|
+
<freememory>10</freememory>
|
12
|
+
<freedisk>1000</freedisk>
|
13
|
+
<virtualservers>2</virtualservers>
|
14
|
+
<freeips>0</freeips>
|
@@ -0,0 +1,10 @@
|
|
1
|
+
<status>success</status>
|
2
|
+
<statusmsg>Virtual server created</statusmsg>
|
3
|
+
<mainipaddress>123.123.123.123</mainipaddress>
|
4
|
+
<extraipaddress>122.122.122.122,111.111.111.111</extraipaddress>
|
5
|
+
<rootpassword>123456</rootpassword>
|
6
|
+
<vserverid>100</vserverid>
|
7
|
+
<consoleuser>console-123</consoleuser>
|
8
|
+
<consolepassword>123456</consolepassword>
|
9
|
+
<hostname>server.hostname.com</hostname>
|
10
|
+
<virtid>vm101|101</virtid>
|
data/test/helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'redgreen'
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
|
+
require 'solusvm'
|
8
|
+
require 'fake_web'
|
9
|
+
|
10
|
+
class Test::Unit::TestCase
|
11
|
+
def load_response(name)
|
12
|
+
File.read(File.join(File.dirname(__FILE__), "fixtures/#{name}.txt"))
|
13
|
+
end
|
14
|
+
|
15
|
+
def base_uri
|
16
|
+
"#{Solusvm.api_endpoint}?id=api_id&key=api_key"
|
17
|
+
end
|
18
|
+
|
19
|
+
def api_login
|
20
|
+
{:id => 'api_id', :key => 'api_key'}
|
21
|
+
end
|
22
|
+
|
23
|
+
def setup_solusvm
|
24
|
+
Solusvm.config(api_login[:id], api_login[:key], :url => 'http://www.example.com/api')
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/test/test_base.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
class TestBase < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
FakeWeb.allow_net_connect = false
|
7
|
+
FakeWeb.clean_registry
|
8
|
+
setup_solusvm
|
9
|
+
@base = Solusvm::Base.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_valid_server_types
|
13
|
+
assert_equal 3, Solusvm::Base::VALID_SERVER_TYPES.size
|
14
|
+
assert_equal ['openvz', 'xen', 'xen hvm'].sort, Solusvm::Base::VALID_SERVER_TYPES.sort
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_parse_response
|
18
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=test&vserverid=1", :body => load_response('server_create_success'))
|
19
|
+
assert_nil @base.returned_parameters
|
20
|
+
@base.perform_request(:action => 'test', :vserverid => 1)
|
21
|
+
params = @base.returned_parameters
|
22
|
+
|
23
|
+
assert_equal 10, params.size
|
24
|
+
assert_equal '123.123.123.123', params['mainipaddress']
|
25
|
+
assert_equal 'console-123', params['consoleuser']
|
26
|
+
assert_equal '100', params['vserverid']
|
27
|
+
assert_equal 'Virtual server created', params['statusmsg']
|
28
|
+
assert_equal 'vm101|101', params['virtid']
|
29
|
+
assert_equal '123456', params['consolepassword']
|
30
|
+
assert_equal '122.122.122.122,111.111.111.111', params['extraipaddress']
|
31
|
+
assert_equal 'server.hostname.com', params['hostname']
|
32
|
+
assert_equal '123456', params['rootpassword']
|
33
|
+
assert_equal 'success', params['status']
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_successful
|
37
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=testsuccess&vserverid=1", :body => load_response('server_create_success'))
|
38
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=testfail&vserverid=1", :body => load_response('error'))
|
39
|
+
|
40
|
+
@base.perform_request(:action => 'testsuccess', :vserverid => 1)
|
41
|
+
assert @base.successful?
|
42
|
+
|
43
|
+
assert ! @base.perform_request(:action => 'testfail', :vserverid => 1)
|
44
|
+
assert_equal "error message", @base.statusmsg
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_api_login
|
48
|
+
assert_equal api_login, @base.api_login
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_statusmsg
|
52
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=testsuccess&vserverid=1", :body => load_response('server_create_success'))
|
53
|
+
@base.perform_request(:action => 'testsuccess', :vserverid => 1)
|
54
|
+
assert_equal 'Virtual server created', @base.statusmsg
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_validate_server_type
|
58
|
+
Solusvm::Base::VALID_SERVER_TYPES.each do |type|
|
59
|
+
assert_nothing_raised do
|
60
|
+
@base.validate_server_type!(type)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
begin
|
65
|
+
@base.validate_server_type!('bob')
|
66
|
+
flunk "Shouldn't get here"
|
67
|
+
rescue Solusvm::SolusvmError => e
|
68
|
+
assert_equal 'Invalid Virtual Server type: bob', e.message
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/test/test_client.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
class TestClient < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
setup_solusvm
|
7
|
+
FakeWeb.allow_net_connect = false
|
8
|
+
FakeWeb.clean_registry
|
9
|
+
@client = Solusvm::Client.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_create
|
13
|
+
options = {:username => 'vps123', :password=> '123456', :email=> 'email@address.com', :firstname => 'phill', :lastname => 'smith'}
|
14
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=client-create&#{options.to_query}", :body => load_response('client_create_success'))
|
15
|
+
assert @client.create(options)
|
16
|
+
|
17
|
+
params = @client.returned_parameters
|
18
|
+
assert_equal options[:username], params['username']
|
19
|
+
assert_equal options[:firstname], params['firstname']
|
20
|
+
assert_equal options[:lastname], params['lastname']
|
21
|
+
assert_equal options[:password], params['password']
|
22
|
+
assert_equal options[:email], params['email']
|
23
|
+
assert_equal 'Successfully added client', params['statusmsg']
|
24
|
+
assert_equal 'success', params['status']
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_create_fail
|
28
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=client-create", :body => load_response('client_create_error'))
|
29
|
+
assert ! @client.create
|
30
|
+
assert_equal 'Empty username field', @client.statusmsg
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_authenticate
|
34
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=client-authenticate&username=u&password=p", :body => load_response('client_authenticate_success'))
|
35
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=client-authenticate&username=u&password=notp", :body => load_response('client_authenticate_error'))
|
36
|
+
assert @client.authenticate('u', 'p')
|
37
|
+
assert ! @client.authenticate('u', 'notp')
|
38
|
+
assert_equal 'invalid username or password', @client.statusmsg
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
class TestGeneral < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
FakeWeb.allow_net_connect = false
|
6
|
+
FakeWeb.clean_registry
|
7
|
+
setup_solusvm
|
8
|
+
@general = Solusvm::General.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_nodes
|
12
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=listnodes&type=xen", :body => load_response('general_nodes_success'))
|
13
|
+
actual_nodes = @general.nodes('xen')
|
14
|
+
expected_nodes = %w(node1 node2 node3 node4)
|
15
|
+
assert_equal expected_nodes, actual_nodes
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_nodes_error
|
19
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=listnodes&type=whatever", :body => load_response('error'))
|
20
|
+
begin
|
21
|
+
@general.nodes('whatever')
|
22
|
+
flunk "Shouldn't get here"
|
23
|
+
rescue Solusvm::SolusvmError => e
|
24
|
+
assert e.message.match /Invalid Virtual Server type/
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_templates
|
29
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=listtemplates&type=xen", :body => load_response('general_templates_success'))
|
30
|
+
actual_templates = @general.templates('xen')
|
31
|
+
expected_templates = %w(template1 template2 template3)
|
32
|
+
assert_equal expected_templates, actual_templates
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_template_error
|
36
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=listtemplates&type=whatever", :body => load_response('error'))
|
37
|
+
begin
|
38
|
+
@general.templates('whatever')
|
39
|
+
flunk "Shouldn't get here"
|
40
|
+
rescue Solusvm::SolusvmError => e
|
41
|
+
assert e.message.match /Invalid Virtual Server type/
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_node_statistics
|
46
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=node-statistics&nodeid=1", :body => load_response('general_node_statistics_success'))
|
47
|
+
node_statistics = @general.node_statistics(1)
|
48
|
+
|
49
|
+
assert_equal '1000', node_statistics['freedisk']
|
50
|
+
assert_equal '22', node_statistics['sshport']
|
51
|
+
assert_equal 'city', node_statistics['city']
|
52
|
+
assert_equal 'name', node_statistics['name']
|
53
|
+
assert_equal '0', node_statistics['freeips']
|
54
|
+
assert_equal 'country', node_statistics['country']
|
55
|
+
assert_equal 'x86_64', node_statistics['arch']
|
56
|
+
assert_equal '1', node_statistics['id']
|
57
|
+
assert_equal '10', node_statistics['freememory']
|
58
|
+
assert_equal '2', node_statistics['virtualservers']
|
59
|
+
assert_equal '127.0.0.1', node_statistics['ip']
|
60
|
+
assert_equal 'hostname.com', node_statistics['hostname']
|
61
|
+
assert_equal 'success', node_statistics['status']
|
62
|
+
end
|
63
|
+
end
|
data/test/test_hash.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
class TestHash < Test::Unit::TestCase
|
4
|
+
def test_reverse_merge
|
5
|
+
defaults = { :a => "x", :b => "y", :c => 10 }.freeze
|
6
|
+
options = { :a => 1, :b => 2 }
|
7
|
+
expected = { :a => 1, :b => 2, :c => 10 }
|
8
|
+
|
9
|
+
# Should merge defaults into options, creating a new hash.
|
10
|
+
assert_equal expected, options.reverse_merge(defaults)
|
11
|
+
assert_not_equal expected, options
|
12
|
+
|
13
|
+
# Should merge! defaults into options, replacing options.
|
14
|
+
merged = options.dup
|
15
|
+
assert_equal expected, merged.reverse_merge!(defaults)
|
16
|
+
assert_equal expected, merged
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_to_query
|
20
|
+
defaults = { :a => "x", :b => "y", :c => 10 }
|
21
|
+
expected = "a=x&b=y&c=10".split('&').sort
|
22
|
+
actual = defaults.to_query.split('&').sort
|
23
|
+
|
24
|
+
assert defaults.to_query.is_a?(String)
|
25
|
+
assert_equal expected, actual
|
26
|
+
end
|
27
|
+
end
|
data/test/test_server.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
class TestServer < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
setup_solusvm
|
6
|
+
FakeWeb.allow_net_connect = false
|
7
|
+
FakeWeb.clean_registry
|
8
|
+
@server = Solusvm::Server.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_create
|
12
|
+
options = {:hostname => 'server.hostname.com', :type => 'xen', :username => 'bob', :password => '123456',
|
13
|
+
:node => 'node1', :plan => 'plan1', :template => 'mytpl', :ips => 1}
|
14
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=vserver-create&#{options.to_query}", :body => load_response('server_create_success'))
|
15
|
+
hostname = options.delete(:hostname)
|
16
|
+
password = options.delete(:password)
|
17
|
+
actual = @server.create(hostname, password, options)
|
18
|
+
assert_equal hostname, actual['hostname']
|
19
|
+
assert_equal options[:password], actual['rootpassword']
|
20
|
+
assert_equal '100', actual['vserverid']
|
21
|
+
assert_equal 'console-123', actual['consoleuser']
|
22
|
+
assert_equal '123456', actual['consolepassword']
|
23
|
+
assert_equal '123.123.123.123', actual['mainipaddress']
|
24
|
+
assert_equal 'vm101|101', actual['virtid']
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_create_error
|
28
|
+
FakeWeb.register_uri(:get, "#{base_uri}&type=xen&plan=&action=vserver-create&template=&hostname=&ips=1&username=&password=&node=", :body => load_response('error'))
|
29
|
+
@server.create('', '')
|
30
|
+
assert ! @server.successful?
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_boot
|
34
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=vserver-boot&vserverid=1", :body => load_response('server_boot_success'))
|
35
|
+
assert @server.boot(1)
|
36
|
+
assert_equal 'Virtual server booted', @server.statusmsg
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_reboot
|
40
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=vserver-reboot&vserverid=1", :body => load_response('server_reboot_success'))
|
41
|
+
assert @server.reboot(1)
|
42
|
+
assert_equal 'Virtual server rebooted', @server.statusmsg
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_suspend
|
46
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=vserver-suspend&vserverid=1", :body => load_response('server_suspend_success'))
|
47
|
+
assert @server.suspend(1)
|
48
|
+
assert_equal 'Virtual server suspended', @server.statusmsg
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_resume
|
52
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=vserver-unsuspend&vserverid=1", :body => load_response('server_resume_success'))
|
53
|
+
assert @server.resume(1)
|
54
|
+
assert_equal 'Virtual server unsuspended', @server.statusmsg
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_shutdown
|
58
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=vserver-shutdown&vserverid=1", :body => load_response('server_shutdown_success'))
|
59
|
+
assert @server.shutdown(1)
|
60
|
+
assert_equal 'Virtual server shutdown', @server.statusmsg
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_terminate
|
64
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=vserver-terminate&vserverid=1&deleteclient=false", :body => load_response('server_terminate_success'))
|
65
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=vserver-terminate&vserverid=1&deleteclient=true", :body => load_response('server_terminate_success'))
|
66
|
+
assert @server.terminate(1)
|
67
|
+
assert_equal 'Virtual server terminated', @server.statusmsg
|
68
|
+
|
69
|
+
assert @server.terminate(1, true)
|
70
|
+
assert_equal 'Virtual server terminated', @server.statusmsg
|
71
|
+
end
|
72
|
+
|
73
|
+
def text_exists
|
74
|
+
FakeWeb.register_uri(:get, "#{base_uri}&action=vserver-checkexists&vserverid=1", :body => load_response('server_exists_success'))
|
75
|
+
assert @server.exists(1)
|
76
|
+
assert_equal 'Virtual server exists', @server.statusmsg
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_status
|
80
|
+
flunk "Implement this"
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_add_ip
|
84
|
+
flunk "This is broken on the SolusVM API Level"
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_change_plan
|
88
|
+
flunk "Implement this"
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
class TestSolusvm < Test::Unit::TestCase
|
4
|
+
def test_config
|
5
|
+
setup_solusvm
|
6
|
+
url = 'http://www.example.com/api'
|
7
|
+
assert_equal 'api_id', Solusvm.api_id
|
8
|
+
assert_equal 'api_key', Solusvm.api_key
|
9
|
+
assert_not_nil Solusvm.api_endpoint
|
10
|
+
assert_equal URI.parse(url), Solusvm.api_endpoint
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: solusvm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 21
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 3
|
9
|
+
- 3
|
10
|
+
version: 0.3.3
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Justin Mazzi
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-21 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: xml-simple
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Solusvm allows for easy interaction with the SolusVM Admin::API.
|
36
|
+
email: jmazzi@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION.yml
|
51
|
+
- lib/solusvm.rb
|
52
|
+
- lib/solusvm/base.rb
|
53
|
+
- lib/solusvm/client.rb
|
54
|
+
- lib/solusvm/exceptions.rb
|
55
|
+
- lib/solusvm/general.rb
|
56
|
+
- lib/solusvm/hash.rb
|
57
|
+
- lib/solusvm/server.rb
|
58
|
+
- solusvm.gemspec
|
59
|
+
- test/fixtures/client_authenticate_error.txt
|
60
|
+
- test/fixtures/client_authenticate_success.txt
|
61
|
+
- test/fixtures/client_create_error.txt
|
62
|
+
- test/fixtures/client_create_success.txt
|
63
|
+
- test/fixtures/error.txt
|
64
|
+
- test/fixtures/general_node_statistics_success.txt
|
65
|
+
- test/fixtures/general_nodes_success.txt
|
66
|
+
- test/fixtures/general_templates_success.txt
|
67
|
+
- test/fixtures/server_boot_success.txt
|
68
|
+
- test/fixtures/server_create_success.txt
|
69
|
+
- test/fixtures/server_exists_success.txt
|
70
|
+
- test/fixtures/server_reboot_success.txt
|
71
|
+
- test/fixtures/server_resume_success.txt
|
72
|
+
- test/fixtures/server_shutdown_success.txt
|
73
|
+
- test/fixtures/server_suspend_success.txt
|
74
|
+
- test/fixtures/server_terminate_success.txt
|
75
|
+
- test/helper.rb
|
76
|
+
- test/test_base.rb
|
77
|
+
- test/test_client.rb
|
78
|
+
- test/test_general.rb
|
79
|
+
- test/test_hash.rb
|
80
|
+
- test/test_server.rb
|
81
|
+
- test/test_solusvm.rb
|
82
|
+
has_rdoc: true
|
83
|
+
homepage: http://github.com/site5/solusvm
|
84
|
+
licenses: []
|
85
|
+
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options:
|
88
|
+
- --charset=UTF-8
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
hash: 3
|
97
|
+
segments:
|
98
|
+
- 0
|
99
|
+
version: "0"
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
hash: 3
|
106
|
+
segments:
|
107
|
+
- 0
|
108
|
+
version: "0"
|
109
|
+
requirements: []
|
110
|
+
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 1.3.7
|
113
|
+
signing_key:
|
114
|
+
specification_version: 3
|
115
|
+
summary: Wrapper for the SolusVM Admin::API
|
116
|
+
test_files:
|
117
|
+
- test/helper.rb
|
118
|
+
- test/test_base.rb
|
119
|
+
- test/test_client.rb
|
120
|
+
- test/test_general.rb
|
121
|
+
- test/test_hash.rb
|
122
|
+
- test/test_server.rb
|
123
|
+
- test/test_solusvm.rb
|