Class: SolusVM::Base
- Inherits:
-
Object
- Object
- SolusVM::Base
- Defined in:
- lib/solusvm/base.rb
Overview
SolusVM::Base is the main class for mapping API resources as subclasses.
Constant Summary
- VALID_SERVER_TYPES =
["openvz", "xen", "xen hvm"]
Instance Attribute Summary (collapse)
-
- (Object) returned_parameters
readonly
Returns the value of attribute returned_parameters.
Instance Method Summary (collapse)
-
- (Object) api_endpoint
Returns the API endpoint set in the instance configuration.
-
- (Object) api_id
Returns the API id set in the instance configuration.
-
- (Object) api_key
Returns the API key set in the instance configuration.
-
- (Object) api_login
API login information.
-
- (Object) api_options(option)
API options.
-
- (Object) conn
Creates a Faraday connection.
-
- (Base) initialize(config = {})
constructor
A new instance of Base.
-
- (void) log_messages(options)
Logs API actions to the configured logger.
-
- (Object) parse_error(status, body)
Parses error responses.
-
- (Object) parse_response(status, body)
Converts the XML response to a Hash.
-
- (Object) parse_returned_params_as_list(attribute)
Parses a returned_parameters value as a list, if present.
-
- (Object) perform_request(options = {})
Prepares and sends the API request to the URL specified in `@config`.
-
- (Object) ssl_option
SSL options used when creating a Faraday connection.
-
- (Object) statusmsg
API response message.
-
- (Boolean) successful?
Check if the request was successful.
-
- (Object) validate_server_type(type) { ... }
Validates the server type.
Constructor Details
- (Base) initialize(config = {})
Returns a new instance of Base
8 9 10 |
# File 'lib/solusvm/base.rb', line 8 def initialize(config = {}) @config = config end |
Instance Attribute Details
- (Object) returned_parameters (readonly)
Returns the value of attribute returned_parameters
6 7 8 |
# File 'lib/solusvm/base.rb', line 6 def returned_parameters @returned_parameters end |
Instance Method Details
- (Object) api_endpoint
Returns the API endpoint set in the instance configuration. Otherwise, it returns the default configuration.
123 124 125 |
# File 'lib/solusvm/base.rb', line 123 def api_endpoint @config.fetch(:url) end |
- (Object) api_id
Returns the API id set in the instance configuration. Otherwise, it returns the default configuration.
131 132 133 |
# File 'lib/solusvm/base.rb', line 131 def api_id @config.fetch(:api_id) end |
- (Object) api_key
Returns the API key set in the instance configuration. Otherwise, it returns the default configuration.
139 140 141 |
# File 'lib/solusvm/base.rb', line 139 def api_key @config.fetch(:api_key) end |
- (Object) api_login
API login information.
157 158 159 |
# File 'lib/solusvm/base.rb', line 157 def api_login { id: api_id, key: api_key } end |
- (Object) api_options(option)
API options
148 149 150 151 152 |
# File 'lib/solusvm/base.rb', line 148 def (option) if = @config[:options] [option.to_sym] end end |
- (Object) conn
Creates a Faraday connection.
44 45 46 47 48 49 |
# File 'lib/solusvm/base.rb', line 44 def conn @conn ||= Faraday.new(ssl: ssl_option) do |c| c.request :retry if @config.fetch(:retry_request, false) c.adapter :net_http end end |
- (void) log_messages(options)
This method returns an undefined value.
Logs API actions to the configured logger.
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/solusvm/base.rb', line 167 def () logger, logger_method = (:logger), (:logger_method) if logger && logger.respond_to?(logger_method) logger.send(logger_method, "[Start] => #{[:action]}") returned_parameters.each do |k,v| logger.send(logger_method, " #{k} => #{v}") end logger.send(logger_method, "[End] => #{[:action]}") end end |
- (Object) parse_error(status, body)
Parses error responses.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/solusvm/base.rb', line 92 def parse_error(status, body) if (200..299).include?(status) # Checks for application errors case body.downcase when /invalid ipaddress/i raise AuthenticationError, "This IP is not authorized to use the API" when /Invalid id or key/i raise AuthenticationError, "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 |
- (Object) parse_response(status, body)
Converts the XML response to a Hash.
67 68 69 |
# File 'lib/solusvm/base.rb', line 67 def parse_response(status, body) parse_error(status, body) || JSON.parse(body) end |
- (Object) parse_returned_params_as_list(attribute)
Parses a returned_parameters value as a list, if present.
76 77 78 79 80 |
# File 'lib/solusvm/base.rb', line 76 def parse_returned_params_as_list(attribute) if returned_parameters[attribute] && !returned_parameters[attribute].empty? returned_parameters[attribute].to_s.split(",") end end |
- (Object) perform_request(options = {})
Prepares and sends the API request to the URL specified in `@config`.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/solusvm/base.rb', line 28 def perform_request( = {}) .reject! { |_, v| v.nil? } # Force JSON responses [:rdtype] = "json" response = conn.get api_endpoint, .merge(api_login) @returned_parameters = parse_response(response.status, response.body) () successful? end |
- (Object) ssl_option
SSL options used when creating a Faraday connection.
54 55 56 57 58 59 |
# File 'lib/solusvm/base.rb', line 54 def ssl_option { verify: true, ca_file: File.("../../cacert.pem", __FILE__) } end |
- (Object) statusmsg
API response message
182 183 184 |
# File 'lib/solusvm/base.rb', line 182 def statusmsg returned_parameters["statusmsg"] end |
- (Boolean) successful?
Check if the request was successful.
my_class = MyClass.new my_class.create_server("example.com") my_class.successful? # => true
115 116 117 |
# File 'lib/solusvm/base.rb', line 115 def successful? returned_parameters["status"].nil? || returned_parameters["status"] == "success" end |
- (Object) validate_server_type(type) { ... }
Validates the server type.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/solusvm/base.rb', line 193 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 |