Class: CloudstackClient::Api

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

Constant Summary collapse

DEFAULT_API_VERSION =
"4.5"
API_PATH =
File.expand_path("../../../data/", __FILE__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#camel_case_to_underscore, #print_debug_output, #underscore_to_camel_case

Constructor Details

#initialize(options = {}) ⇒ Api

Returns a new instance of Api.



22
23
24
25
26
# File 'lib/cloudstack_client/api.rb', line 22

def initialize(options = {})
  set_api_path(options)
  set_api_version_and_file(options)
  load_commands
end

Instance Attribute Details

#api_fileObject (readonly)

Returns the value of attribute api_file.



14
15
16
# File 'lib/cloudstack_client/api.rb', line 14

def api_file
  @api_file
end

#api_pathObject (readonly)

Returns the value of attribute api_path.



14
15
16
# File 'lib/cloudstack_client/api.rb', line 14

def api_path
  @api_path
end

#api_versionObject (readonly)

Returns the value of attribute api_version.



14
15
16
# File 'lib/cloudstack_client/api.rb', line 14

def api_version
  @api_version
end

#commandsObject (readonly)

Returns the value of attribute commands.



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

def commands
  @commands
end

Class Method Details

.versions(api_path = API_PATH) ⇒ Object



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

def self.versions(api_path = API_PATH)
  Dir[api_path + "/*.json.gz"].map do |path|
    File.basename(path, ".json.gz")
  end
end

Instance Method Details

#all_required_params?(command, args) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/cloudstack_client/api.rb', line 64

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

#command_supported?(command) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/cloudstack_client/api.rb', line 28

def command_supported?(command)
  !find_command(command).nil?
end

#command_supports_param?(command, key) ⇒ Boolean

Returns:

  • (Boolean)


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

def command_supports_param?(command, key)
  command = find_command(command)
  return false if command.nil?

  command["params"].any? { |param| param["name"] == key.to_s }
end

#find_command(command) ⇒ Object

Resolves a command given either its CloudStack name ("createSSHKeyPair") or its underscored Ruby name ("create_ssh_key_pair").

underscore_to_camel_case cannot be relied on here: converting to underscores loses acronym casing, so "create_ssh_key_pair" converts back to "createSshKeyPair", which is not a CloudStack command. Underscored names are resolved through an index built in the lossy direction instead.

The index is built on first use: most lookups arrive as CloudStack names and hit @commands directly, so instantiating an Api should not pay for it.



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

def find_command(command)
  command = command.to_s
  @commands[command] || underscored_commands[command]
end

#missing_params_msg(command) ⇒ Object



68
69
70
71
72
# File 'lib/cloudstack_client/api.rb', line 68

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

#params(command) ⇒ Object



60
61
62
# File 'lib/cloudstack_client/api.rb', line 60

def params(command)
  @commands[command]["params"]
end

#required_params(command) ⇒ Object



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

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