cloudstack_client 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 43509af9b335f021f6a89803c57489e0fa9a4c41
4
- data.tar.gz: f3ea42424fd79f38946b04d06f4199efb0820c33
3
+ metadata.gz: 6127adfd7dadcb64018f35d332657b3bc1337249
4
+ data.tar.gz: 400f596034466b1efc5f406646f26b0cc1a2846a
5
5
  SHA512:
6
- metadata.gz: 843305242656b87b1bc394156b093a627285b07e9164f5e20831aa4152677e6c9843f48908398d06b49c74ad7aaad8b34fc917922b285314b6feb2b291f882be
7
- data.tar.gz: 51e7e002f60f1a0b6be2186811eb71c9bde6c1d2a14dfed07febc2a73f3938d006ed4b45dd5e91399f5f2d63d0413f8c629183dcf40a4a5ef60883ca8a65efc5
6
+ metadata.gz: f4edc4749533a536be14b1b8c718abd47144ebba52a6bc9f737901cf32a94723394da94328680b6c1206857690ef7921ab6bf0403f3c9a45de155971ddef29e1
7
+ data.tar.gz: 51a48c6e1934569891e42fa012645d831ebac09fbf397b89f1191371370355fcea9cfb7c91b9491d1e2e6da6c930770a7fc8246c49445182d01150cdc4d997e0
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2013 Nik Wolfgramm
3
+ Copyright (c) 2013-2015 Nik Wolfgramm
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
21
+ THE SOFTWARE.
@@ -7,6 +7,8 @@ module CloudstackClient
7
7
 
8
8
  DEFAULT_API_VERSION = "4.2"
9
9
 
10
+ attr_reader :commands
11
+
10
12
  def initialize(options = {})
11
13
  if options[:api_file]
12
14
  @api_file = options[:api_file]
@@ -15,15 +17,46 @@ module CloudstackClient
15
17
  @api_version = options[:api_version] || DEFAULT_API_VERSION
16
18
  @api_file = File.expand_path("../../../config/#{@api_version}.msgpack", __FILE__)
17
19
  end
20
+ @commands = load_commands
21
+ end
22
+
23
+ def command_supports_param?(command, key)
24
+ commands[command].params.detect { |p| p["name"] == key }
25
+ end
26
+
27
+ def required_params(command)
28
+ commands[command].params.map do |param|
29
+ param["name"] if param["required"] == true
30
+ end.compact
31
+ end
32
+
33
+ def all_required_params?(command, args)
34
+ required_params(command).all? {|k| args.key? k}
18
35
  end
19
36
 
20
- def commands
37
+ def normalize_key(key)
38
+ key.to_s.gsub("_", "")
39
+ end
40
+
41
+ def missing_params_msg(command)
42
+ requ = required_params(command)
43
+ "#{command} requires the following parameter#{ 's' if requ.size > 1}: #{requ.join(', ')}"
44
+ end
45
+
46
+ private
47
+
48
+ def load_commands
21
49
  begin
22
50
  api = MessagePack.unpack(IO.read @api_file)
23
51
  rescue => e
24
52
  raise "Error: Unable to read file '#{@api_file}' : #{e.message}"
25
53
  end
26
- api["api"].map {|command| OpenStruct.new command }
54
+ @command_map
55
+ commands = Hash.new
56
+ api["api"].each do |command|
57
+ commands[command["name"]] = OpenStruct.new command
58
+ end
59
+ commands
27
60
  end
28
61
 
29
62
  end
@@ -1,9 +1,9 @@
1
- require 'cloudstack_client/client'
2
- require 'yaml'
1
+ require "cloudstack_client/client"
2
+ require "yaml"
3
3
 
4
4
  begin
5
- require 'thor'
6
- require 'ripl'
5
+ require "thor"
6
+ require "ripl"
7
7
  rescue LoadError => e
8
8
  missing_gem = if e.message =~ /thor/
9
9
  "thor"
@@ -12,7 +12,7 @@ rescue LoadError => e
12
12
  else
13
13
  raise
14
14
  end
15
- puts "Please install the #{missing_gem} gem first ('gem install #{missing_gem}')"
15
+ puts "Please install the #{missing_gem} gem first ('gem install #{missing_gem}')."
16
16
  exit 1
17
17
  end
18
18
 
@@ -1,35 +1,36 @@
1
1
  require "cloudstack_client/api"
2
2
  require "cloudstack_client/error"
3
+ require "cloudstack_client/utils"
3
4
  require "cloudstack_client/connection"
4
5
 
5
6
  module CloudstackClient
6
7
  class Client < Connection
7
-
8
- attr_accessor :api_version, :api_path
8
+ include Utils
9
+ attr_accessor :options
10
+ attr_reader :api
9
11
 
10
12
  def initialize(api_url, api_key, secret_key, options = {})
