Class: CloudstackClient::Api

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

Constant Summary collapse

DEFAULT_API_VERSION =
"4.5"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Api



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cloudstack_client/api.rb', line 21

def initialize(options = {})
  if options[:api_file]
    @api_file = options[:api_file]
    @api_version = File.basename(@api_file, ".msgpack")
  else
    @api_version = options[:api_version] || DEFAULT_API_VERSION
    unless Api.versions.include? @api_version
      raise "API definition not found for #{@api_version}"
    end
    @api_file = File.join(Api.config_path, "#{@api_version}.msgpack")
  end
  @commands = load_commands
end

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



9
10
11
# File 'lib/cloudstack_client/api.rb', line 9

def api_version
  @api_version
end

#commandsObject (readonly)

Returns the value of attribute commands.



8
9
10
# File 'lib/cloudstack_client/api.rb', line 8

def commands
  @commands
end

Class Method Details

.config_pathObject



17
18
19
# File 'lib/cloudstack_client/api.rb', line 17

def self.config_path
  File.expand_path("../../../config/", __FILE__)
end

.versionsObject



11
12
13
14
15
# File 'lib/cloudstack_client/api.rb', line 11

def self.versions
  Dir["#{self.config_path}/*.msgpack"].map do |path|
    File.basename(path, ".msgpack")
  end
end

Instance Method Details

#all_required_params?(command, args) ⇒ Boolean



49
50
51
# File 'lib/cloudstack_client/api.rb', line 49

def all_required_params?(command, args)
  required_params(command).all? { |k| args.key? k }
end

#command_supported?(command) ⇒ Boolean



35
36
37
# File 'lib/cloudstack_client/api.rb', line 35

def command_supported?(command)
  @commands.has_key? command
end

#command_supports_param?(command, key) ⇒ Boolean



39
40
41
# File 'lib/cloudstack_client/api.rb', line 39

def command_supports_param?(command, key)
  @commands[command]["params"].detect { |p| p["name"] == key } ? true : false
end

#missing_params_msg(command) ⇒ Object



53
54
55
56
# File 'lib/cloudstack_client/api.rb', line 53

def missing_params_msg(command)
  requ = required_params(command)
  "#{command} requires the following parameter#{ 's' if requ.size > 1}: #{requ.join(', ')}"
end

#required_params(command) ⇒ Object



43
44
45
46
47
# File 'lib/cloudstack_client/api.rb', line 43

def required_params(command)
  @commands[command]["params"].map do |param|
    param["name"] if param["required"] == true
  end.compact
end