class Solusvm::Base

Solusvm::Base is the main class for mapping API resources as subclasses.

Constants

VALID_SERVER_TYPES

Attributes

returned_parameters[R]

Public Class Methods

new(config = {}) click to toggle source
# File lib/solusvm/base.rb, line 8
def initialize(config = {})
  @config = config
end

Public Instance Methods

api_endpoint() click to toggle source

Returns the API endpoint set in the instance configuration. Otherwise, it returns the default configuration.

Returns a String

# File lib/solusvm/base.rb, line 90
def api_endpoint
  @config.fetch(:url)
end
api_id() click to toggle source

Returns the API id set in the instance configuration. Otherwise, it returns the default configuration.

Returns a String

# File lib/solusvm/base.rb, line 98
def api_id
  @config.fetch(:api_id)
end
api_key() click to toggle source

Returns the API key set in the instance configuration. Otherwise, it returns the default configuration.

Returns a String

# File lib/solusvm/base.rb, line 106
def api_key
  @config.fetch(:api_key)
end
api_login() click to toggle source
# File lib/solusvm/base.rb, line 116
def api_login
  {id: api_id, key: api_key}
end
api_options(option) click to toggle source
# File lib/solusvm/base.rb, line 110
def api_options(option)
  if options = @config[:options]
    options[option.to_sym]
  end
end
log_messages(options) click to toggle source
# File lib/solusvm/base.rb, line 120
def log_messages(options)
  logger, logger_method = api_options(:logger), api_options(:logger_method)

  if logger && logger.respond_to?(logger_method)
    logger.send(logger_method, "[Start] => #{options[:action]}")
    returned_parameters.each do |k,v|
      logger.send(logger_method, "   #{k} => #{v}")
    end
    logger.send(logger_method, "[End] => #{options[:action]}")
  end
end
parse_error(status, body) click to toggle source

Parses error responses.

# File lib/solusvm/base.rb, line 61
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
parse_response(status, body, force_array = false) click to toggle source

Converts the XML response to a Hash

force_array - Parses the xml element as an array; can be a string with the element name

or an array with element names
# File lib/solusvm/base.rb, line 45
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
parse_returned_params_as_list(attribute) click to toggle source

Parses a returned_parameters value as a list, if present.

# File lib/solusvm/base.rb, line 54
def parse_returned_params_as_list(attribute)
  if returned_parameters[attribute] && !returned_parameters[attribute].empty?
    returned_parameters[attribute].to_s.split(",")
  end
end
perform_request(options = {}, force_array = false) click to toggle source

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)
  end
end

Options:

  • :action - Specifies which API method to execute

All other options passed in are converted to http query arguments and are passed along to the API

force_array - see parse_response

# File lib/solusvm/base.rb, line 25
def perform_request(options = {}, force_array = false)
  ca_path  = File.join(File.dirname(__FILE__), "..", "cacert.pem")
  ssl      = {verify: true, ca_file: File.expand_path(ca_path)}

  options.reject! {|_,v| v.nil? }

  response = Faraday.new(url: api_endpoint, ssl: ssl) do |c|
    c.params = options.merge(api_login)
    c.adapter :net_http
  end.get

  @returned_parameters = parse_response(response.status, response.body, force_array)
  log_messages(options)
  successful?
end
statusmsg() click to toggle source

API response message

# File lib/solusvm/base.rb, line 133
def statusmsg
  returned_parameters["statusmsg"]
end
successful?() click to toggle source

Returns true when a request has been successful

my_class = MyClass.new
my_class.create_server("example.com")
my_class.successful? # => true
# File lib/solusvm/base.rb, line 82
def successful?
  returned_parameters["status"].nil? || returned_parameters["status"] == "success"
end
validate_server_type(type) { || ... } click to toggle source

Validates the server type.

# File lib/solusvm/base.rb, line 138
def validate_server_type(type, &block)
  type = type.strip

  if VALID_SERVER_TYPES.include?(type)
    yield
  else
    @returned_parameters = {
      "status"    => "error",
      "statusmsg" => "Invalid Virtual Server type: #{type}"
    }

    false
  end
end