11
- super(api_url, api_key, secret_key, options = {})
12
- @api_version = options[:api_version] if options[:api_version]
13
- @api_file = options[:api_file] if options[:api_file]
13
+ super(api_url, api_key, secret_key, options)
14
14
  define_api_methods unless options[:no_api_methods]
15
15
  end
16
16
 
17
17
  def define_api_methods
18
- Api.new(api_file: @api_file, api_version: @api_version).commands.each do |command|
18
+ @api = Api.new(@options)
19
+ @api.commands.each do |name, command|
19
20
  method_name = camel_case_to_underscore(command.name).to_sym
20
21
 
21
22
  define_singleton_method(method_name) do |args = {}, options = {}|
22
23
  params = {"command" => command.name}
23
24
 
24
25
  args.each do |k, v|
25
- k = normalize_key(k)
26
- if v != nil && command_supports_key?(command, k)
26
+ k = @api.normalize_key(k)
27
+ if v != nil && @api.command_supports_param?(command.name, k)
27
28
  params[k] = v
28
29
  end
29
30
  end
30
31
 
31
- unless all_required_args?(command, params)
32
- raise ParameterError, missing_args_msg(command)
32
+ unless @api.all_required_params?(command.name, params)
33
+ raise ParameterError, @api.missing_params_msg(command.name)
33
34
  end
34
35
 
35
36
  sync = command.isasync == false || options[:sync]
@@ -38,37 +39,5 @@ module CloudstackClient
38
39
  end
39
40
  end
40
41
 
41
- def command_supports_key?(command, key)
42
- command.params.detect { |p| p["name"] == key }
43
- end
44
-
45
- def required_args(command)
46
- command.params.map do |param|
47
- param["name"] if param["required"] == true
48
- end.compact
49
- end
50
-
51
- def all_required_args?(command, args)
52
- required_args(command).all? {|k| args.key? k}
53
- end
54
-
55
- private
56
-
57
- def normalize_key(key)
58
- key.to_s.gsub("_", "")
59
- end
60
-
61
- def missing_args_msg(command)
62
- requ = required_args(command)
63
- "#{command.name} requires the following argument#{ 's' if requ.size > 1}: #{requ.join(', ')}"
64
- end
65
-
66
- def camel_case_to_underscore(camel_case)
67
- camel_case.gsub(/::/, '/').
68
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
69
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
70
- tr("-", "_").downcase
71
- end
72
-
73
42
  end # class
74
43
  end
@@ -1,10 +1,10 @@
1
- require 'base64'
2
- require 'openssl'
3
- require 'uri'
4
- require 'cgi'
5
- require 'net/http'
6
- require 'net/https'
7
- require 'json'
1
+ require "base64"
2
+ require "openssl"
3
+ require "uri"
4
+ require "cgi"
5
+ require "net/http"
6
+ require "net/https"
7
+ require "json"
8
8
 
9
9
  module CloudstackClient
10
10
  class Connection
@@ -20,6 +20,7 @@ module CloudstackClient
20
20
  @debug = options[:debug] ? true : false
21
21
  @async_poll_interval = options[:async_poll_interval] || 2.0
22
22
  @async_timeout = options[:async_timeout] || 400
23
+ @options = options
23
24
  end
24
25
 
25
26
  ##
@@ -62,7 +63,7 @@ module CloudstackClient
62
63
  body = JSON.parse(response.body).values.first
63
64
  rescue JSON::ParserError
64
65
  raise ParseError,
65
- "Response from server is not readable. Check if the API endpoint (#{@api_url}) is correct is accessible."
66
+ "Response from server is not readable. Check if the API endpoint (#{@api_url}) is correct and accessible."
66
67
  end
67
68
 
68
69
  if response.is_a? Net::HTTPOK
@@ -77,8 +78,8 @@ module CloudstackClient
77
78
  body.size == 0 ? [] : body
78
79
  end
79
80
  else
80
- message = data[data.keys.first]['errortext'] rescue body
81
- raise ApiError, "Error #{response.code} - #{message}."
81
+ message = body['errortext'] rescue body
82
+ raise ApiError, "Status #{response.code}: #{message}."
82
83
  end
83
84
  end
84
85
 
@@ -0,0 +1,10 @@
1
+ module CloudstackClient
2
+ module Utils
3
+ def camel_case_to_underscore(camel_case)
4
+ camel_case.gsub(/::/, '/').
5
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
6
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
7
+ tr("-", "_").downcase
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module CloudstackClient
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
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.1
4
+ version: 1.0.2
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-04-29 00:00:00.000000000 Z
11
+ date: 2015-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack
@@ -102,6 +102,7 @@ files:
102
102
  - lib/cloudstack_client/client.rb
103
103
  - lib/cloudstack_client/connection.rb
104
104
  - lib/cloudstack_client/error.rb
105
+ - lib/cloudstack_client/utils.rb
105
106
  - lib/cloudstack_client/version.rb
106
107
  homepage: https://github.com/niwo/cloudstack_client
107
108
  licenses: