Class: CloudStackClient::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_stack_client/connector.rb

Overview

Simple class to interact with the CloudStack API

Examples:

The server URI is passed on instance creation

connector = CloudStackClient::Connector.new('http://example.com/client/api')
# or
connector = CloudStackClient::Connector.new(:api_uri => 'https://example.com/client/api', :api_key => 'MyKeY', :secret_key => 'mYsEcReT')

Login with Username, Passwort and Domain if API Key and Secret Key are not available

connector.('MyUser', 'MyPassword', 'MyDomain') # => true or false

Execute an arbitrary command

connector.listVirtualMachines # => Raw result record as REXML::Document
connector.listVirtualMachines(:response => :json, :state => :running) # => Hash with result record

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Connector

Create API Connector.

Configuration can passed as configuration Hash or as String containing the API URL.

Parameters:

  • config (String, Hash) (defaults to: {})

    URI of the Service API or Hash with configuration parameters

See Also:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cloud_stack_client/connector.rb', line 27

def initialize(config = {})
  if config.is_a? String
    if config.length > 0
      config = {:api_uri => config}
      if config[:api_uri].index("/api")
        config[:web_uri] = config[:api_uri].sub("/api", "")
      end
    else
      config = {}
    end
  end
  @config = CloudStackClient::API_DEFAULTS.dup
  config.each {|key, value| @config[key.to_sym] = value}
  @credentials = {}
  @credentials[:api_key] = @config[:api_key] if @config[:api_key]
  @credentials[:secret_key] = @config[:secret_key] if @config[:secret_key]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ REXML::Document, Hash (private)

Dynamic methods are passed as commands to CloudStack

Examples:

Use dynamic methods as commands

cs_connector = CloudStackClient::Connector.new "https://example.com/client/api"
cs_connector. "Me", "MyPass", "MyDomain"
response = cs_connector.listVirtualMachines(:response => :json, :state => :running)
if response.has_key? "listvirtualmachinesresponse"
  vms = response["listvirtualmachinesresponse"]["virtualmachine"]
  cs_connector.stopVirtualMachine :id => vms.first["id"]
end

Parameters:

  • method_name (String)

    Name of the command

  • parameters (Hash, Array)

    command parameters

  • disable_auto_paging (Boolean)

    (optional) If true the command will be executed with manual paging

Returns:

  • (REXML::Document, Hash)

    Result of the query



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/cloud_stack_client/connector.rb', line 134

def method_missing(*args)
  name = args.shift
  parameters = args.shift
  auto_paging = !args.shift
  if parameters.is_a?(Hash) && !parameters.has_key?(:command)
    parameters[:command] = name
  elsif parameters.is_a?(Array) && !parameters.find {|pos| pos.is_a?(Array) && pos[0] == :command}
    parameters << [:command => name]
  else
    parameters = {:command => name}
  end
  if auto_paging && name.to_s.downcase.start_with?("list")
    query_all_pages(parameters)
  else
    query(parameters)
  end
end

Instance Attribute Details

#configHash

Connection credentials and settings. Defaults to API_DEFAULTS.

Returns:

  • (Hash)


19
20
21
# File 'lib/cloud_stack_client/connector.rb', line 19

def config
  @config
end

Instance Method Details

#authenticated?Boolean

Check whether valid login credentials are present

Returns:

  • (Boolean)


91
92
93
94
95
96
97
# File 'lib/cloud_stack_client/connector.rb', line 91

def authenticated?
  if @credentials.any? && query({:command => "listCapabilities"})
    true
  else
    false
  end
end

#login(username, password, domain = "") ⇒ Boolean

Login with username and plain text password

Parameters:

  • username (String)
  • password (String)

    Plain text password

  • domain (String) (defaults to: "")

    Login Domain. The / prefix can be omitted

Returns:

  • (Boolean)

    true if successful



51
52
53
54
# File 'lib/cloud_stack_client/connector.rb', line 51

def (username, password, domain = "")
  password ||= ""
   username, CloudStackClient::Helpers::hash_password(password), domain
end

#login_hashed(username, password_hash, domain = "") ⇒ Boolean

Login with username and hashed password

Parameters:

  • username (String)
  • password_hash (String)

    Password hash

  • domain (String) (defaults to: "")

    Login Domain. The / prefix can be omitted

Returns:

  • (Boolean)

    true if successful

See Also:

  • Helpers::hash_password


63
64
65
66
67
68
69
70
71
# File 'lib/cloud_stack_client/connector.rb', line 63

def (username, password_hash, domain = "")
  result = CloudStackClient::API::get_credentials @config[:api_uri], username, password_hash, domain, @config[:http_method], @config[:ssl_check]
  if result
    @credentials = result
    true
  else
    false
  end
end

#login_with_keys(api_key, secret_key) ⇒ Boolean

Login with API Key and Secret Key

Parameters:

  • api_key (String)
  • secret_key (String)

Returns:

  • (Boolean)

    true if successful



78
79
80
81
82
83
84
85
86
# File 'lib/cloud_stack_client/connector.rb', line 78

def (api_key, secret_key)
  if CloudStackClient::API::verify_keys(@config[:api_uri], api_key, secret_key, @config[:http_method], @config[:ssl_check])
    @credentials[:api_key] = api_key
    @credentials[:secret_key] = secret_key
    true
  else
    false
  end
end

#query(parameters) ⇒ REXML::Document, ...

Send a command

Parameters:

  • parameters (Hash, Array)

    Hash or Array of [Key, Value] pairs containing all query parameters

Returns:

  • (REXML::Document, Hash, String)

    The query result depending on the HTTP response content type



103
104
105
# File 'lib/cloud_stack_client/connector.rb', line 103

def query(parameters)
  CloudStackClient::API::query(@config[:api_uri], parameters, @credentials, @config[:http_method], @config[:ssl_check])
end

#query_all_pages(parameters) ⇒ REXML::Document, ...

Send a command and merge all result pages

If the result spans multiple pages, multiple calls are executed to retrieve all pages. These will be returned as one.

Parameters:

  • parameters (Hash, Array)

    Hash or Array of [Key, Value] pairs containing all query parameters

Returns:

  • (REXML::Document, Hash, String)

    The query result depending on the HTTP response content type



113
114
115
# File 'lib/cloud_stack_client/connector.rb', line 113

def query_all_pages(parameters)
  CloudStackClient::API::query_with_auto_paging(@config[:api_uri], parameters, @credentials, @config[:page_size], @config[:http_method], @config[:ssl_check])
end