cloudstack_helper 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,26 @@
1
+ # cloudstack_helper
2
+ cloudstack_helper is a simple ruby library that helps making request to CloudStack API easier. With cloudstack_helper, you only have to specify the command the parameters for the request and it will handle everything for you.
3
+
4
+ ## Library Usage
5
+
6
+ ```ruby
7
+ require 'cloudstack_helper'
8
+
9
+ API_URL = "http://mycloudstack:8080/client/api"
10
+ API_KEY = "WiUVlx74PpVE8w7z7hasgJjiOQvMsMWLFL3A054D_IPeSxxcm1PoNlpYlA2ujGSJBGiS2uBcG0GoLwLisosDCA"
11
+ SECRET_KEY = "uqzgrfMXnYSVvUi2XahgiDgvtXBemILs_npqi90KiYw0OKjyETOvQTtYg093EUrHSLZJEnI3lV1z9PrQmv2SxQ"
12
+ cs_helper = CloudStackHelper.new(:api_key => API_KEY, :secret_key => SECRET_KEY, :api_url => API_URL)
13
+
14
+ params = {:command => "listZones"}
15
+ puts cs_helper.get(params).body
16
+
17
+ params[:response] = "json"
18
+ puts cs_helper.get(params).body
19
+ ehelper.request :post, 'http://api.enstratus.com/api/enstratus/2011-02-24/admin/BillingCode', :data => json_data, :header => {'Accept' => 'application/json'}
20
+ ```
21
+
22
+ ## Executable Script Usage
23
+ ```
24
+ cloudstack_rb -x listUsers domainid=1
25
+ cloudstack_rb -x deployVirtualMachine serviceofferingid=12 templateid=4 zoneid=1 displayname=ohyea
26
+ ```
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ require 'yaml'
4
+
5
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'cloudstack_helper'
7
+
8
+ options = []
9
+ command = nil
10
+ conf_file = "cloudstack.yaml"
11
+ opts = OptionParser.new(nil, 24, ' ')
12
+ opts.banner = 'Usage: cloudstack_rb -c conf -x command param1=value1 param2=value2'
13
+ opts.on('-x', '--execute COMMAND', 'command to execute. See CloudStack API for list of commands.') do |opt|
14
+ command = opt
15
+ end
16
+ opts.on('-c', '--conf FILE', 'YAML config file that stores api and secret key') do |opt|
17
+ conf_file = opt
18
+ end
19
+ opts.on_tail('-h', '--help', 'Show this message.') do
20
+ puts opts
21
+ exit
22
+ end
23
+
24
+ params = opts.parse(ARGV)
25
+
26
+ if ARGV.length == 0
27
+ puts opts
28
+ exit
29
+ end
30
+
31
+ # Read config file for key and api url
32
+ unless File.exists?(conf_file)
33
+ abort("Unable to read config file")
34
+ end
35
+ conf = YAML.load(File.read(conf_file))
36
+
37
+ cs_helper = CloudStackHelper.new(:api_key => conf['api_key'], :secret_key => conf['secret_key'], :api_url => conf['api_url'])
38
+
39
+ params_hash= {:command => command}
40
+ params.each do |param|
41
+ tokens = param.split("=")
42
+ next unless tokens.size == 2
43
+ params_hash[tokens[0]] = tokens[1]
44
+ end
45
+
46
+ puts cs_helper.get(params_hash).body
@@ -0,0 +1,74 @@
1
+ require 'rubygems'
2
+ require 'base64'
3
+ require 'rest_client'
4
+ require 'openssl'
5
+ require 'cgi'
6
+
7
+ class CloudStackHelper
8
+ CONFIGURABLE_ATTRIBUTES = [
9
+ :response,
10
+ :secret_key,
11
+ :api_key,
12
+ :api_url
13
+ ]
14
+ attr_accessor *CONFIGURABLE_ATTRIBUTES
15
+
16
+ def initialize(options = {})
17
+ CONFIGURABLE_ATTRIBUTES.each do |attribute_name|
18
+ self.send("#{attribute_name}=", options[attribute_name]) if options.has_key?(attribute_name)
19
+ end
20
+ end
21
+
22
+ # To generate the signature:
23
+ # 1. For each field-value pair (as separated by a ‘&’) in the Command String, URL encode each value so that it can be safely sent via HTTP GET.
24
+ # NOTE: Make sure all spaces are encoded as “%20” rather than “+”.
25
+ # 2. Lower case the entire Command String and sort it alphabetically via the field for each field-value pair.
26
+ # 3. Take the sorted Command String and run it through the HMAC SHA-1 hashing algorithm
27
+ # with the user’s Secret Key.
28
+ # 4. Base64 encode the resulting byte array in UTF-8 so that it can be safely transmitted via HTTP.
29
+ def generate_signature(params)
30
+ params.each { |k,v| params[k] = CGI.escape(v).gsub('+', '%20').downcase }
31
+ sorted_params = params.sort_by{|key,value| key.to_s}
32
+
33
+ data = parameterize(sorted_params, false)
34
+
35
+ hash = OpenSSL::HMAC.digest('sha1', @secret_key, data)
36
+ signature = Base64.b64encode(hash).chomp
37
+ end
38
+
39
+ def parameterize(params, escape=true)
40
+ params = params.collect do |k,v|
41
+ if escape
42
+ "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
43
+ else
44
+ "#{k}=#{v}"
45
+ end
46
+ end
47
+ params.join('&')
48
+ end
49
+
50
+ def generate_params_str(params)
51
+ unless params[:response]
52
+ params[:response] = @response if @response
53
+ end
54
+ params[:apikey] = @api_key
55
+ params[:signature] = generate_signature(params.clone)
56
+ str = parameterize(params)
57
+ end
58
+
59
+ def request(params, api_url, method = :get)
60
+ case method
61
+ when :get
62
+ url = api_url + "?" + generate_params_str(params.clone)
63
+ response = RestClient.send(method, url)
64
+ else
65
+ raise "HTTP method #{method} not supported"
66
+ end
67
+ response
68
+ end
69
+
70
+ def get(params, api_url = nil)
71
+ api_url ||= @api_url
72
+ request(params, api_url, :get)
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloudstack_helper
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Darren Dao
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-02-03 00:00:00 Z
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: rest-client
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 1
28
+ segments:
29
+ - 1
30
+ - 6
31
+ - 7
32
+ version: 1.6.7
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: yard
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 5
44
+ segments:
45
+ - 0
46
+ - 7
47
+ version: "0.7"
48
+ type: :development
49
+ version_requirements: *id002
50
+ description: cloudstack_helper is a simple ruby library that helps making request to CloudStack API easier. With cloudstack_helper, you only have to specify the command the parameters for the request and it will handle everything for you.
51
+ email:
52
+ - darrendao@gmail.com
53
+ executables:
54
+ - cloudstack_rb
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - README.md
59
+ files:
60
+ - README.md
61
+ - bin/cloudstack_rb
62
+ - lib/cloudstack_helper.rb
63
+ homepage: https://github.com/darrendao/cloudstack_helper
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options: []
68
+
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.11
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: cloudstack_helper is a simple ruby library that helps making request to CloudStack API easier.
96
+ test_files: []
97
+
98
+ has_rdoc: