aliyun_ruby_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5f0b6ccede45089c9a08654fea2dffc15b9dd4f9
4
+ data.tar.gz: 2abb1b6d3b155a51133ea00dd11f8217396e55a8
5
+ SHA512:
6
+ metadata.gz: 2b2ea31836e2beca06404e6f170f47e9292fcda291190a2eafebe622886ca3ac30f5f4b1d53893760240a22d05724e9835339c6f20d54a9ce37b65b8c28b1a07
7
+ data.tar.gz: 9172a34b3e3c6ddc8406d899586722de7e78c86c0a6e2e070d56118b91561417cbe70c368d36636a3c8822a7c8572e14bf7663bfe5baa5d799b10d8780d837f6
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.project ADDED
@@ -0,0 +1,17 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>aliyun_ruby_api</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ <buildCommand>
9
+ <name>org.rubypeople.rdt.core.rubybuilder</name>
10
+ <arguments>
11
+ </arguments>
12
+ </buildCommand>
13
+ </buildSpec>
14
+ <natures>
15
+ <nature>org.rubypeople.rdt.core.rubynature</nature>
16
+ </natures>
17
+ </projectDescription>
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aliyun_ruby_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 cheyang
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # Aliyun ECS API Client for Ruby
2
+
3
+
4
+ The Aliyun ECS API Client for Ruby is a library for connecting to and calling [The Aliyun ECS](http://help.aliyun.com/view/11108189_13730407.html?spm=5176.7376125.1997918129.6.k2vNso) from the [Ruby](http://www.ruby-lang.org programming language).
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'aliyun_ruby_api'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install aliyun_ruby_api
20
+
21
+ ## Usage
22
+
23
+ First, you're probably gonna want to require it:
24
+
25
+ ```
26
+ require 'rubygems'
27
+ require 'aliyun_ruby_api'
28
+ ```
29
+
30
+ Then, you need to set
31
+
32
+ ```
33
+ options = {:access_key_id => "xxxxxx",
34
+ :access_key_secret => "yyyyyy",
35
+ :endpoint_url => "https://ecs.aliyuncs.com/"}
36
+
37
+ service = Aliyun::Service.new options
38
+ ```
39
+
40
+ Now, you can call all the aliyun ECS API interface in the following way
41
+
42
+ ```
43
+ parameters = {:[parameter_name] => :[parameter_value]}
44
+
45
+ service.[Action] parameters
46
+ ```
47
+
48
+ (1) For example, if you want to use alyun ECS API to describe regions
49
+
50
+ ```
51
+ parameters = {}
52
+
53
+ service.DescribeRegions parameters
54
+ ```
55
+
56
+ (2) For example, if you want to use alyun ECS API to describe images template of a specified region
57
+ ```
58
+ parameters = {:RegionId => "id", :PageNumber => "2", :RageSize => "20"}
59
+
60
+ service.DescribeImages parameters
61
+ ```
62
+
63
+ Notice:
64
+
65
+ If you want to output the debug info, please add this line before you call the API
66
+ ```
67
+ $DEBUG = true
68
+
69
+ ```
70
+
71
+
72
+ ## Contributing
73
+
74
+ 1. Fork it ( http://github.com/<my-github-username>/aliyun_ruby_api/fork )
75
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
76
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
77
+ 4. Push to the branch (`git push origin my-new-feature`)
78
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aliyun_ruby_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aliyun_ruby_api"
8
+ spec.version = AliyunRubyApi::VERSION
9
+ spec.authors = ["cheyang"]
10
+ spec.email = ["cheyang@163.com"]
11
+ spec.summary = %q{Ruby API client for accessing Aliyun ECS}
12
+ spec.description = %q{Ruby API client for using Aliyun ECS}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '>= 1.9.3'
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_dependency "ruby-hmac"
25
+ end
data/example/ecs.conf ADDED
@@ -0,0 +1,4 @@
1
+ Format=JSON
2
+ Version=2013-01-10
3
+ AccessKeyId=key
4
+ SignatureVersion=1.0
@@ -0,0 +1,3 @@
1
+ require "aliyun_ruby_api/version"
2
+ require "aliyun_ruby_api/base"
3
+ require "aliyun_ruby_api/service"
@@ -0,0 +1,23 @@
1
+ module Aliyun
2
+
3
+ ALIYUN_API_ENDPOINT='https://ecs.aliyuncs.com/'
4
+
5
+ SEPARATOR = "&"
6
+
7
+ HTTP_METHOD = "GET"
8
+
9
+ $ENDPOINT_URL = nil
10
+
11
+
12
+ $ACCESS_KEY_ID = nil
13
+
14
+
15
+ $ACCESS_KEY_SECRET = nil
16
+
17
+
18
+ DEFAULT_PARAMETERS = {:Format=>"JSON",
19
+ :Version=>"2013-01-10",
20
+ :SignatureMethod=>"HMAC-SHA1",
21
+ :SignatureVersion=>"1.0"}
22
+
23
+ end
@@ -0,0 +1,151 @@
1
+ require 'net/http'
2
+ require 'time'
3
+ require 'securerandom'
4
+ require 'uri'
5
+ require 'base64'
6
+ require 'hmac-sha1'
7
+ require 'json'
8
+
9
+ module Aliyun
10
+
11
+ class AliyunAPIException < RuntimeError
12
+ end
13
+
14
+ class Service
15
+
16
+
17
+
18
+ attr_accessor :options
19
+
20
+ attr_accessor :access_key_id
21
+
22
+ attr_accessor :access_key_secret
23
+
24
+ attr_accessor :endpoint_url
25
+
26
+ def initialize(options={})
27
+
28
+ self.access_key_id = options[:access_key_id] || $ACCESS_KEY_ID || ""
29
+
30
+ self.access_key_secret = options[:access_key_secret] || $ACCESS_KEY_SECRET || ""
31
+
32
+ self.endpoint_url = options[:endpoint_url] || $ENDPOINT_URL || ALIYUN_API_ENDPOINT
33
+
34
+ self.options = {:AccessKeyId => self.access_key_id}
35
+ end
36
+
37
+ #The method entry to call ECS url method
38
+ def method_missing(method_name, *args)
39
+ if $DEBUG
40
+ puts "Not Found Method: #{method_name}"
41
+ end
42
+
43
+ if args[0].nil?
44
+ raise AliyunAPIException.new "No such method #{method_name}!"
45
+ end
46
+
47
+ call_aliyun_with_parameter(method_name, args[0])
48
+ end
49
+
50
+ #Dispatch the request with parameter
51
+ private
52
+ def call_aliyun_with_parameter(method_name, params)
53
+
54
+ params = gen_request_parameters method_name, params
55
+
56
+ uri = URI(endpoint_url)
57
+
58
+ uri.query = URI.encode_www_form(params)
59
+
60
+ http = Net::HTTP.new(uri.host, uri.port)
61
+ http.use_ssl = true if (uri.scheme == "https")
62
+
63
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
64
+ if $DEBUG
65
+ puts "request url: #{uri.request_uri}"
66
+ end
67
+
68
+ request = Net::HTTP::Get.new(uri.request_uri)
69
+
70
+ response = http.request(request)
71
+
72
+ case response
73
+ when Net::HTTPSuccess
74
+
75
+ return JSON.parse(response.body)
76
+ else
77
+ raise AliyunAPIException.new "response error code: #{response.code} and details #{response.body}"
78
+ end
79
+
80
+ end
81
+
82
+ #generate the parameters
83
+ def gen_request_parameters method_name, params
84
+ #add common parameters
85
+ params.merge! DEFAULT_PARAMETERS
86
+
87
+ params.merge! self.options
88
+
89
+ params[:Action] = method_name.to_s
90
+
91
+ params[:TimeStamp] = Time.now.utc.iso8601
92
+
93
+ params[:SignatureNonce] = SecureRandom.uuid
94
+
95
+ params[:Signature] = compute_signature params
96
+
97
+ params
98
+ end
99
+
100
+ #compute the signature of the parameters String
101
+ def compute_signature params
102
+ if $DEBUG
103
+ puts "keys before sorted: #{params.keys}"
104
+ end
105
+
106
+ sorted_keys = params.keys.sort
107
+
108
+ if $DEBUG
109
+ puts "keys after sorted: #{sorted_keys}"
110
+ end
111
+
112
+ canonicalized_query_string = ""
113
+
114
+ sorted_keys.each {|key| canonicalized_query_string << SEPARATOR
115
+ canonicalized_query_string << percent_encode(key.to_s)
116
+ canonicalized_query_string << '='
117
+ canonicalized_query_string << percent_encode(params[key])
118
+ }
119
+
120
+ length = canonicalized_query_string.length
121
+
122
+ string_to_sign = HTTP_METHOD + SEPARATOR + percent_encode('/') + SEPARATOR + percent_encode(canonicalized_query_string[1,length])
123
+
124
+ if $DEBUG
125
+ puts "string_to_sign is #{string_to_sign}"
126
+ end
127
+
128
+ signature = calculate_signature access_key_secret+"&", string_to_sign
129
+
130
+ end
131
+
132
+ #calculate the signature
133
+ def calculate_signature key, string_to_sign
134
+ hmac = HMAC::SHA1.new(key)
135
+ hmac.update(string_to_sign)
136
+ signature = Base64.encode64(hmac.digest).gsub("\n", '')
137
+ if $DEBUG
138
+ puts "signature #{signature}"
139
+ end
140
+ signature
141
+ end
142
+
143
+ #encode the value to aliyun's requirement
144
+ def percent_encode value
145
+
146
+ value = URI.encode_www_form_component(value).gsub(/\+/,'%20').gsub(/\*/,'%2A').gsub(/%7E/,'~')
147
+
148
+ end
149
+
150
+ end
151
+ end
@@ -0,0 +1,3 @@
1
+ module AliyunRubyApi
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aliyun_ruby_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - cheyang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ruby-hmac
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Ruby API client for using Aliyun ECS
56
+ email:
57
+ - cheyang@163.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .project
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - aliyun_ruby_api.gemspec
69
+ - example/ecs.conf
70
+ - lib/aliyun_ruby_api.rb
71
+ - lib/aliyun_ruby_api/base.rb
72
+ - lib/aliyun_ruby_api/service.rb
73
+ - lib/aliyun_ruby_api/version.rb
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: 1.9.3
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Ruby API client for accessing Aliyun ECS
98
+ test_files: []