lib/solusvm/base.rb in solusvm-1.1.0.beta2 vs lib/solusvm/base.rb in solusvm-1.2.0
- old
+ new
@@ -1,63 +1,82 @@
-Faraday.register_middleware :response, :solusvm_errors => Solusvm::SolusvmErrors
-
module Solusvm
# Solusvm::Base is the main class for mapping API resources as subclasses.
class Base
- attr_reader :returned_parameters, :statusmsg
- VALID_SERVER_TYPES = ['openvz', 'xen', 'xen hvm']
+ VALID_SERVER_TYPES = ["openvz", "xen", "xen hvm"]
+ attr_reader :returned_parameters
+
# Prepares and sends the API request to the URL specificed in Solusvm.config
#
# class MyClass < Base
# def create_server(name)
- # perform_request(:action => 'name', :id => 1)
+ # perform_request(:action => "name", :id => 1)
# end
# end
#
# Options:
# * <tt>:action</tt> - Specifies which API method to execute
# All other options passed in are converted to http query arguments and are passed along to the API
#
# <tt>force_array</tt> - see parse_response
def perform_request(options = {}, force_array = false)
- ca_path = File.join(File.dirname(__FILE__), '..', 'cacert.pem')
+ ca_path = File.join(File.dirname(__FILE__), "..", "cacert.pem")
ssl = {:verify => true, :ca_file => File.expand_path(ca_path)}
+
response = Faraday.new(:url => api_endpoint, :ssl => ssl) do |c|
c.params = options.merge(api_login)
- c.response :solusvm_errors
c.adapter :net_http
end.get
- @returned_parameters = parse_response(response.body, force_array)
+
+ @returned_parameters = parse_response(response.status, response.body, force_array)
log_messages(options)
successful?
end
# Converts the XML response to a Hash
#
# <tt>force_array</tt> - Parses the xml element as an array; can be a string with the element name
# or an array with element names
- def parse_response(body, force_array = false)
- force_array = Array(force_array) if force_array
- body = "<solusrequest>#{body}</solusrequest>"
- XmlSimple.xml_in(body, 'ForceArray' => force_array)
+ def parse_response(status, body, force_array = false)
+ parse_error(status, body) || begin
+ force_array = Array(force_array) if force_array
+ body = "<solusrequest>#{body}</solusrequest>"
+ XmlSimple.xml_in(body, "ForceArray" => force_array)
+ end
end
# Parses a returned_parameters value as a list, if present.
def parse_returned_params_as_list(attribute)
if returned_parameters[attribute] && !returned_parameters[attribute].empty?
- returned_parameters[attribute].to_s.split(',')
+ returned_parameters[attribute].to_s.split(",")
end
end
+ # Parses error responses.
+ def parse_error(status, body)
+ if (200..299).include?(status)
+ # Checks for application errors
+ case body.downcase
+ when /invalid ipaddress/i
+ { "status" => "error", "statusmsg" => "This IP is not authorized to use the API" }
+ when /Invalid id or key/i
+ { "status" => "error", "statusmsg" => "Invalid ID or key" }
+ when /Node not found/i
+ { "status" => "error", "statusmsg" => "Node does not exist" }
+ end
+ else
+ { "status" => "error", "statusmsg" => "Bad HTTP Status: #{status}" }
+ end
+ end
+
# Returns true when a request has been successful
#
# my_class = MyClass.new
- # my_class.create_server('example.com')
+ # my_class.create_server("example.com")
# my_class.successful? # => true
def successful?
- returned_parameters['status'] == 'success'
+ returned_parameters["status"] == "success"
end
# URI parsed API URL
def api_endpoint
Solusvm.api_endpoint.dup
@@ -79,17 +98,25 @@
end
end
# API response message
def statusmsg
- returned_parameters['statusmsg']
+ returned_parameters["statusmsg"]
end
- # Raises an exception unless a valid type is specified
- def validate_server_type!(type)
+ # Validates the server type.
+ def validate_server_type(type, &block)
type = type.strip
- unless VALID_SERVER_TYPES.include?(type)
- raise SolusvmError, "Invalid Virtual Server type: #{type}"
+
+ if valid = VALID_SERVER_TYPES.include?(type)
+ yield
+ else
+ @returned_parameters = {
+ "status" => "error",
+ "statusmsg" => "Invalid Virtual Server type: #{type}"
+ }
+
+ false
end
end
end
end