cloudstack_client 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6127adfd7dadcb64018f35d332657b3bc1337249
4
- data.tar.gz: 400f596034466b1efc5f406646f26b0cc1a2846a
3
+ metadata.gz: c717a0dab3ed2016549deae5d529b9da29b1f083
4
+ data.tar.gz: 13c959b12cbc809503910b5bfd97e910ab166946
5
5
  SHA512:
6
- metadata.gz: f4edc4749533a536be14b1b8c718abd47144ebba52a6bc9f737901cf32a94723394da94328680b6c1206857690ef7921ab6bf0403f3c9a45de155971ddef29e1
7
- data.tar.gz: 51a48c6e1934569891e42fa012645d831ebac09fbf397b89f1191371370355fcea9cfb7c91b9491d1e2e6da6c930770a7fc8246c49445182d01150cdc4d997e0
6
+ metadata.gz: c22640516a49e62f7346de350219097d9f155789435d9928b09c50c52e80bbf40584a42c3407516ca37a489862335657df6625214c18d857c459f30f846264a3
7
+ data.tar.gz: 61e750a05e16187411fb5b26bf26e5f363a5b2728f0442ccb4aa5b73a663f6465d084fd3320efdf038764852f5a982e9d11ad01f59721609df9c179ec031cbb6
data/.travis.yml ADDED
@@ -0,0 +1 @@
1
+ language: ruby
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # cloudstack_client
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/cloudstack_client.png)](http://badge.fury.io/rb/cloudstack_client)
4
+ [![Build Status](https://travis-ci.org/niwo/cloudstack_client.svg?branch=master)](https://travis-ci.org/niwo/cloudstack_client)
4
5
 
5
6
  A CloudStack API client written in Ruby.
6
7
 
data/Rakefile CHANGED
@@ -1,2 +1,12 @@
1
- require 'bundler'
2
- Bundler::GemHelper.install_tasks
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.pattern = "test/*_test.rb"
9
+ end
10
+
11
+ desc "Run Tests"
12
+ task default: :test
@@ -20,10 +20,11 @@ Gem::Specification.new do |gem|
20
20
  gem.require_paths = ["lib"]
21
21
  gem.rdoc_options = %w[--line-numbers --inline-source]
22
22
 
23
- gem.add_dependency('msgpack', '~> 0.5.11')
24
-
25
23
  gem.add_development_dependency('rdoc', '~> 4.2.0')
26
24
  gem.add_development_dependency('rake', '~> 10.4.2')
27
25
  gem.add_development_dependency('thor', '~> 0.19.1')
28
26
  gem.add_development_dependency('ripl', '~> 0.7.0')
27
+ gem.add_development_dependency('minitest', '~> 5.6.1')
28
+
29
+ gem.add_dependency('msgpack', '~> 0.5.11')
29
30
  end
data/config/4.2.msgpack CHANGED
Binary file
@@ -1,6 +1,4 @@
1
1
  require "msgpack"
2
- require "json"
3
- require "ostruct"
4
2
 
5
3
  module CloudstackClient
6
4
  class Api
@@ -20,22 +18,22 @@ module CloudstackClient
20
18
  @commands = load_commands
21
19
  end
22
20
 
21
+ def command_supported?(command)
22
+ @commands.has_key? command
23
+ end
24
+
23
25
  def command_supports_param?(command, key)
24
- commands[command].params.detect { |p| p["name"] == key }
26
+ @commands[command]["params"].detect { |p| p["name"] == key } ? true : false
25
27
  end
26
28
 
27
29
  def required_params(command)
28
- commands[command].params.map do |param|
30
+ @commands[command]["params"].map do |param|
29
31
  param["name"] if param["required"] == true
30
32
  end.compact
31
33
  end
32
34
 
33
35
  def all_required_params?(command, args)
34
- required_params(command).all? {|k| args.key? k}
35
- end
36
-
37
- def normalize_key(key)
38
- key.to_s.gsub("_", "")
36
+ required_params(command).all? { |k| args.key? k }
39
37
  end
40
38
 
41
39
  def missing_params_msg(command)
@@ -51,11 +49,8 @@ module CloudstackClient
51
49
  rescue => e
52
50
  raise "Error: Unable to read file '#{@api_file}' : #{e.message}"
53
51
  end
54
- @command_map
55
52
  commands = Hash.new
56
- api["api"].each do |command|
57
- commands[command["name"]] = OpenStruct.new command
58
- end
53
+ api.each { |command| commands[command["name"]] = command }
59
54
  commands
60
55
  end
61
56
 
@@ -49,7 +49,7 @@ module CloudstackClient
49
49
  option :remove_description, default: true, type: :boolean,
50
50
  desc: "remove description sections"
51
51
  def list_apis
52
- apis = client.send_request('command' => 'listApis')
52
+ apis = client(no_api_methods: true).send_request('command' => 'listApis')
53
53
  apis.each do |command|
54
54
  command.delete("response") if options[:remove_response]
55
55
  if options[:remove_description]
@@ -58,9 +58,9 @@ module CloudstackClient
58
58
  end
59
59
  end
60
60
 
61
- puts case options[:format]
61
+ print case options[:format]
62
62
  when "json"
63
- options[:pretty_print] ? JSON.pretty_generate(apis) : data.to_json
63
+ options[:pretty_print] ? JSON.pretty_generate(apis) : apis.to_json
64
64
  when "yaml"
65
65
  apis.to_yaml
66
66
  else
@@ -84,14 +84,15 @@ module CloudstackClient
84
84
  @client ||= CloudstackClient::Client.new(
85
85
  @config[:url],
86
86
  @config[:api_key],
87
- @config[:secret_key]
87
+ @config[:secret_key],
88
+ opts
88
89
  )
89
90
  end
90
91
 
91
92
  def load_configuration(config_file = options[:config_file], env = options[:env])
92
93
  unless File.exists?(config_file)
93
94
  say "Configuration file #{config_file} not found.", :red
94
- say "Please run \'cloudstack-cli environment add\' to create one."
95
+ say "Please run 'cloudstack-cli environment add' to create one."
95
96
  exit 1
96
97
  end
97
98
 
@@ -102,8 +103,7 @@ module CloudstackClient
102
103
  exit 1
103
104
  end
104
105
 
105
- env ||= config[:default]
106
- if env
106
+ if env ||= config[:default]
107
107
  unless config = config[env]
108
108
  say "Can't find environment #{env}.", :red
109
109
  exit 1
@@ -17,24 +17,27 @@ module CloudstackClient
17
17
  def define_api_methods
18
18
  @api = Api.new(@options)
19
19
  @api.commands.each do |name, command|
20
- method_name = camel_case_to_underscore(command.name).to_sym
20
+ method_name = camel_case_to_underscore(command["name"]).to_sym
21
21
 
22
22
  define_singleton_method(method_name) do |args = {}, options = {}|
23
- params = {"command" => command.name}
23
+ params = { "command" => command["name"] }
24
24
 
25
25
  args.each do |k, v|
26
- k = @api.normalize_key(k)
27
- if v != nil && @api.command_supports_param?(command.name, k)
26
+ k = k.to_s.gsub("_", "")
27
+ if v && @api.command_supports_param?(command["name"], k)
28
28
  params[k] = v
29
29
  end
30
30
  end
31
31
 
32
- unless @api.all_required_params?(command.name, params)
33
- raise ParameterError, @api.missing_params_msg(command.name)
32
+ unless @api.all_required_params?(command["name"], params)
33
+ raise ParameterError, @api.missing_params_msg(command["name"])
34
34
  end
35
35
 
36
- sync = command.isasync == false || options[:sync]
37
- sync ? send_request(params) : send_async_request(params)
36
+ if command["isasync"] == false || options[:sync]
37
+ send_request(params)
38
+ else
39
+ send_async_request(params)
40
+ end
38
41
  end
39
42
  end
40
43
  end
@@ -1,3 +1,3 @@
1
1
  module CloudstackClient
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
data/test/api_test.rb ADDED
@@ -0,0 +1,76 @@
1
+ require "test_helper"
2
+
3
+ describe CloudstackClient::Api do
4
+ before do
5
+ @api = CloudstackClient::Api.new
6
+ end
7
+
8
+ describe "when commands are accessed" do
9
+ it "must return a Hash" do
10
+ @api.commands.class.must_equal Hash
11
+ end
12
+
13
+ it "the Hash must have a key named 'listDomains'" do
14
+ @api.commands.has_key?('listDomains').must_equal true
15
+ end
16
+
17
+ it "the 'listDomains' element must contain the correct 'name' value" do
18
+ @api.commands['listDomains']['name'].must_equal "listDomains"
19
+ end
20
+
21
+ it "the 'listDomains' element must contain 'params'" do
22
+ @api.commands['listDomains'].has_key?('params').must_equal true
23
+ end
24
+ end
25
+
26
+ describe "when asked about supported commands" do
27
+ it "must respond positively for 'listVirtualMachines'" do
28
+ @api.command_supported?('listVirtualMachines').must_equal true
29
+ end
30
+
31
+ it "must respond positively for 'createUser'" do
32
+ @api.command_supported?('createUser').must_equal true
33
+ end
34
+
35
+ it "must respond netagively for 'listClowns'" do
36
+ @api.command_supported?('listClowns').must_equal false
37
+ end
38
+ end
39
+
40
+ describe "when asked about supported params" do
41
+ it "must respond positively for 'listVirtualMachines' and param 'name'" do
42
+ @api.command_supports_param?('listVirtualMachines', 'name').must_equal true
43
+ end
44
+
45
+ it "must respond netagively for 'listVirtualMachines' and param 'hotdog'" do
46
+ @api.command_supports_param?('listVirtualMachines', 'hotdog').must_equal false
47
+ end
48
+ end
49
+
50
+ describe "when asked about required params" do
51
+ it "must respond with correct Array for 'createUser'" do
52
+ params = %w(account email firstname lastname password username)
53
+ @api.required_params('createUser').sort.must_equal params.sort
54
+ end
55
+ end
56
+
57
+ describe "when asked about all required params" do
58
+ it "must respond positively for 'createUser' and params 'account, email, firtsname, lastname, password, username'" do
59
+ params = {
60
+ "account" => "Master",
61
+ "email" => "me@me.com",
62
+ "firstname" => "Me",
63
+ "lastname" => "Me",
64
+ "password" => "secret",
65
+ "username" => "meme",
66
+ "domainid" => "1"
67
+ }
68
+ @api.all_required_params?('createUser', params).must_equal true
69
+ end
70
+
71
+ it "must respond netagively for 'createUser' and params 'username'" do
72
+ @api.all_required_params?('createUser', { "username" => "meme" }).must_equal false
73
+ end
74
+ end
75
+
76
+ end
data/test/benchmark.rb ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "cloudstack_client/api"
4
+ require "benchmark"
5
+ require "json"
6
+
7
+ GC.disable
8
+
9
+ memory_before = `ps -o rss= -p #{Process.pid}`.to_i/1024
10
+ gc_stat_before = GC.stat
11
+
12
+ time = Benchmark.realtime do
13
+ 100.times do
14
+ api = CloudstackClient::Api.new
15
+ command = "listVirtualMachines"
16
+ api.command_supports_param?(command, "id")
17
+ api.required_params(command)
18
+ api.all_required_params?("deployVirtualMachine", { name: "test", templateid: 1 })
19
+ end
20
+ end
21
+
22
+ gc_stat_after = GC.stat
23
+ memory_after = `ps -o rss= -p #{Process.pid}`.to_i/1024
24
+
25
+ puts(
26
+ {
27
+ time: time.round(2),
28
+ gc_count: gc_stat_after[:count] - gc_stat_before[:count],
29
+ memory: "%dM" % (memory_after - memory_before)
30
+ }.to_json
31
+ )
@@ -0,0 +1,29 @@
1
+ require "test_helper"
2
+
3
+ describe CloudstackClient::Client do
4
+ before do
5
+ @client = CloudstackClient::Client.new(
6
+ "https://cloudstack.api/client/api",
7
+ "test-key",
8
+ "test-secret"
9
+ )
10
+ end
11
+
12
+ describe "when the client is instantiated" do
13
+ it "must respond_to 'list_virtual_machines'" do
14
+ @client.respond_to?(:list_virtual_machines).must_equal true
15
+ end
16
+
17
+ it "must respond_to 'deploy_virtual_machine'" do
18
+ @client.respond_to?(:deploy_virtual_machine).must_equal true
19
+ end
20
+
21
+ it "must respond_to 'create_user'" do
22
+ @client.respond_to?(:create_user).must_equal true
23
+ end
24
+
25
+ it "must not be in debug mode" do
26
+ @client.debug.must_equal false
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ require "cloudstack_client"
2
+
3
+ require "minitest/spec"
4
+ require "minitest/autorun"
5
+ require "minitest/pride"
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudstack_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nik Wolfgramm
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-01 00:00:00.000000000 Z
11
+ date: 2015-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: msgpack
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 0.5.11
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: 0.5.11
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rdoc
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +66,34 @@ dependencies:
80
66
  - - "~>"
81
67
  - !ruby/object:Gem::Version
82
68
  version: 0.7.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 5.6.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 5.6.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: msgpack
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.5.11
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.5.11
83
97
  description: CloudStack API client written in Ruby
84
98
  email:
85
99
  - nik.wolfgramm@gmail.com
@@ -89,6 +103,7 @@ extensions: []
89
103
  extra_rdoc_files: []
90
104
  files:
91
105
  - ".gitignore"
106
+ - ".travis.yml"
92
107
  - Gemfile
93
108
  - LICENSE.txt
94
109
  - README.md
@@ -104,6 +119,10 @@ files:
104
119
  - lib/cloudstack_client/error.rb
105
120
  - lib/cloudstack_client/utils.rb
106
121
  - lib/cloudstack_client/version.rb
122
+ - test/api_test.rb
123
+ - test/benchmark.rb
124
+ - test/client_test.rb
125
+ - test/test_helper.rb
107
126
  homepage: https://github.com/niwo/cloudstack_client
108
127
  licenses:
109
128
  - MIT
@@ -130,4 +149,8 @@ rubygems_version: 2.2.2
130
149
  signing_key:
131
150
  specification_version: 4
132
151
  summary: CloudStack API client written in Ruby
133
- test_files: []
152
+ test_files:
153
+ - test/api_test.rb
154
+ - test/benchmark.rb
155
+ - test/client_test.rb
156
+ - test/test_helper.rb