Class: Cloudstack::Api

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::AttributeMethods, ActiveModel::Validations
Defined in:
lib/cloudstack/api.rb

Overview

Instance of Cloudstack::Api class uses to prepare CloudStack API request and send it to Cloudstack Management Server. Then Cludstack::Api handles a response and return it in usable type.

Instance Method Summary collapse

Instance Method Details

#execute_command(command, options = {}) ⇒ Object

Method provides the same functionality as #execute_command! except it does not raises exceptions. When error occurs, it just returns false. So if method returns false, errors message should be in #errors

Example

@api = Cloudstack::Api.new
@api.execute_command('some command', some_param: 'some value')   # => false
@api.errors.messages # => {:command=>["'some command' is invalid command"]}


111
112
113
114
115
116
117
# File 'lib/cloudstack/api.rb', line 111

def execute_command(command, options={})
  begin
    return execute_command!(command, options)
  rescue
    return false
  end
end

#execute_command!(command, params = {}) ⇒ Object

Calls CloudStack API command on a remote server. Method arguments is command and a list of params

Method returns a response hash (if request was success) or raises an error else. Before raising an error it stores in an #errors object. It works like ActiveModel::Validations So error messages can be retrieved:

api_instance.errors.messages

Example

@api = Cloudstack::Api.new
@api.execute_command('some command', some_param: 'some value')   # => raises ArgumentError exception
@api.errors.messages # => {:command=>["'some command' is invalid command"]}

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cloudstack/api.rb', line 39

def execute_command!(command, params={})
  # Clear errors from previous request
  errors.clear

  # Set local variables
  @command = command.to_s
  @params = params.symbolize_keys

  # Validate command and parameters
  raise ArgumentError if invalid?

  # Assemble request_options
  request_options = {
      command: @command,
      apikey: Cloudstack.config.api_key,
      response: 'json'
  }
  request_options.merge! @params

  # Generate request signature and add it to request_options
  request_options[:signature] = sign_request(request_options)

  # Generate request url with parameters
  request_url = Cloudstack.config.api_url + '?' + request_options.to_query

  # Check current mode. If mode is +test+ than command not executes,
  # but only returns request url.
  return request_url if Cloudstack.config.api_mode == 'test'

  begin
    # send curl request to the CloudStack server
    curl = Curl::Easy.new(request_url)
    curl.ssl_verify_peer = false
    curl.perform

    # handle response
    response = JSON.parse(curl.body_str, symbolize_names: true)
  rescue Exception => e
    # If error occurs, it adds to instance errors
    errors.add(:base, e.message)
    raise e
  end

  # Checks if a command returns a CloudStack error. If error occurs,
  # it adds to errors and excepions reises.
  if response[:errorresponse].present?
    errors.add(:base, response[:errorresponse][:errortext])
    raise StandardError, response[:errorresponse][:errortext]
  end

  result = response["#{command}response".downcase.to_sym]

  if result[:errortext].present?
    errors.add(:base, result[:errortext])
    raise StandardError, result[:errortext]
  end

  # If all pass success, returns a result hash
  result
end