cloudstack_client 1.0.5 → 1.1.0
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 +4 -4
- data/README.md +39 -9
- data/cloudstack_client.gemspec +1 -1
- data/data/4.5.json.gz +0 -0
- data/lib/cloudstack_client/api.rb +55 -31
- data/lib/cloudstack_client/cli.rb +6 -6
- data/lib/cloudstack_client/version.rb +1 -1
- data/test/api_test.rb +25 -1
- data/test/benchmark.rb +5 -7
- data/test/data/0.42.json +15 -0
- data/test/data/0.42.json.gz +0 -0
- metadata +10 -7
- data/config/4.2.msgpack +0 -0
- data/config/4.5.msgpack +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f64a91a71aa5ffe62432e9d5c306288f5814ab8
|
4
|
+
data.tar.gz: 529f9c16b616564a52f9a2abbbc3239a7e990abb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4989517ca038f29d621dc07f9956c3e7f30439571e6808a8894f17c0f74f43980cc7d65f8a01668c0124dac995c95aff8cf7b68f858cd9e334dfbee53832497e
|
7
|
+
data.tar.gz: 4fec2c1962dc14e1f6d7d7b58ffab771652c3690c0e2417be447b3dbdcb0880ec326a6897887d96faeac6c9952d000d262478b41597fc38148ab92e9b7a0775b
|
data/README.md
CHANGED
@@ -13,8 +13,18 @@ Install the cloudstack_client gem:
|
|
13
13
|
$ gem install cloudstack_client
|
14
14
|
```
|
15
15
|
|
16
|
+
## Features
|
17
|
+
- Dynamically builds API methods based on the lisApis function of CloudStack
|
18
|
+
- Command names are converted to match Ruby naming conventions (i.e. ListVirtualMachines becomes list_virtual_machines)
|
19
|
+
- Accepts Ruby style args passed to commands (i.e. list_all: true becomes listall=true)
|
20
|
+
- makes sure all required arguments are passed
|
21
|
+
- Removes unsupported arguments and arguments with nil values from commands
|
22
|
+
|
23
|
+
|
16
24
|
## Usage
|
17
25
|
|
26
|
+
### Basic usage
|
27
|
+
|
18
28
|
```ruby
|
19
29
|
require "cloudstack_client"
|
20
30
|
|
@@ -29,25 +39,45 @@ cs.list_virtual_machines(state: "running").each do |server|
|
|
29
39
|
end
|
30
40
|
```
|
31
41
|
|
32
|
-
|
42
|
+
### Initialize with options
|
33
43
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
44
|
+
Load API definition file from a alternative path and set the version:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
cs = CloudstackClient::Client.new(
|
48
|
+
"https://cloudstack.local/client/api",
|
49
|
+
"API_KEY",
|
50
|
+
"API_SECRET",
|
51
|
+
{
|
52
|
+
api_path: "~/cloudstack",
|
53
|
+
api_version: "4.6"
|
54
|
+
}
|
55
|
+
)
|
56
|
+
```
|
57
|
+
|
58
|
+
..or load the API definition directly from a file:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
cs = CloudstackClient::Client.new(
|
62
|
+
"https://cloudstack.local/client/api",
|
63
|
+
"API_KEY",
|
64
|
+
"API_SECRET",
|
65
|
+
{ api_file: "~/cloudstack/4.6.json.gz" }
|
66
|
+
)
|
67
|
+
```
|
39
68
|
|
40
69
|
## Development
|
41
70
|
|
42
|
-
### Generate
|
71
|
+
### Generate/Update API versions
|
43
72
|
|
44
|
-
New API configs can be
|
73
|
+
New API configs can be generated using the list_apis command.
|
45
74
|
|
46
75
|
*Example:*
|
47
76
|
|
48
77
|
```bash
|
49
78
|
# running against an CloudStack 4.5 API endpoint:
|
50
|
-
$ cloudstack_client list_apis >
|
79
|
+
$ cloudstack_client list_apis > data/4.5.json
|
80
|
+
$ gzip data/4.5.json
|
51
81
|
```
|
52
82
|
|
53
83
|
### Interactive Console
|
data/cloudstack_client.gemspec
CHANGED
data/data/4.5.json.gz
ADDED
Binary file
|
@@ -1,35 +1,24 @@
|
|
1
|
-
require "
|
1
|
+
require "zlib"
|
2
2
|
|
3
3
|
module CloudstackClient
|
4
4
|
class Api
|
5
5
|
|
6
6
|
DEFAULT_API_VERSION = "4.5"
|
7
|
+
API_PATH = File.expand_path("../../../data/", __FILE__)
|
7
8
|
|
8
9
|
attr_reader :commands
|
9
|
-
attr_reader :api_version
|
10
|
+
attr_reader :api_version, :api_file, :api_path
|
10
11
|
|
11
|
-
def self.versions
|
12
|
-
Dir["
|
13
|
-
File.basename(path, ".
|
12
|
+
def self.versions(api_path = API_PATH)
|
13
|
+
Dir[api_path + "/*.json.gz"].map do |path|
|
14
|
+
File.basename(path, ".json.gz")
|
14
15
|
end
|
15
16
|
end
|
16
17
|
|
17
|
-
def self.config_path
|
18
|
-
File.expand_path("../../../config/", __FILE__)
|
19
|
-
end
|
20
|
-
|
21
18
|
def initialize(options = {})
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
else
|
26
|
-
@api_version = options[:api_version] || DEFAULT_API_VERSION
|
27
|
-
unless Api.versions.include? @api_version
|
28
|
-
raise "API definition not found for #{@api_version}"
|
29
|
-
end
|
30
|
-
@api_file = File.join(Api.config_path, "#{@api_version}.msgpack")
|
31
|
-
end
|
32
|
-
@commands = load_commands
|
19
|
+
set_api_path(options)
|
20
|
+
set_api_version_and_file(options)
|
21
|
+
load_commands
|
33
22
|
end
|
34
23
|
|
35
24
|
def command_supported?(command)
|
@@ -37,7 +26,11 @@ module CloudstackClient
|
|
37
26
|
end
|
38
27
|
|
39
28
|
def command_supports_param?(command, key)
|
40
|
-
@commands[command]["params"].detect { |p| p["name"] == key }
|
29
|
+
if @commands[command]["params"].detect { |p| p["name"] == key }
|
30
|
+
true
|
31
|
+
else
|
32
|
+
false
|
33
|
+
end
|
41
34
|
end
|
42
35
|
|
43
36
|
def required_params(command)
|
@@ -51,21 +44,52 @@ module CloudstackClient
|
|
51
44
|
end
|
52
45
|
|
53
46
|
def missing_params_msg(command)
|
54
|
-
|
55
|
-
|
47
|
+
"#{command} requires the following parameter" +
|
48
|
+
"#{ 's' if required_params(command).size > 1 }: " +
|
49
|
+
required_params(command).join(", ")
|
56
50
|
end
|
57
51
|
|
58
52
|
private
|
59
53
|
|
60
|
-
def
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
54
|
+
def set_api_version_and_file(options)
|
55
|
+
if options[:api_file]
|
56
|
+
@api_file = options[:api_file]
|
57
|
+
@api_version = File.basename(@api_file, ".json.gz")
|
58
|
+
else
|
59
|
+
set_api_version(options)
|
60
|
+
@api_file = File.join(@api_path, "#{@api_version}.json.gz")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def set_api_path(options)
|
65
|
+
@api_path = if options[:api_path]
|
66
|
+
File.expand_path(options[:api_path])
|
67
|
+
else
|
68
|
+
API_PATH
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def set_api_version(options)
|
73
|
+
@api_version = options[:api_version] || DEFAULT_API_VERSION
|
74
|
+
unless Api.versions(@api_path).include? @api_version
|
75
|
+
if options[:api_version]
|
76
|
+
raise "API definition not found for version '#{@api_version}' in api_path '#{@api_path}'"
|
77
|
+
elsif Api.versions(@api_path).size < 1
|
78
|
+
raise "no API file available in api_path '#{@api_path}'"
|
79
|
+
else
|
80
|
+
@api_version = Api.versions(@api_path).last
|
81
|
+
end
|
65
82
|
end
|
66
|
-
|
67
|
-
|
68
|
-
|
83
|
+
@api_version
|
84
|
+
end
|
85
|
+
|
86
|
+
def load_commands
|
87
|
+
@commands = {}
|
88
|
+
Zlib::GzipReader.open(@api_file) do |gz|
|
89
|
+
JSON.parse(gz.read)
|
90
|
+
end.each {|cmd| @commands[cmd["name"]] = cmd }
|
91
|
+
rescue => e
|
92
|
+
raise "Error: Unable to read file '#{@api_file}': #{e.message}"
|
69
93
|
end
|
70
94
|
|
71
95
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
require "cloudstack_client/client"
|
2
2
|
require "yaml"
|
3
|
+
require "json"
|
4
|
+
require 'pp'
|
3
5
|
|
4
6
|
begin
|
5
7
|
require "thor"
|
@@ -40,8 +42,8 @@ module CloudstackClient
|
|
40
42
|
map %w(-v --version) => :version
|
41
43
|
|
42
44
|
desc "list_apis", "list api commands using the Cloudstack API Discovery service"
|
43
|
-
option :format, default: '
|
44
|
-
enum: %w(
|
45
|
+
option :format, default: 'json',
|
46
|
+
enum: %w(json yaml), desc: "output format"
|
45
47
|
option :pretty_print, default: true, type: :boolean,
|
46
48
|
desc: "pretty print json output"
|
47
49
|
option :remove_response, default: true, type: :boolean,
|
@@ -59,12 +61,10 @@ module CloudstackClient
|
|
59
61
|
end
|
60
62
|
|
61
63
|
print case options[:format]
|
62
|
-
when "json"
|
63
|
-
options[:pretty_print] ? JSON.pretty_generate(apis) : apis.to_json
|
64
64
|
when "yaml"
|
65
65
|
apis.to_yaml
|
66
66
|
else
|
67
|
-
apis.
|
67
|
+
options[:pretty_print] ? JSON.pretty_generate(apis) : apis.to_json
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
@@ -84,7 +84,7 @@ module CloudstackClient
|
|
84
84
|
ARGV.clear
|
85
85
|
env = options[:env] ? options[:env] : load_configuration.last
|
86
86
|
Ripl.config[:prompt] = "#{env} >> "
|
87
|
-
Ripl.start binding: cs_client.instance_eval
|
87
|
+
Ripl.start binding: cs_client.instance_eval{ binding }
|
88
88
|
end
|
89
89
|
|
90
90
|
no_commands do
|
data/test/api_test.rb
CHANGED
@@ -5,12 +5,36 @@ describe CloudstackClient::Api do
|
|
5
5
|
@api = CloudstackClient::Api.new
|
6
6
|
end
|
7
7
|
|
8
|
+
describe "when the API is initialized" do
|
9
|
+
it "has to find the default version in available versions" do
|
10
|
+
CloudstackClient::Api.versions.include?(
|
11
|
+
CloudstackClient::Api::DEFAULT_API_VERSION
|
12
|
+
).must_equal true
|
13
|
+
end
|
14
|
+
|
15
|
+
it "must raise exception for unavailable version" do
|
16
|
+
proc { CloudstackClient::Api.new(api_version: "0.0") }.must_raise RuntimeError
|
17
|
+
end
|
18
|
+
|
19
|
+
it "must set correct version of fake api file" do
|
20
|
+
CloudstackClient::Api.new(
|
21
|
+
api_file: "#{File.expand_path File.dirname(__FILE__)}/data/0.42.json.gz"
|
22
|
+
).api_version.must_equal "0.42"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "must set correct api file of fake api when loaded with api_version option" do
|
26
|
+
CloudstackClient::Api.new(
|
27
|
+
api_path: "#{File.expand_path File.dirname(__FILE__)}/data/",
|
28
|
+
).api_file.must_equal "#{File.expand_path File.dirname(__FILE__)}/data/0.42.json.gz"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
8
32
|
describe "when commands are accessed" do
|
9
33
|
it "must return a Hash" do
|
10
34
|
@api.commands.class.must_equal Hash
|
11
35
|
end
|
12
36
|
|
13
|
-
it "
|
37
|
+
it "must have a key named 'listDomains'" do
|
14
38
|
@api.commands.has_key?('listDomains').must_equal true
|
15
39
|
end
|
16
40
|
|
data/test/benchmark.rb
CHANGED
@@ -22,10 +22,8 @@ end
|
|
22
22
|
gc_stat_after = GC.stat
|
23
23
|
memory_after = `ps -o rss= -p #{Process.pid}`.to_i/1024
|
24
24
|
|
25
|
-
puts(
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
}.to_json
|
31
|
-
)
|
25
|
+
puts({
|
26
|
+
time: time.round(2),
|
27
|
+
gc_count: gc_stat_after[:count] - gc_stat_before[:count],
|
28
|
+
memory: "%dM" % (memory_after - memory_before)
|
29
|
+
}.to_json)
|
data/test/data/0.42.json
ADDED
Binary file
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cloudstack_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
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-11-
|
11
|
+
date: 2015-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rdoc
|
@@ -81,19 +81,19 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 5.8.2
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: json
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
89
|
+
version: 1.8.3
|
90
90
|
type: :runtime
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
96
|
+
version: 1.8.3
|
97
97
|
description: CloudStack API client written in Ruby
|
98
98
|
email:
|
99
99
|
- nik.wolfgramm@gmail.com
|
@@ -110,8 +110,7 @@ files:
|
|
110
110
|
- Rakefile
|
111
111
|
- bin/cloudstack_client
|
112
112
|
- cloudstack_client.gemspec
|
113
|
-
-
|
114
|
-
- config/4.5.msgpack
|
113
|
+
- data/4.5.json.gz
|
115
114
|
- lib/cloudstack_client.rb
|
116
115
|
- lib/cloudstack_client/api.rb
|
117
116
|
- lib/cloudstack_client/cli.rb
|
@@ -123,6 +122,8 @@ files:
|
|
123
122
|
- test/api_test.rb
|
124
123
|
- test/benchmark.rb
|
125
124
|
- test/client_test.rb
|
125
|
+
- test/data/0.42.json
|
126
|
+
- test/data/0.42.json.gz
|
126
127
|
- test/test_helper.rb
|
127
128
|
homepage: https://github.com/niwo/cloudstack_client
|
128
129
|
licenses:
|
@@ -154,4 +155,6 @@ test_files:
|
|
154
155
|
- test/api_test.rb
|
155
156
|
- test/benchmark.rb
|
156
157
|
- test/client_test.rb
|
158
|
+
- test/data/0.42.json
|
159
|
+
- test/data/0.42.json.gz
|
157
160
|
- test/test_helper.rb
|
data/config/4.2.msgpack
DELETED
Binary file
|
data/config/4.5.msgpack
DELETED
Binary file
|