Class: CloudMonkey::CMClient

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

Overview

A CloudMonkey client. Requires the cloudmonkey command to be installed on the system. The client must be synced outside of this module, or by using the provided sync method, to cache the API commands available. Once synced, cloudmonkey commands can be called like Ruby methods using symbolized parameter names in a hash for the parameters and values.

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ CMClient

Returns a new instance of CMClient.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cm.rb', line 13

def initialize(params)
  @host = params[:host] || 'localhost'
  @port = params[:port] || 8080
  @apikey = params[:apikey]
  @secretkey = params[:secretkey]

  @conf_dir = params[:conf_dir] || File.join("#{Dir.home}", ".cloudmonkey-cmrb")
  Dir.mkdir(@conf_dir) if not Dir.exists?(@conf_dir)
  
  @config = params[:config] || File.join(@conf_dir, "config")
  @cache = File.join(@conf_dir, "cache")
  @log = File.join(@conf_dir, "log")
  @history = File.join(@conf_dir, "history")
  @debug = params[:debug] || false
  configure
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Catch missing methods to transform them into cloudmonkey commands. This allows magic translation of ruby into cloudmonkey commands. Because cloudmonkey api commands have a specific format of 1 or more words followed by key=value parameters, we turn the method_name into “method name” and pass in the first argument only, which is assumed to be a hash or SINGLE string value.



83
84
85
86
# File 'lib/cm.rb', line 83

def method_missing(name, *args)
  cmd = name.to_s.split('_').join(' ').strip
  execute cmd, args[0]
end

Instance Method Details

#configureObject

Configure the cloudmonkey client for use with the library.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cm.rb', line 36

def configure
  # Connection info
  set('host', "#{@host}")
  set('port', "#{@port}")
  set('apikey', "#{@apikey}")
  set('secretkey', "#{@secretkey}")

  # Local Storage Info
  set('cache_file', @cache)
  set('log_file', @log)
  set('history_file', @history)

  # Display options needed to make parsing of responses simple.
  set('display', 'json')
  set('asyncblock', 'false')
  set('color', 'false')
end

#execute(cmd, params = nil) ⇒ Object

Execute a command with cloudmonkey. The command can be multi-word, e.g. “list users”. Params, if provided, must be a hash (e.g. :account => “admin”), or a string value (.e.g “help” as the command and “list users” as the parameter).



71
72
73
74
# File 'lib/cm.rb', line 71

def execute(cmd, params = nil)
  cmd.downcase!
  run_cmd(cmd, params)
end

#set(key, value) ⇒ Object

Set a cloudmonkey configuration option.



31
32
33
# File 'lib/cm.rb', line 31

def set(key, value)
  run_cmd("set #{key} #{value}", nil)
end

#syncObject

Synchronize the API list for cloudmonkey and return true or false depending on success.



56
57
58
# File 'lib/cm.rb', line 56

def sync
  execute('sync').has_key?(:message)
end

#versionObject

Retrieve the cloudmonkey version as a string.



61
62
63
64
65
# File 'lib/cm.rb', line 61

def version
  ver = run_cmd('-v')[:message]
  # x.y.Z versioning scheme, e.g. 5.1.0
  /\d+\.\d+\.\d+/.match(ver).to_s
end