Class: CloudStackClient::Connector
- Inherits:
-
Object
- Object
- CloudStackClient::Connector
- Defined in:
- lib/cloud_stack_client/connector.rb
Overview
Simple class to interact with the CloudStack API
Instance Attribute Summary collapse
-
#config ⇒ Hash
Connection credentials and settings.
Instance Method Summary collapse
-
#authenticated? ⇒ Boolean
Check whether valid login credentials are present.
-
#initialize(config = {}) ⇒ Connector
constructor
Create API Connector.
-
#login(username, password, domain = "") ⇒ Boolean
Login with username and plain text password.
-
#login_hashed(username, password_hash, domain = "") ⇒ Boolean
Login with username and hashed password.
-
#login_with_keys(api_key, secret_key) ⇒ Boolean
Login with API Key and Secret Key.
-
#query(parameters) ⇒ REXML::Document, ...
Send a command.
-
#query_all_pages(parameters) ⇒ REXML::Document, ...
Send a command and merge all result pages.
Constructor Details
#initialize(config = {}) ⇒ Connector
Create API Connector.
Configuration can passed as configuration Hash or as String containing the API URL.
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
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
#config ⇒ Hash
Connection credentials and settings. Defaults to API_DEFAULTS.
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
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
51 52 53 54 |
# File 'lib/cloud_stack_client/connector.rb', line 51 def login(username, password, domain = "") password ||= "" login_hashed username, CloudStackClient::Helpers::hash_password(password), domain end |
#login_hashed(username, password_hash, domain = "") ⇒ Boolean
Login with username and hashed password
63 64 65 66 67 68 69 70 71 |
# File 'lib/cloud_stack_client/connector.rb', line 63 def login_hashed(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
78 79 80 81 82 83 84 85 86 |
# File 'lib/cloud_stack_client/connector.rb', line 78 def login_with_keys(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
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.
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 |